Raspberry Pi aids conservation work

Fox Hunt

© Lead Image © donatas1205, 123RF.com

© Lead Image © donatas1205, 123RF.com

Author(s): , Author(s):

As a countermeasure to predators of rare ground-breeding birds, live traps are monitored by a microcontroller and a Raspberry Pi.

In a nature reserve in northern Germany, foxes and martens threaten rare ground-breeding birds. Although the park supervisor catches the predators with live traps, checking the traps is time consuming and expensive in this virtually inaccessible area. Between 40 and 50 traps have to be monitored, which means long and mostly unnecessary inspections.

The use of modern technology would minimize the considerable effort involved in regularly checking the traps by having a microcontroller and Raspberry Pi team up to monitor the traps electronically and send an automatic alarm when one of the traps is triggered. However, the path from this idea to its implementation took longer than originally thought.

One of the challenges was the lack of mobile Internet in the sparsely populated area, although text message services are available. A previous article [1], in which one of us described how to set up a 3G hotspot with a Rasp Pi and a UMTS [2] stick, was the basis for the collaboration on this project.

The Idea

The basic idea is simple: The PIC16F690 microcontroller with nanoWatt Technology uses a sensor, such as a ball switch or magnetic contact, to monitor the state of the trap and uses the PIC internal analog-to-digital converter (ADC) to monitor the battery voltage. When the trap is closed or the battery voltage is too low, the microcontroller stimulates a connected Rasp Pi and signals the status. The Rasp Pi then sends a text message to the supervisor over a connected UMTS stick, and the supervisor then transports the captured animal away or replaces the battery.

For the status of the trap, the Rasp Pi uses two GPIOs as input (one for the latch and one for the battery). At first glance, the script required seems very simple, because reading GPIOs and sending text messages can be implemented quickly, thanks to the large software base in Raspbian. However, the devil is in the details, because the solution has to be very robust: You would not want a captive animal to die in a trap because text messaging fails.

The Rasp Pi therefore not only has to read out the signals from the PIC but also report back the status. To do this, the setup requires two more GPIOs: one pin for the status and a second that indicates a shutdown to the PIC, which then switches off the power and saves the battery. In the event of a fault, the PIC stimulates the Pi after five minutes for another attempt. After a successful text message transmission, the PIC remains inactive until a reset, which the supervisor must handle.

An optional component is a real-time clock (RTC). Because the Rasp Pi does not make contact with a network, it cannot update its time. The RTC provides the right time for each boot, which means that all log messages have the correct time stamp. The text messages now also contain the transmission time in addition to the case number and the case or power status.

Microcontroller

The PIC16F690 is located on a carrier board that plugs directly into the Rasp Pi GPIO strip (Figure 1). The diagram and the parts list, as well as all programs, are on GitHub [3]. (English versions are available on the Linux Pro Magazine anonymous FTP site [4].)

Figure 1: Prototype: The experimental setup with a connected Rasp Pi and microcontroller; Klappe auf = trap open.

The PIC16F690 by Microchip was chosen because it works with particularly economical nanoWatt Technology. In sleep mode, a watchdog timer monitors the PIC so that the power consumption from the battery is only about 1-1.5µA. Among other things, the controller has 18 I/O pins, an internal oscillator, a 10-bit ADC with 12 channels, flash memory for 4096 words, 256-byte SRAM, and interrupt on change (IOC) at ports A and B. Transfer of data to a 4x16 LCD uses software I2C and the PCF8574 I/O expander. This solution allows transmission at up to 100kHz; the PIC can then be operated at a maximum of 4MHz. This data transfer variant saves I/O pins.

The PIC is set to sleep mode for about five minutes after configuration. It then wakes up and controls the trapdoor sensor and battery voltage. If everything is OK, it will switch to sleep mode again. In this case, the Rasp Pi is left out. If no animal is trapped, a battery charge is sufficient for about a half a year in this setup.

Only when an event occurs (trap closes, battery dies) does the Rasp Pi boot and send a text message. The Pi reports a transmission failure to the PIC, which "remembers" it. After another five minutes, the Rasp Pi starts resending the text and does so repeatedly until the transfer is successful. The system then returns to the idle phase.

The chip is programmed with assembler in a special single-step mode. The information is forwarded by software I2C and pluggable auxiliary boards and is visualized on an LCD with four lines of 16 characters each (Figure 2).

Figure 2: A segment display allows debugging of the setup; Klappe zu = trap closed.

The PIC is connected to the Rasp Pi through four GPIOs (Figure 3). GPIOs 17 and 27 inform the Rasp Pi of the status of a trap, which in turn sets GPIO 22 to HIGH during program startup and to LOW in the event of an error. The shutdown signal comes through GPIO 10. After the value changes to HIGH, the PIC gives the Rasp Pi another minute to shut down and then turns off the power (Figure 4). If the Rasp Pi takes too long (e.g., because a text message is pending), the PIC still strictly regulates the current. The Pi loses no data, apart from some log messages.

Figure 3: The signal lines from the PIC to the Rasp Pi; US = UMTS stick.
Figure 4: The intended schematic for the fox trap.

However, the PIC alone is not enough for the circuit. Another critical component is the boost converter (Figure 5), which uses the 3.0-4.2V battery voltage to generate the 5V required by the Rasp Pi. First, the XL6009 switching regulator module (up to 4A) was used. However, because this was no longer reliable enough in the lower voltage range of 3-3.5V, the ML8205 (2A) module was given preference, which runs smoothly down to 3V. For test purposes, a switch mode power supply connected with an adjustable step-up converter replaces the lithium polymer (LiPo) battery, which means it is possible to stimulate undervoltage without problem.

Figure 5: The carrier board with battery and boost converter.

The pull-up/pull-down resistors integrated in Rasp Pi seem to be some of the least known components of the small computer and proved to be a particular challenge. Although the GPIOs are well documented, with many tutorials about connecting with external pull-up or pull-down resistors, hardly any articles about the internal resistors can be found.

These pull-ups can be configured using the WiringPi library. Once set up, Rasp Pi saves the status, but it can no longer be read out. The data is probably stored in an EEPROM-like cell that cannot cope with arbitrary write operations. However, none of this is documented and is one downside of the Rasp Pi hardware not being completely open.

Rasp Pi Tasks

When the PIC switches on power, the Rasp Pi boots and works directly on its tasks: reading GPIOs, searching for modem, setting PIN code, sending text message, informing PIC, and shutting down again. Ideally, this should take no more than two minutes. However, the implementation has to intercept various error situations.

The /etc/rc.local file starts the /usr/local/sbin/fuchsfalle.sh processing script at the end of the boot process. It then monitors in a loop to see whether the script stops again. After a configurable amount of time, it terminates it regardless of its status, informs the PIC about the failure, and shuts down the system. The Rasp Pi tries to prevent the hard shutdown caused by the PIC.

The search for the modem, the setting of the PIN code, and the sending of text messages all make several attempts in a loop, which proves to be a useful precautionary measure in practice. Sometimes it takes a while for the UMTS stick modem to become available. From time to time, setting the PIN or sending text messages also requires several attempts.

The Rasp Pi behavior is controlled by the central configuration file /boot/fuchsfalle.cfg. The storage location was chosen because it is located in the first FAT-formatted partition and can therefore also be accessed from Windows. However, on the Windows side, you will need an editor that also saves the file in UTF-8 format, which is common on Linux. Notepad++ does a good job of this.

Special Features

The sysfs interface under /sys/class/gpio was originally used for the configuration, as well as for reading and writing the GPIOs. However, when the problems with the internal pull-ups described above arose, we switched to the wiringPi package, which provides a command-line tool and thus performs all tasks without problem from scripts. The following commands are used to configure the pull-ups:

$ sudo gpio -g mode 17 up
$ sudo gpio -g mode 27 up

In addition to up are the down and tri switches. The latter probably stands for "trigger" and switches off the internal resistors. However, it is not possible to read the status with software.

In addition to GPIOs, other challenges need to be solved by the software. Even if it only runs for a few minutes, the system needs to save as much energy as possible. The Rasp Pi Zero is considered unbeatable in this respect, but because UMTS sticks draw a large amount of power, it is not guaranteed that this arrangement will work with all sticks. Turning off the screen and network helps all Rasp Pis conserve energy. An interface-free system on a chip (SoC) also poses no problem in production because communication with the PIC takes place through GPIOs.

The various power-saving techniques were implemented with a separate system service, reconfigured in the central configuration file, which advantageously allows the individual techniques to be switched on and off as required.

Consistent logging is another feature. To ensure that as little information as possible is lost in the event of a power outage, a sync function ensures a secure write to the SD card after every written record and also reduces the possibility of filesystem corruption.

Installation

A Raspbian Lite installation with a number of additional packages, like wiringPi for the GPIOs and gammu for sending text messages, is sufficient, but because the installation involves a number of manual steps, the Raspbian Netinstaller [5] comes into play by automating the entire installation process and ensuring uniform, reproducible, and consistent systems.

The project files are located in the appropriate folders, and the configuration files for the installer are also updated directly in the project. A script downloads the Netinstaller and integrates the files from the project into its own image. After booting, the system then retrieves all necessary files and subsequently configures the system. After installing, all that remains is configuring for the specific trap.

Conclusions

At present, the system is sufficiently advanced to start beta testing, with plans for improvements already in place. For example, the Rasp Pi could send a text message at regular intervals, even without triggering it as a heartbeat message, so that the supervisors can have an overview of all traps. However, this would mean increased capacity requirements with regard to the LiPo battery.

Since the beginning of 2016, small SIM modules like the SIM800L [6] have been around that can be connected to the Rasp Pi through the GPIO. These modules offer an interesting alternative to the UMTS sticks currently used in the project in terms of price and technology. Problems with unrecognized sticks under Linux are then a thing of the past.

The mode switch from a CD-ROM to a modem device, which is necessary under Linux, turned out to be particularly problematic with these sticks, some of which only work under Wheezy and jessie-backports, but not under Jessie. Others work with Jessie without problem, but when switching to the USB modeswitch backport, the system suddenly no longer recognizes it. The software installation then depends on an external component.

Another advantage is that the SIM800 modules draw power directly from the LiPo battery, which means the module even works on first-generation Rasp Pis or the Pi Zero without problem. Additionally, the entire USB complex of the Rasp Pi 2/3 models can be switched off, which saves even more energy.

Regardless of the challenges on the hardware and software sides, many administrative and logistical problems have yet to be solved. For example, you would have to produce the printed circuit board (PCB) in small batches. The many required SIM cards also present a problem; because, ideally, the Rasp Pi only sends a handful of SMS messages per year, prepaid cards are best suited for this purpose. However, managing 40 to 50 contracts is a challenge and, in the worst-case scenario, could lead to false positives.

Finally, financing all the traps is still a problem. Purely from the electronics side, each costs about EUR100, if you produce the PCBs yourself. Another matter of concern is the cost of construction for the concrete pipe traps (Figure 6).

Figure 6: The robust concrete pipe traps will provide a long service life.

The circuit and schematic developed for the fox trap easily can be transferred to other projects that work autonomously and only occasionally transfer data. Because a text message has up to 160 characters, it is suitable for more than just a simple status report, and more information could be distributed across multiple text messages.

If GPRS at least is available in the intended area of application, an Internet connection with the UMTS stick is more suitable for the transfer of large amounts of data. Setting up the stick for this purpose is described in a previous RPG article [7].

The Author

Bernhard Bablok (mailto:mail@bablokb.de) works at Allianz Services SE as a SAP HR developer. When he's not listening to music, cycling, or walking, he enjoys Linux, programming, and SBCs.

Lothar Hiller is a retired intelligence operations engineer. He is a passionate electronics enthusiast with a soldering iron that is almost always hot. Hiller programs microcomputers like PICs in assembler, and since 2015, he has been working with Raspberry Pis in the home applications field.