It took us once to measure the temperature and humidity in the same room, a simple task, you should contact Google and there to a heap of examples on Arduino how to do it, sketches, diagrams and explanations. Simple tasks of this kind arise quite often, and each time we did not want to modify the code for the controller. We thought, what if we wrote something like an interpreter for a microcontroller (MK) that would accept commands and execute them. Thus, there is no need to write software every time for the MK, this exercise takes time often more than under the PC due to the fact that the memory of the MK is limited and you don’t use advanced tools such as regex and others. The interpreter should be extensible, that is, adding a new command should not be a furious operation. Also in the future, it should be easily transferable to ARM Cortex-M3 architecture and able to work through various communication interfaces, be it RS485, Ethernet, ZigBee.
Based on these requirements, it was decided to develop such a device and + libraries to work with it for several languages ​​(now Python and GCC C / C ++) and OS (Win and Linux) so as not to steam anymore with writing software for the controller that does the routine every time - collects data or turns something on or off. ')
Fig. 1. MeteoBoard Controller
Simply put, I wanted to make such a device that I do not need to always flash, rewrite the firmware code, and flash again to adapt to a new task. I wanted to manage simple commands and bring all the control logic to the program on the PC. The last obstacle on this path was the interface through which the device will be connected to a PC. We didn’t really want to deal with USB ports, drivers and endpoints, it would only complicate the work with the device, so it was decided to pick up the open USB stack on the MK and exchange data between the device and the PC via the virtual COM port that appears in the system every time we connect the board to the PC. Working with a COM port is more comfortable than USB, but we decided to go further and wrote a library that takes over all the rough work of finding the port to which the board is connected and initializing it, while giving the user only useful sensor data devices.
Of course, sometimes such an approach, when the entire control logic is brought to a higher level, is unacceptable, for example, it is required to develop a fully autonomous control system than either, but such tasks are less common and we do not have this case.
Iron To test the chosen approach, a printed circuit board based on an ATMEGA32U4 microcontroller with USB 2.0 hardware was designed. The following sensors were installed on the board:
Pressure sensor BMP085 . It measures pressure, temperature and altitude above sea level (!), Apparently using a barometric formula. It can measure pressure in the range of 30-110 kPa, works at different altitudes from +9000 m above sea level and up to -500 m below sea level
DHT11 humidity sensor . Measures humidity and temperature, does not shine with the accuracy of conversion, but it is important that it is digital and accessible. Humidity measures in the range from 20 to 90%, an error of 5%, in terms of temperature measurement here are almost room conditions from 0 to 50 C, and an error of 2 C
Measurement of light occurs using a photoresistor . The signal from the photoresistor is fed to the ADC channel, and depending on the lighting, the voltage from the photoresistor varies. The dependence itself is non-linear, so calibration additionally occurs in the controller. At the output, the programmer receives the refined data from 0 to 255, 0 - if it is dark “at least take out the eye” and 255 if the photocell is illuminated
Temperature measurement is made with DS18b20 sensors. Their temperature range is from -55 to 125 ° C, but I think the rubber casing with which the remote sensor is covered cannot withstand such a temperature, maximum +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 limiting length depends on the medium, 50 meters can be reached with a normal shielded cable, there are 3 sensors on the board (a white contact on the board, one is installed, two are not sealed)
When it was partially sealed, work began on the software. We wanted an interpreter that executes commands to be easily transferred to another hardware, if we suddenly need to scale, for example, we will need to measure the temperature not in 3 places, but at 10, the code should work without changes. Each sensor was assigned a Device ID number, as well as a list of actual Param ID parameters, so that any sensor can be accessed without collisions. For example, for the pressure sensor device ID = 5, the parameters can be the following: 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 from 0 to 255
Tab. 1 Commands and Parameters
Fig. 3 Data exchange between the PC and the MeteoBoard device
This table will only grow in the future as new controllers appear with specific requirements, but the main thing is that all controllers work on a unified set of commands, then the code for a PC once written can easily work on a new device (if, of course, physically there is such a sensor on board). There is no dependence on the type of chip, its manufacturer and so on. If a sensor produces a temperature, then by passing a packet to the controller that contains the device ID of this sensor and then 001, we 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 hardware can be obtained from any user application through the library to work with the device, currently 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 is already exchanging data with the sensor with which it is needed. In order for the function to “know” to which COM port the device is connected, we first scan all available COM ports and send special requests there — the port that answered — we are interested, the device is sitting on it, working with it further.
1) Python
What is the minimal application that works with MeteoBoard? Here it is:
For Python, a library was written to work with the device, which is based on PySerial, which means it will work both on Windows and Linux (under Linux, the library has not been tested yet). Working in Python with the device turned out to be particularly transparent, the meteoboardlib.py library contains a function for determining which port the board is connected to. The PC simply sends the 'w' character to all available ports, and the port that responded with the string 'MeteoBoard' for 0.5s is used in further work.
The following code reads the values ​​of several sensors at the current time and displays all of this in the console.
You can already work with these data as you like: do a partially smart home, monitor something, make an expert system, mass possibilities. The graph below was obtained using matplotlib, accumulated arrays of values ​​and plot'm drawn. At the very beginning one can see how the temperature and humidity have jumped, pressure and altitude within the measurement error. When the impact was removed, the data gradually began to return to their original states. Fig. 4 Visualizing data from a 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 the temperature, humidity and all other characteristics of the external environment and, depending on what is happening at the current time with the environment, react according to the program. For example, to call the fire brigade when the temperature rises above the 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, and how much pleasure from the process of receiving data from the real world, you do not need to solder anything, you do not need to write code for the microcontroller, although I don’t hide there is something special in it.
For the gcc c compiler for Linux, a small library was written to work with the device, which is based on the 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 (tests on Windows were not done). Unfortunately, to get the port number to which the board is connected in automatic mode has not yet succeeded, unlike the Python library, here the port is set by hands, and the stub function for defining the port simply gives out a constant. To get data from a device, you need to connect libraries, get a port number (set by hand) and use functions.
If it is necessary to visualize the data, then it seemed to me to be fast enough as follows: 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 schedule, here at first I breathe on the DHT11 sensor, and then I blow on it with a hairdryer and dry the air, along the y axis there is a percentage of humidity, x reports are simple, they can be done at any frequency. Fig. 5 Data Visualization with gnuplot
3) Path of the Jedi, or choose the tool yourself
Since working with a COM port is a rather ancient technology (but 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 packet according to the table, send it to the port to which the board is connected, wait about 10 ms, and then receive the packet 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 quite wide: you can write an expert system that will collect data about the real external world and make predictions, for example, of the weather, in this case. You can also use this system for monitoring in a production facility. Of course, some sensors have not so hot which parameters (DHT11, 5% humidity error), but in most cases this is enough to signal in time that “the roof is out of stock in the warehouse” and puddles around. You can send data to some remote server, for example, in the project “people's monitoring.” With the help of 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. Opportunities mass, it all depends on the imagination of the developer. I am very glad that we managed to make such a cool device, now, thanks to its capabilities, we will be able to solve our tasks more efficiently and with less time.