Iot
Pycom
Lopy4
PIR
Animal
http
TTN
Education
Lora
webhook
Twilio
This tutorial is about make a Iot (internet of things) system to detect when a animal is very close to one or more water sources.
An Lopy device with one or more PIR sensors detect movement of an animal and trigger scripts through LoraWAN.
There will be two main scripts that will be triggered.
One to collect whether data from another api and contain information in a database for data presentation.
The other one will access another api to send an sms when animal have been detected.
To just set up with the code and instructions in this tutorial may take 1-2 hours depending on your experience.
This project have many different parts. Each part have to work properly and communicate the right way. To install, setup, program, test, construction and learn would take a week of full time-work to implement, If everything is planned. Also minimum one day to test the system on real environment. For me personally i had experience with Python before and had the basics in micro controllers. But the communication between different platforms i had to learn more about. I had different version of my project and this is the one i am most satisfied with. For me it took more or less 2 weeks to implement everything.
I live with my parents, and they have a stud farm with many horses. On the summer they need plenty of water, depending on the weather they will drink more or less often.
Often where the water source are is not close to any local networks and limited energy sources. With enough data it could be possible to somehow
predict how much they need to have access to water.
There are some interest in knowing how often horses are drinking. Also even more interesting to know how many times they drink compared to the weather.
Also explore some more http possibilities to alert when an animal is detected.
This project will give insight in how hardware can interact with the cloud and hook with https servers to interact with different apis. There is
research that are conducted right now trying to implement different monitoring systems for horses, Example see movement path they have and detect while they may give birth to a foal.
Component | Minimum Needed | Maximum Needed | Usage | cost | Store |
---|---|---|---|---|---|
Lopy4 (with expantion board) | 1 | 1 | Main microprocessor to use sensor and send data with LoraWAN. | 750 sek | electrokit (THis is for Lnu students.) |
antenna | 1 | 1 | An antenna to be conected to loraWAN port to increase connectivity. | 40 sek | electrokit |
Lipo battery | 1 | 1 | An battery to conecct to lopy device as power supply. | 50 - 300 sek | electrokit |
PIR mini SR602 | 1 | 8 | Cheap low power sensor to sense movment. | 50 sek | electrokit |
Cables (long) | 5 | 2 + 3 * SR602 | Cables to connect sensor(s) to the lopy device. | 9+ sek/st | electrokit |
Computer | 1 | 1 | To program the lopy device and host http servers and database. | ? sek | komplett |
Case material | 1 | 1 | Anything to use as a case and hold it together. Example the box lopy freighted with and some tape, or some kind of customize plastic case. | ? sek | ? |
After collecting the nessesary materials for the hardware part preparation of the software can continue. This is a personal choice for me personally i like virtual studio codes best but i want to use the tools from ATOM.
For this projekt it is recommended to use two IDE (integrated development environment), one for program the lopy and one for mostly everything else
It is recomended use ATOM to program lopy and upload it to the lopy device. Do not forget to install Pymakr Plugin for it, This will give you easy to use console with download and upload functionality. It is extreamly recomended to firmware update your lopy before you start. Recomended using default setnings. To know what port your lopy is simply open Device manager on your computer, open up Port (COM & LPT) and connect your lopy device. After your computer have detected your device you can now see the new port that you are gonna use.
For most other purpose Visual Studio Code. But this may be a up to the reader on what to IDE to use. I recomend using Anaconda to make create an environment for your code on the computer. When Anaconda is installed simply install everything you need by type this command in Anaconda promp on your computer.
After you have said yes to the installation activate it by typing the below code in CMD or Anaconda promp
Install all modules needed by using the command below.
In the future simply navigate a map on your computer to where your code is and type cmd in the field you see in the image bellow and then activate your environment.
To run a python script is simply type:
Other software that gonna be used is Docker. Install and activate Hyper-V.
To set up the mysql database and grafana used in this projekt open cmd in a folder with this yml file:
Then just print in the command:
For the diffrent API's (Application Programming Interface) that gonna be used get an account for eatch listed below. Free acounts are just needed.
Device / Feature | consumption | consumption per hour |
---|---|---|
Lopy (idle) | 35.4 mA | 35.4 mAh |
Lopy (running) | 70 mA | 70 mAh |
Lora (idle) | 1 uA | 0.001 mah |
Lora (Transmitting) | 108 mA | 0.02916 mAh |
PIR mini SR602 | 20 uA | 0.02 mAh |
The formula to calculate A (ampere) to Ah (ampere hours) is:
To transmit i make the assumption that it will take one secound in time.
The device will as least send a payload 6 times a hour , That means the power consumation becomes with only one sensor:
In worst case when the device will send a payload every minute with one sensor will be:
The battery i used is a lipo battery containing 8000 mAh. And converting that to how many hours the device can be on in worst case will then be:
Before implementation lets get everything we need from the different platforms.
This is where the lopy device gonna send to with the LoraWAN. It will use the MQTT ( Message Queuing Telemetry Transport) protocol.
This is one of the most simplest platform to use when handling data from a device from LoraWAN into the cloud. Easy to decode and send to other integrations.
You will need the accses key that you will find in overview of the application you create in TTN. Also you need the EUIthat you will find in settnings of your application.
I chose to host my own https servers with help of the http.server module in python. it is very easy to handle information and queries in python. The hardest part is just to port forward your home router. You have to figure it out how to port forward your own router because it depends on the constructor. In this projekt port forward port 9123 and 9321.
To lower the amount of complexity from the physical devices i get my weather data from another whether station nearby. Easy to fetch data with python. The data that is fetched will be in json format.
After creating an account you can generate a api-key to use in your code.
This projekt only need the free subscription.
Twilio is a platform with tools to integrate with phones. To try it is free but little limited if not subscribed. But it is enough to use a while. When a account is created you will need to get your SID-code and Auth token. Also you have to generate a phone number where it will send your message.
Docker is one of the best way to create virtual environments. your database will work best while in it own space. There is no cost to get and usfull for any future projekts.
I personally chose mysql because i have some experience with it before. Also not very hard to connect and use with python. This will enable the data that are collected to be saved endlessly, most online sulutions only save data limited time.
Grafana will be installed together with mysql. This is a very good tool to show data and do calculations with the data.
In a earlier version of this projekt i used ubidot instead. It could save the data up to 7 days and it required that all the data needed to come from the lopy. So i had to send the whether data to the lopy to send the whether data to ubidot. This works fine but was added none needed complexity.
For a complete repositorie you can get from github here.
Fist some kode that are in the lib folder on the lopy memory.
here i define a module to get my keys needed to get on the lora network. Easy to change before upload.
This module will handle everything with the sensors. It will get a file named pin_used.txt to get what pins to use. It will create a iterable list of abc objects to easy check every sensor that are used.
With help of threading it will queue diffrent light configurations on the lopy without taking any priority of the main thread.
the code in the none lib folder:
Boot will be the first file to run. Here it will connect to the LoraWAN.
Here the the logistic how the lopy will loop it's code.
A simple decoder that only care about what port it is send to. Port 2 is a sensor trigger. Port 2 is a echo/alive call. It will also send what pin that were activated.
This file is for connecting to the local mysql server. This will automatically create the table where the data will be stored.
This file opens a http server where TTN can post to. When posted to it will fetch weather data and call methods to push information to the mysql server.
When posting from TTN it will send a request to Twilio. It will check the payload if it gonna send or not.
To be able to connect TNN with the https servers simply create a https integration and get your global ip adress and set as address with : the port you use.
I Use the lora network, where this device will be used do not have access to wify and there are a lora access point not so far away.
With the code above it will send a echo/alive payload. This payload is just an integer of 255 or in hex 0xFF. I would want to use no payload but lopy do not support unnecessary transmission so it must have a payload. The data that is transmitted will have the lowest rate possible, this will increase the connectivity range but limit the amount of bytes per payload. The limitation on bytes do not affect this project we have very low amount of data that is needed to be send.
Lopy'n will check if the motion sensor is triggered every other secound, This motion sensor that are small and low power is very simple. When it is triggered it will send 3.3v in 3-5 secounds and then become 0v. plenty of time to not check all the time. They payload when the sensor are triggered have a payload with bytes what number the pin had, etc pin name with only the number.
When the data comes to the TTN it is connected to 2 webhooks to your own http services. There are two because you do not want to have the sms service on always.
By having whether fetched from another whether station the power consumption that would be needed if those sensors where on the lopy does not apply. This is more cost efficient for this device.
With help of grafana it is easy to have a good looking data presentation.
This data is while the device have collected from one watersource in a horsebox in a stable. Under a time while the horse was inside one night. Top left show every trigger that happens. Top right show how many times it detected something near water source per hour. Rest of the graphs are weather data colected every trigger.
weather data is saved minimum each 10 minutes wile the device is alive. The sensor can trigger the data more often maximum 1 minute between each.
When the lopy do not send data data will not be saved to the database.
Mysql is a easy to handle database, it is very good for webservers.
The automation with sms could be trigered by the grafana but i wanted to just see every time something where detected so i connected the sms script directly to TTN.
Here are some images of the final design.
This image is when having two sensors connected without the case.
Here is the device installed at a horse stable box to gather data.
Here is a screenshot of the aoutomatic sms