The return of the Yoctopuce mailbox

The return of the Yoctopuce mailbox

Do you remember the laser mailbox? It works quite well, but we noticed a very annoying defect. Because the mailbox is made of metal sheets, it tends to slightly deform itself depending on temperature changes. This deformation changes the laser beam trajectory inside the box, sometimes to the point of bringing it off the light sensor target. Therefore, we had to regularly adjust the mailbox to solve this issue. After a while, we had enough and we came up with another idea to fix this. But this time, we are seeing big: we are going to render our mailbox autonomous thanks to a solar panel and a Wifi connection.



Distance sensor + Wifi

The idea is to use a small optical distance sensor instead of the laser. Therefore, we don't need the mirror anymore. If there is nothing in the mail box, the sensor measures the height of the box. If there is something, the measured distance is much smaller. Actually, as there are two parts in our mailbox, we could even discriminate between letters and parcels. We selected a led based sensor with a 4-20mA output. The order of precision is of the 10th of millimeter, which is clearly an overkill, but at least it's serious material.

Our 4-20mA distance sensor. Objectively, it's an overkill, but it works so well...
Our 4-20mA distance sensor. Objectively, it's an overkill, but it works so well...


The plan is to put the distance sensor under the mail box
The plan is to put the distance sensor under the mail box


Obviously, the sensor is connected to a Yocto-4-20mA-Rx. And while at it, we might as well solve the problem of unending electric cables with a YoctoHub-Wireless. However, we must power this hub.

The electronic components of the project: a YoctoHub-Wireless and a Yocto-4-20mA-RX
The electronic components of the project: a YoctoHub-Wireless and a Yocto-4-20mA-RX



Solar panel and USB batteries

We can easily power our YoctoHub-Wireless with a simple USB battery that we charge with a solar panel, as long as we use the same trick as for the weather station. The YoctoHub-Wireless is indeed able to sleep on demand and to wake up at a regular interval. When sleeping, the YoctoHub-Wireless consumes almost nothing. The obtained average consumption is quite compatible with battery-operated devices.

Two USB batteries
Two USB batteries



About USB batteries

These USB batteries are essentially designed to charge hand-held devices, such as smart phones or tablets. Some of these batteries have a feature very useful when charging a phone: when they detect that there is no significative battery consumption anymore, they infer that the device is charged and shut down completely. They won't switch back on before another device is physically connected.This prevents the battery from discharging itself on its own. However, this type of battery is to be avoided for our application. They definitively switch off as soon as the hub goes to sleep. So be very particularly attentive to this issue when you buy a USB battery for a purpose other than charging your phone.

Response time

We configured the hub to wake up every 5 minutes around the time the mail person comes by, and once every hour at other times. However, it would be a pity to have to wait an hour for the system to notice that the mailbox has been emptied. It so happens that the YoctoHub-Wireless contains an input enabling us to artificially wake it up. This input is made of two contacts that must be shunt to wake up the hub. So we need to connect these contacts to a switch which closes the circuit as soon as the door of the mailbox is opened. Actually, as long as this contact is closed, the hub cannot go back to sleep. This gives us time to empty the mailbox without hurrying.

We use a switch to detect when the mailbox door is opened
We use a switch to detect when the mailbox door is opened


The installation diagram
The installation diagram



Realizing the project

The realization is relatively easy. We put all the electronics in a waterproof metallic box that we fixed at the back of the mailbox. As the installation works at most a few seconds every 5 minutes, it's unlikely to overheat.

We packed it all in a waterproof metallic box
We packed it all in a waterproof metallic box


The installation components
The installation components


The distance sensor is put in a printed enclosure fixed on the underside of the mailbox and the solar panel is put on top of the box.

It's almost ready
It's almost ready



To signal the presence of mail in the mailbox, we go on using our luminous frame, as we are very happy with it. It's a picture frame which we somewhat tinkered with and which contains a Yocto-Color.

The luminous frame is used to signal mail in the mailbox
The luminous frame is used to signal mail in the mailbox



Programming

The programming part is very simple, as long as you attack it from the correct angle. We decided to code in Python, but any language would have done as well. We simply run our program on a computer with access to the same network as the mailbox, and we connect the luminous frame to it via USB. The idea is to pre-register the hub in the mailbox, to register a callback which is automatically called as soon as a module appears. Thus, each time the hub wakes up, our callback is automatically called once for the hub and once for the Yocto-4-20mA-Rx connected to it. The main part of the code consists in setting up the callback and in waiting.

# Setup the API to use local USB devices
if YAPI.RegisterHub("usb", errmsg)!= YAPI.SUCCESS:
    sys.exit("init error"+errmsg.value)

# configure the API to use the mailbox hub if available
if YAPI.PreregisterHub("172.17.17.76", errmsg)!= YAPI.SUCCESS:
    sys.exit("init error"+errmsg.value)

# Each time a device comes online, deviceArrival is called
YAPI.RegisterDeviceArrivalCallback(deviceArrival)

while True:
    YAPI.UpdateDeviceList(errmsg) # traps plug/unplug events
    YAPI.Sleep(500, errmsg)   # traps others events



Each time our callback is called, we check that its origin is truly the mailbox Yocto-4-20mA-Rx. If it is the case, we retrieve the distance measured by the distance sensor connected to it, we update the luminous frame leds, depending on the distance, and we then ask the hub to go back to sleep after 5 seconds.

def sendHubtoSleep():
  hub = YWakeUpMonitor.FindWakeUpMonitor("MailBox_Monitor")
  if hub.isOnline() : hub.sleep(5)

def deviceArrival(m):
    print('Device arrival : ' +  m.get_serialNumber())
    name = m.get_logicalName()
    if (name=='Mail_Sensor'):
         sensor = YGenericSensor.FindGenericSensor("Mail_Sensor.genericSensor1")
         value = sensor.get_currentValue()
         SetMailIndicator(sensor,"%f" % value)
         sendHubtoSleep()
         sensor.registerValueCallback(SetMailIndicator)



We added a little twist to manage the case when someone is emptying the mailbox: we put a second callback on the changes in the values measured by the Yocto-4-20mA-Rx. Each time this value changes, this callback is called, the frame leds are updated, and if the color was modified, putting the hub to sleep is delayed by 5 seconds. Therefore, as long as the mailbox door is open, the hub stays awake and the luminous frame is updated in real time. Convenient.

def SetMailIndicator(mailSensor,value):
  global laststate
  led1 = YColorLed.FindColorLed("Mail_Indicator.colorLed1")
  led2 = YColorLed.FindColorLed("Mail_Indicator.colorLed2")

  if (float(value)<200):
    led1.set_rgbColor(0xFF0000)
    led2.set_rgbColor(0xFF0000)
    state = 1
  else:
    led1.set_rgbColor(0x00FF00)
    led2.set_rgbColor(0x00FF00)
    state = 0
  if (state!=laststate): sendHubtoSleep()
  laststate = state


Here we are, we more or less went around the issue. You can find the complete code here.

A mailbox with an antenna, what could be more normal? :-)
A mailbox with an antenna, what could be more normal? :-)



Conclusion

We decided to go all out by making our mailbox both Wifi and solar. But you don't have to go that far. You can do without the solar panel, for instance. You will simply need to recharge the battery once in a while. You can also decide to do without the USB battery and draw a 5 volt wire up to your mailbox... It's up to you...

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.