MeteoBoard Controller
It took us once to measure the temperature and humidity in one room, a simple task, it’s worth going to Google and there to a bunch of examples on Arduino how to do this, sketches, diagrams and explanations. Simple tasks of this kind arise often enough and every time I did not want to modify the code for the controller. We thought, what if we write something like an interpreter for a microcontroller (MK), which would take commands and execute them. Thus, there is no need to write software every time for MK, this lesson often requires more time than under a PC due to the fact that the memory capacity of MK is limited and you will not use advanced tools such as regex and other things. The interpreter must be extensible, that is, adding a new command should not be a furious operation.
Our other projects here
Based on these requirements, it was decided to develop such a device and + libraries for working with it for several languages (now Python and GCC C / C ++) and OS (Win and Linux) so as not to worry anymore with writing software for the controller each time that does a routine - collects data or turns something on or off.
Fig. 1. MeteoBoard Controller
Simply put, I wanted to make a device that does not need to be constantly flashed, to rewrite the firmware code, and again to flash in order to adapt to a new task. I wanted to manage simple commands and put all the control logic into a program on a PC. The last obstacle in this way was the interface through which the device will be connected to the PC. We didn’t really want to deal with USB ports, with drivers and endpoints, it would only complicate the work with the device, so it was decided to raise an open USB stack on the MK and exchange data between the device and the PC via the virtual COM port that appears in system every time we connect the board to the PC. Working with a COM port is already more comfortable than USB, but we decided to go ahead and write a library,
Of course, sometimes such an approach, when the whole control logic is taken to a higher level, is unacceptable, for example, it is required to develop a fully autonomous control system by something, but such tasks are less common and we do not have this case.
Iron
To test the chosen approach, a circuit board based on the ATMEGA32U4 microcontroller with hardware USB 2.0 was designed. The following sensors were installed on the board:
When it was partially soldered, work on software began. We wanted the interpreter that executes the commands to be easily transferred to another hardware, if we suddenly need to scale, for example, it will be necessary to measure the temperature not in 3 places, but in 10, the code should work without changes. Each sensor was assigned a Device ID, as well as a list of relevant Param ID parameters so that any sensor could be contacted without collisions. For example, for a pressure sensor device ID = 5, the parameters can be as follows:
1 for temperature, 3 for atmospheric pressure and 4 for altitude.
Fig. 2. Device sensors
Tab. 1 Commands and parameters
Fig. 3 Data exchange between a PC and a MeteoBoard device
This table will only increase in the future as new controllers with specific requirements appear, but the main thing is that all controllers work according to a unified set of commands, so the code for a PC once written can easily work on a new device (if Of course, physically there is such a sensor on the board). There is no dependence on the type of chip, its manufacturer, etc. If some sensor gives out the temperature, it means passing to the controller a packet that contains the device ID of this sensor and after 001 we will always get the temperature in degrees Celsius and this is for any parameter that is and will be in the table.
Software
You can work with MeteoBoard in almost any language that can access the COM port. It can even be scripting languages like Perl and Python. Access to the equipment can be obtained from any user application through the library for working with the device, at the moment there are libraries for C and Python.
For example, to read the temperature on one of the sensors, you need to call the getDHT11_temp (port) function, and this function already independently exchanges data with the sensor with which it is needed. In order for the function to “know” which COM port the device is connected to, we first scan all available COM ports and send special requests there - the port that answered is of interest to us, the device sits on it, and we work with it further.
1) Python
What does the minimal application that works with MeteoBoard look like?
Here it is:
A library was created for Python for working with the device, which is based on PySerial, which means it will work both on Windows and Linux (the library has not been tested under Linux yet). Working in Python with the device turned out to be especially transparent, the meteoboardlib.py library contains a function for determining which port the board is connected to. The PC simply sends the 'w' symbol to all available ports, and the port that responded with the 'MeteoBoard' line for 0.5s is used in further work.
The following code reads the values of several sensors at the current time and displays all this in the console.
You can already work with this data as you please: to make a partially smart home, to monitor something, to make an expert system, there are a lot of possibilities. The graph below was obtained using matplotlib, the arrays of values were accumulated, and plot was drawn.
At the very beginning, you can see how temperature and humidity jumped, pressure and altitude within the measurement error. When the impact was removed, the data gradually began to return to its original state.
Fig. 4 Visualization of data from the device using matplotlib
You can write data directly to a file, something like a logging system, and then open it in excel and study for anomalies, for example, indoors.
It is also possible to write programs in several lines of code that will monitor temperature, humidity and all other characteristics of the external environment and, depending on what is happening at the current time with the environment, respond according to the program. For example, call a fire brigade when the temperature rises above a critical value
A couple of lines, but how much pleasure there is from the process of obtaining data from the real world, you do not need to solder anything, you do not need to write code for the microcontroller, although I will not hide something special in this.
An interesting area of work here can be sending data from sensors to a remote server in order to be able to monitor the situation from any mobile or stationary device. Or participation in the project of national monitoring , there were several articles on Habré on this issue 1 , 2
2) GCC C / C ++ compiler
For gcc c compiler for Linux a small library for working with the device was written, which is based on RS-232 for Linux and Windows library. All further work was carried out on Linux Ubuntu 12.04, but in theory it should also work on Windows (no tests were done on Windows). Unfortunately, it has not yet been possible to get the port number to which the board is connected in automatic mode, unlike the Python library, here the port is set by hand, and the stub function for determining the port simply gives a constant.
To get data from the device, you need to connect the libraries, get the port number (set by hand) and use the functions.
If you need to visualize the data, then it seemed to me rather fast is the following way: we write the data into a text file in a column, and then display it using gnuplot.
gnuplot >> plot “data.txt”
We get such a graph, here I first breathe on the DHT11 sensor, and then blow a hairdryer on it and dry the air, the moisture percentages along the y axis, simply reports on x, they can be done at any frequency.
Fig. 5 Data visualization with gnuplot
3) Jedi Path, or choose a tool for yourself
Since working with the COM port is a rather ancient technology (but it is still actively used because of simplicity, reliability and easy implementation), there are many libraries written for these purposes for different languages. To work with the device in any familiar language, you need to create a package according to the table, send it to the port to which the board is connected, wait about 10 ms, and then get the package with the necessary information. There are good modules for working with virtual COM ports for Qt QtSerialPort, for perl Device: SerialPort, for Delphi ComPort Library, etc.
In fact, the scope of such a controller is wide enough: you can write an expert system that will collect data about the real external world and make predictions, for example, of weather, in this case. You can also use this system for monitoring in a production room. Of course, some sensors do not have any parameters (DHT11, 5% humidity error), but in most cases even this is enough to signal in time that the "roof in the warehouse has become leaky" and around a puddle. You can send data to some remote server, for example, to the “people's monitoring.” Project. Using MeteoBoard, you can see how the humidity in the apartment changes when the kettle boils, when the window is open, when a bunch of friends come and breathe in the room, this is also a sensor notices. There are a lot of possibilities
I am very glad that we managed to make such a cool device, now thanks to its capabilities we can solve our problems more efficiently and with less time.
Linux Ubuntu 12.04, GCC C
Win7, Python2.7 + PySerial
1) MeteoBoard library link to GitHub
2) RS-232 for Linux and Windows
3) PySerial for Python
4) Perl COM-port lib
Our other projects are here
Our other projects here
Based on these requirements, it was decided to develop such a device and + libraries for working with it for several languages (now Python and GCC C / C ++) and OS (Win and Linux) so as not to worry anymore with writing software for the controller each time that does a routine - collects data or turns something on or off.
Fig. 1. MeteoBoard Controller
Simply put, I wanted to make a device that does not need to be constantly flashed, to rewrite the firmware code, and again to flash in order to adapt to a new task. I wanted to manage simple commands and put all the control logic into a program on a PC. The last obstacle in this way was the interface through which the device will be connected to the PC. We didn’t really want to deal with USB ports, with drivers and endpoints, it would only complicate the work with the device, so it was decided to raise an open USB stack on the MK and exchange data between the device and the PC via the virtual COM port that appears in system every time we connect the board to the PC. Working with a COM port is already more comfortable than USB, but we decided to go ahead and write a library,
Of course, sometimes such an approach, when the whole control logic is taken to a higher level, is unacceptable, for example, it is required to develop a fully autonomous control system by something, but such tasks are less common and we do not have this case.
Iron
To test the chosen approach, a circuit board based on the ATMEGA32U4 microcontroller with hardware USB 2.0 was designed. The following sensors were installed on the board:
- Pressure sensor BMP085 . It measures pressure, temperature and altitude (!), Apparently using a barometric formula. It can measure pressure in the range of 30-110 kPa, works at different heights from +9000 m above sea level and to -500 m below sea level
- Humidity sensor DHT11 . Measures humidity and temperature, does not shine with conversion accuracy, but it is important that it is digital and affordable. Humidity measures in the range from 20 to 90%, the error is 5%, in terms of temperature measurement there are almost room conditions from 0 to 50 C, and the error is 2 C
- The measurement of illumination occurs using a photoresistor . The signal from the photoresistor is connected to the ADC channel, and depending on the lighting, the voltage from the photoresistor changes. The dependence itself is nonlinear, so calibration is additionally performed in the controller. At the output, the programmer receives refined data from 0 to 255, 0 - if it is dark “even though the eye is poked out” and 255 if the photocell is illuminated
- Temperature measurement is done by DS18b20 sensors . Their temperature range is from -55 to 125 C, but I think the rubber casing with which the remote sensor is covered will not withstand such a temperature, a maximum of +70, and at a negative temperature below 30, the casing can crack if it is not carefully moved. Accuracy is 0.5 C. The length of the wire in our case was 2 meters, the maximum length depends on the environment, with a normal shielded cable you can reach 50 meters, there are 3 sensors in total on the board (white contact on the board, one installed, two not soldered)
When it was partially soldered, work on software began. We wanted the interpreter that executes the commands to be easily transferred to another hardware, if we suddenly need to scale, for example, it will be necessary to measure the temperature not in 3 places, but in 10, the code should work without changes. Each sensor was assigned a Device ID, as well as a list of relevant Param ID parameters so that any sensor could be contacted without collisions. For example, for a pressure sensor device ID = 5, the parameters can be as follows:
1 for temperature, 3 for atmospheric pressure and 4 for altitude.
Fig. 2. Device sensors
Sensor | Device id | Parameter ID | Description |
---|---|---|---|
DS18b20 # 1 | 001 | 001 | Temperature (accuracy 0.5 C) |
DS18b20 # 2 | 002 | 001 | Temperature (accuracy 0.5 C) |
DS18b20 # 3 | 003 | 001 | Temperature (accuracy 0.5 C) |
DHT11 | 004 | 001 002 | Temperature (accuracy 2 C) Humidity (accuracy 5%) |
BMP085 | 005 | 001 003 004 | Temperature (accuracy 0.1 C) Pressure, Pa Altitude, m |
Light sensor | 010 | 223 | Digital range 0 to 255 |
Tab. 1 Commands and parameters
Fig. 3 Data exchange between a PC and a MeteoBoard device
This table will only increase in the future as new controllers with specific requirements appear, but the main thing is that all controllers work according to a unified set of commands, so the code for a PC once written can easily work on a new device (if Of course, physically there is such a sensor on the board). There is no dependence on the type of chip, its manufacturer, etc. If some sensor gives out the temperature, it means passing to the controller a packet that contains the device ID of this sensor and after 001 we will always get the temperature in degrees Celsius and this is for any parameter that is and will be in the table.
Software
You can work with MeteoBoard in almost any language that can access the COM port. It can even be scripting languages like Perl and Python. Access to the equipment can be obtained from any user application through the library for working with the device, at the moment there are libraries for C and Python.
For example, to read the temperature on one of the sensors, you need to call the getDHT11_temp (port) function, and this function already independently exchanges data with the sensor with which it is needed. In order for the function to “know” which COM port the device is connected to, we first scan all available COM ports and send special requests there - the port that answered is of interest to us, the device sits on it, and we work with it further.
1) Python
What does the minimal application that works with MeteoBoard look like?
Here it is:
# -*- coding: UTF-8 -*-
from meteoboardlib import * # подключаем функции для работы с устройством
port = getMeteoBoard_port() # определяем к какому порту подключена плата
print u'Температура, % = ', getDHT11_temp(port) # Считываем значения в консоль
A library was created for Python for working with the device, which is based on PySerial, which means it will work both on Windows and Linux (the library has not been tested under Linux yet). Working in Python with the device turned out to be especially transparent, the meteoboardlib.py library contains a function for determining which port the board is connected to. The PC simply sends the 'w' symbol to all available ports, and the port that responded with the 'MeteoBoard' line for 0.5s is used in further work.
The following code reads the values of several sensors at the current time and displays all this in the console.
# meteoboard_test.py
# -*- coding: UTF-8 -*-
from meteoboardlib import * # подключаем функции для работы с устройством
port = getMeteoBoard_port() # определяем к какому порту подключена плата
# запрашиваем необходимые значения и выводим в консоль
print u'Влажность, % = ', getDHT11_temp(port)
print u'Температура, C = ',getDHT11_Hum(port)
print u'Температура, C = ',getBMP085_temp(port)
print u'Давление, Па = ',getBMP085_pressure(port)
print u'Альтитуда, м = ',getBMP085_altitude(port)
You can already work with this data as you please: to make a partially smart home, to monitor something, to make an expert system, there are a lot of possibilities. The graph below was obtained using matplotlib, the arrays of values were accumulated, and plot was drawn.
At the very beginning, you can see how temperature and humidity jumped, pressure and altitude within the measurement error. When the impact was removed, the data gradually began to return to its original state.
Fig. 4 Visualization of data from the device using matplotlib
You can write data directly to a file, something like a logging system, and then open it in excel and study for anomalies, for example, indoors.
# -*- coding: UTF-8 -*-
from meteoboardlib import *
import csv
c = csv.writer(open("MYFILE.csv", "wb"))
port = getMeteoBoard_port() # узнаем к какому порту подключена плата
# выводим заголовок таблицы
c.writerow(["humidity,% ","Temperature,C ","Temperature,C ","Pressure,Pa ","altitude,m"])
for i in range(500):
print i
c.writerow([str(getDHT11_temp(port)),str(getDHT11_Hum(port)),str(getBMP085_temp(port)),str(getBMP085_pressure(port)), str(getBMP085_altitude(port))])
It is also possible to write programs in several lines of code that will monitor temperature, humidity and all other characteristics of the external environment and, depending on what is happening at the current time with the environment, respond according to the program. For example, call a fire brigade when the temperature rises above a critical value
# -*- coding: UTF-8 -*-
from meteoboardlib import *
port = getMeteoBoard_port() # честно узнаем к какому порту подключена плата
temp = 0
temp = getDHT11_temp(port)
while(1):
if temp > 35:
print "Fireman save us!!! ",temp
temp = getDHT11_temp(port)
print temp
A couple of lines, but how much pleasure there is from the process of obtaining data from the real world, you do not need to solder anything, you do not need to write code for the microcontroller, although I will not hide something special in this.
An interesting area of work here can be sending data from sensors to a remote server in order to be able to monitor the situation from any mobile or stationary device. Or participation in the project of national monitoring , there were several articles on Habré on this issue 1 , 2
2) GCC C / C ++ compiler
For gcc c compiler for Linux a small library for working with the device was written, which is based on RS-232 for Linux and Windows library. All further work was carried out on Linux Ubuntu 12.04, but in theory it should also work on Windows (no tests were done on Windows). Unfortunately, it has not yet been possible to get the port number to which the board is connected in automatic mode, unlike the Python library, here the port is set by hand, and the stub function for determining the port simply gives a constant.
To get data from the device, you need to connect the libraries, get the port number (set by hand) and use the functions.
#include
#include
#include
#include "rs232.h"
#include "meteoboard.h"
int main()
{
int cport_nr, bdrate=19200;
cport_nr = getMeteoBoard_port(); // получаем порт(функция заглушка)
if(OpenComport(cport_nr, bdrate)) // пробуем открыть порт, не открывается завершаем работу
{
printf("Invalid ComPort\n");
return(0);
}
while(1)
{
printf("%d %d %d %d %d\n",getDHT11_temp(cport_nr), getDHT11_Hum(cport_nr), getBMP085_temp(cport_nr),getBMP085_pressure(cport_nr), getBMP085_altitude(cport_nr));
}
return 0;
}
If you need to visualize the data, then it seemed to me rather fast is the following way: we write the data into a text file in a column, and then display it using gnuplot.
gnuplot >> plot “data.txt”
We get such a graph, here I first breathe on the DHT11 sensor, and then blow a hairdryer on it and dry the air, the moisture percentages along the y axis, simply reports on x, they can be done at any frequency.
Fig. 5 Data visualization with gnuplot
3) Jedi Path, or choose a tool for yourself
Since working with the COM port is a rather ancient technology (but it is still actively used because of simplicity, reliability and easy implementation), there are many libraries written for these purposes for different languages. To work with the device in any familiar language, you need to create a package according to the table, send it to the port to which the board is connected, wait about 10 ms, and then get the package with the necessary information. There are good modules for working with virtual COM ports for Qt QtSerialPort, for perl Device: SerialPort, for Delphi ComPort Library, etc.
Application:
In fact, the scope of such a controller is wide enough: you can write an expert system that will collect data about the real external world and make predictions, for example, of weather, in this case. You can also use this system for monitoring in a production room. Of course, some sensors do not have any parameters (DHT11, 5% humidity error), but in most cases even this is enough to signal in time that the "roof in the warehouse has become leaky" and around a puddle. You can send data to some remote server, for example, to the “people's monitoring.” Project. Using MeteoBoard, you can see how the humidity in the apartment changes when the kettle boils, when the window is open, when a bunch of friends come and breathe in the room, this is also a sensor notices. There are a lot of possibilities
I am very glad that we managed to make such a cool device, now thanks to its capabilities we can solve our problems more efficiently and with less time.
Video 1. Reading data from the device to the console
Linux Ubuntu 12.04, GCC C
Video 2. Working with the device through Python code
Win7, Python2.7 + PySerial
Additional and information
1) MeteoBoard library link to GitHub
2) RS-232 for Linux and Windows
3) PySerial for Python
4) Perl COM-port lib
Our other projects are here
Only registered users can participate in the survey. Please come in.
Do you need such a controller to monitor the climate in the server \ refrigerator \ outside the window and start making your smart home?
- 6.4% No 38
- 29.4% Gather myself 172
- 53.8% Yes, I need 315
- 5.9% I have something like that 35
- 4.2% understood nothing 25