IoT
Examination
Name: Adam Wengrud
Student credentials: aw223na
Time estimation following this tutorial: 8h
Operating system: macOS Catalina Version 10.15.5
This project is about how to automate a specific object tied to a cord. By, controlling the electrical circuit on the cord with sensor values and a relay. In this case, the object is a floor-fan, controlled by temperature and humidity. Data from the sensor is being handled by a microcontroller that are sending signals to control the electrical circuit for the physical object. Data will be transmitted through an MQTT server and a local TIG-stack. Which uses Grafana to present data on the internet. This project also contain a trigger functions in Grafana that sends me a notification on discord when the fan is on.
The reason why I chose this type of project is because of my interest in optimization and efficiency. I wanted to understand how to use IoT to transmit data from a sensor on the internet and actually automate and optimize a physical object by the data. The purpose of the project itself does not make a huge difference in my daily life. For me, the project was more about understanding the powerful technology of IoT.
I think this is a good starting project for someone who wants a straight forward project for an introduction to IoT. This project is easy to understand and will give insights on how IoT technology can be used for optimization. It also gives a good overview of how all elements from the sensor, protocols, databases, and internet are connected. This project will give you the base of knowledge to develop more complex projects later on and most important the knowledge to communicate and discuss this growing technology with others.
1. LoPy4 with headers
LoPy4 is a combination of wireless radios (WiFi, Bluetooth, LoRa, SigFox) and a MicroPython development platform that helps to get started with making Internet of Things (IoT) endpoints. The device has different inputs and outputs which is necessary to understand before setting up a project. The LoPy4 documentation is shown on pycom.io.
2. Expansion board 3.0
The board is where code is being handled. Here is also where you power your device through USB or batteries.
3. Antenna Kit
A universal LoRa & Sigfox Antenna that can be used with the LoPy, SiPy, LoPy4 and FiPy IoT Development boards. This project uses WiFi and micro USB to the computer, so this antenna is optional. But, using a LoRa or Sigfox radio without the external antenna can lead to damage to the device.
4. DHT11 sensor
A module that measures and provides data. The component contains three pinouts: 3-5V, GND, Data. Product specification for this sensor is a temperature range of 0-50 °C , error of +/- 2 °C, and humidity range of 20-90 %, error +/- 5 %.
5. Relay
Controls the electrical circuit. It´s like a switch, when the module gets a signal from the pycom device it closes the circuit and the power is on. Else the power can´t go through. The terminals can handle, normally closed 120-240 V (NC), normally open (NO) 120-240 V and common, from the AC side. From the DC there are three pinouts, Voltage +5V, GND, and Signal.
6-7. Jumper Cables
Propper cables that will be used as a connection between pycom device and module/breadboard.
8. Breadboard
A neat tool to let you create simple and organized circuits.
11. Non-conductive enclosure
Very important for safety reasons. For this project, you need to cut and handle cords on 230 V. Therefore the relay has to be put inside a non-conductive enclosure for safety reasons. Also, make sure something is keeping the cords in place so there's no risk of drag it out by accident.
It might be the case that you first need to update the firmware on the expansion board, I didn´t. But, if you do, you can check that by follow the linked documentation under Expansion board 3.0. When you have downloaded the recommended files. You need to follow the guide and execute all the steps in the computer terminal.
Useful commands | |
---|---|
$ cd | change direction in the terminal |
$ ls | shows what is in the current file |
As IDE I use Atom, which can be downloaded at https://atom.io.
Atom basics, how to upload code and structure your files.
Depending on which platform you want to use for transmitting data you should choose the appropriate "type". Pybytes is the easiest choice when getting started. They already have their libraries installed in "type" pybytes.
If you want to run with "type" pybytes their will be a conflict between multiple MQTT arguments later on. You need therfore disable the Pybytes lib by using the code line below in the REPL-interface, when you want to connect with your MQTT lib.
Note: Power source for the Pycom device is through micro USB connected to computer. Which is not shown in the picuture below.
The calculation is about how long time the sensor and fan can run before they consuming 1kWh.
P = Watt (W), V = Voltage (V), I = Amp (A)
Power source:
3.3V from microcontroller (DC).
230V from house electricity to fan (AC).
Prerequisites for the DHT11 sensor to work is DC power supply with 3-5V. The DC power supply comes from the pycom device, through 3.3V connected to the sensor. The sensor have a min and max milliAmp(mA) supply during measuring. Powered by a range of [3:5] V the sensor consumes [0.5:2.5] mA (while requesting data). The sensor also consuming [100:150] uA on standby, which neglects in this case. To calculate total time for consume 1 kWh, we need to calculate absorbing Wh on both sensor and fan.
Time to consume 1kWh if measuring every tenth minute.
Further, the fan is powered by 230V AC(house electricity) and is absorbing 40Wh. That means that 1kWh can power the fan in 25hours (same calculations as above). The total time for the energy consumption of 1kWh is therefor:
Which means that the project can run for 24.98h to consume 1kWh. This calculation gives a good perspective on how powerful the sensor technology is according to low power consumption.
I started by presenting data on the Pybytes platform. Where data is automatically stored in the cloud and it uses an MQTT protocol. Pybytes is a user-friendly platform that is automatically connected to the device through the Pycom library, that you get if you update your device with “type” Pybytes. It´s good and it works but, you don’t really have the authority to see what happens in the database. Therefore I created a local TIG-stack with containers of Telegraf, Influxdb, and Grafana on a MQTT server instead. Telgraf is used to collect the data from the MQTT server and send it to the Influxdb. Influxdb stockage the data and send it to Grafana where the data can be presented on the internet. The reason why to set up a TIG-stack was basically that I wanted the authority to control and see everything that happens, which I also think is good for an understanding of the process. I´ve tried both of these platforms, and both are free. For me, it was good to start in Pybytes and with time go more to a local installation as the complexity raises as more authority you get. If I would to scale this idea I would absolutely run a local installation with TTN and maybe using Node-red as well.
main
read_dht
For this project, data is sent every tenth minute (600 sec), when the device is plugged in with micro USB. The choice of sending interval is based on how fast temperature and humidity change. Normally that doesn´t happen very quickly. Therefore I send data every 600second.
The data is further transmitted to the internet through WiFi as a wireless protocol and MQTT and webhooks as transport protocols. WiFi means that the wireless protocol can handle high data rates and also is able to consume a lot of battery power. But, that comes to a cost of low range. Therefore this project actually is more suitable for the usage of LoRa as a wireless protocol. My DHT11 sensor sends temperature and humidity values of low data rate (one byte per value) which means that I don´t need the big data rate advantage the WiFi gives. Therefore it´s better to use the range and mobility advantage through the LoRa protocol which also is less battery consuming, as shown in the picture below. The reason why I didn´t use LoRa was because I was out of range for a station and didn´t have the equipment to solve it.
As a transport protocol, I use MQTT which is a custom protocol for IoT. MQTT is useful because it´s lightweight and takes minimum bandwidth that does ensure high latency. Data send through webhooks in json objects (JavaScript Object Notation), which is a specific format, that is shown in step 4 below. At first, I wanted to use my TIG-stack with the gateway protocol TTN (The Things Network) but I didn´t get any connection with LoRa therefor I use MQTT instead.
You already have the main code where you publish data on MQTT. That´s shown code line 48-52.
Check data from the MQTT server. Download MQTT explorer and log in on your MQTT server.
For setting up the TIG-stack I highly recommend the TIG-stack guide. I used that and it does describe every element you need to know about create your own TIG. Note that you need to find the TIG-stack map from the terminal when you run it. Depending on where you place your TIG map.
My dashboard in Grafana contains the process of inside temperature and humidity in a room. The graph shows the values over the last six-hour. I also use “Stat” visualization to present current values and max/min over the last six hours. In the middle of the board, I have an on/off chart which is illustrated in binary code that represents the status of the fan. This is coded in main and read as “counter”, sets to 1 when temperature is over 24 Celsius. I thought it would be a neat feature to have, to clearly see when the fan is on and off.
On the Object graph, I also created an alert function that automatically sends a status notification to my discord server when the counter is 1 for a timeintervall of 2min.
For setting up Grafana i refer to TIG-stack guide agian. To the topic "Visualizing data with Grafana", it doesn´t get anymore clear than that.
My dashboard is built upon two graphs, one text, and six “Stat” visualizations. The principle is the same for all except text visualization. You need to get data from somewhere and filter what you want to present, as shown in the pictures below.
For the alert function you need to first create a notification channel.
After that you choose that channel into where you want to apply the alert and decide settings.
The database I use in this project is a time series database called Influxdb. The data in this database is being aggregated over time which means that every data point has a timestamp associated with it, compared to rational databases that are based on tables. The biggest difference is that rational databases are more scalable but a time series database often has a faster ingestion rate. Which makes it perfect for me in this project as a combination with IoT where I monitoring devices. As shown in the picture below rational databases also have a fast ingestion rate at first but decrease by size of data.
https://devconnected.com/the-definitive-guide-to-influxdb-in-2019/
Data is saved as often as you send it. For this project as I mentioned earlier that data is sent every tenth minute (600 sec). In my time series databases, Influxdb can take care of dropping data after a certain time, with a concept called retention policy. That defines how long you are going to keep your data. My Influxdb is set to default which automatically sets the retention policy to “autogen” and will keep my data forever, if I don´t want to erase it.
The floor-fan works! My intention with this project was to get a great understanding of IoT technology and how it works, which I did. I also gained knowledge to control electrical objects tied to a cord which I think is a neat thing to know. My pre-knowledge before this project was programming, basic python. Besides that everything was new to me, which means that I developed a lot of valuable knowledge from this course and by doing this project.
This course gave me a new perspective on the importance of low power consumption and how powerful the IoT technology is. Therefor I really wanted to connect my project to LoRa and work with TTN, which didn´t work because of long-distance. Also, it would have been a good idea to add batteries connected to the device instead of always power with micro USB. This would effect the mobility on the device which I think that is an important aspect to take along to future projects.