📜 ⬆️ ⬇️

Thermometer on Raspberry pi with wireless sensor on rf 433 and MK attiny85

This article describes my experience in creating a wireless temperature sensor based on the Attiny85 + ds18b20 + rf 433 TX battery-powered MK. Data reception and programming Attiny85 is made on the basis of Raspberry pi B +.

Foreword


On Habré and not only there is a lot of interesting about the Raspberry Pi mini-computer. I decided to get such a toy in my house. Please do not criticize the choice of this particular platform. The choice fell only because of fairly extensive documentation and many different examples of the use of this mini-computer.

It was decided to start with temperature sensors. At hand turned a couple of sensors (ds18b20, ds1822). The abundance of articles on the connection of these sensors did not take much time in order to attach them and get the temperature values. I had to purchase a DHT22 sensor, which allowed me to record not only the temperature, but also the humidity. On raspberry was raised lighttpd + php + mysql. The data from the sensors now with a certain frequency were recorded in the database. From the article "Internet thermometer or telemetry of a country house" was taken the idea of ​​constructing graphs in Highcharts . Not possessing special programming skills, I had to spend a couple of free evenings. As a result, it was possible to go to the local page and look at the current temperature outside, in the apartment, as well as the dynamics of temperature change over the last n days.

Some time outside the window hung two sensors DHT22 and ds18b20. The temperature readings of these sensors usually differed by a few tenths. The goal was not to make measurements with very high accuracy, and such differences in the testimony suited me perfectly.
')
All in the same article links to openweathermap.com and narodmon.ru projects were spied . Thus, data from the temperature and pressure sensor (DHT22) began to be sent to these two services.

I did not really understand the Openweathermap service. I liked people's monitoring more. On the map, you can visually compare your readings with data from other sensors in your own city. There are applications for phones on Android and iOS.
Everything, of course, is wonderful, but the problems as always lie in the details. The windows of my apartment are facing south. And sensors often end up in the sun. When the temperature is below zero, sensors on the sunny side could show up to +10. At the same time, the sensors are not in direct sunlight (hidden behind the air conditioner unit). Pull the wires through the corridor to the north side is not much wanted. Therefore, it was decided to develop wireless technologies.

Formulation of the problem


RF 433 was chosen for reception and transmission. Again, since I am new to the world of electronics and programming, I decided not to spend a lot of money and leave my choice on the most budget option. The cost of the receiver + transmitter is about $ 3.

Work scheme: MK - rf 433 (transmitter) -> rf 433 (receiver) - Raspberry pi.

It was necessary to choose a microcontroller. Instructables has a great article on how to program an Attiny85 microcontroller with raspberry pi. Translation of this article or something like Habré did not meet. So if interest is shown, a transfer can be made.

Microcontroller selection was made. An experienced pair was purchased on the market at a price of $ 2-3 apiece. For simplicity, decided to start with ds18b20.

The algorithm works as follows: MK reads the temperature value from the sensor, reads the voltage (we will power it from the batteries), checksum is calculated, this data is sent several times, and then we send the whole system to sleep for a few minutes to save power.

rf 433 (receiver) + Raspberry pi listens to the broadcast while waiting for data.

Task performance


Assembly and connection


On the breadboard board, we assemble a circuit for programming the MC:



Connection diagram taken from the Instructables site. Resistors can take a little other 1-5Kom, which is at hand. More than 4.7 kom I personally did not try. In the same place, on the breadboard board, we connect the sensor and the transmitter to the Attini as shown in the diagram:



Thus, we got a connected MK for programming using the Raspberry Pi and a connected sensor and RF433 transmitter. It remains to connect the receiver to rasspbery pi. My connection diagram:

Raspi RF433 RX
GPIO 27 (13) -> DATA
GND (14) -> GND
5V, Vcc (4) -> Vcc

Pins on raspberry pi connection schemes according to their numbering in the WiringPI library for working with GPIO, their physical location on the board in brackets.

And this is how it all looks on the breadboard:



With iron sorted out, let's go to the software part.

Install the necessary software and program Attiny85


How to install Raspbian will not be considered. In raspi-config we include SPI.



Now we need to download and install AVRDude (AVR Downloader-Uploader) - a cross-platform free console program designed for flashing Atmel microcontrollers from the AVR series.

sudo apt-get install bison automake autoconf flex git gcc sudo apt-get install gcc-avr binutils-avr avr-libc git clone https://github.com/kcuzner/avrdude cd avrdude/avrdude ./bootstrap && ./configure && sudo make install 

To work with GPIO, you need to download and install WiringPi:

 cd ~ git clone git://git.drogon.net/wiringPi cd wiringPi ./build 

Check whether we did everything right:

 sudo gpio -g mode 22 out sudo gpio -g write 22 0 sudo avrdude -p t85 -P /dev/spidev0.0 -c linuxspi -b 10000 sudo gpio -g write 22 1 

If we did everything right, we should get this result:



At this stage, the preparatory work is over.

I did not manage to find a working C code that would perform all the tasks. I had to collect pieces from different repositories (and not only).

To read the temperature used this repository . To determine the voltage used this repository .
To send a signal used repository .

What I got laid out on the githab. The code we need is blinky.c , 1wire.c , 1wire.h , manchester.c , manchester.h .

It remains to collect everything and program our Attinku 85.

Create or copy a Makefile in the same place where our project files are located.

We carry out:

 make 

And if everything is good and there will be no errors in the program code, we execute:

 make install 

We relaxed and watched the recording of our program on the microcontroller:



If everything is good and nothing has prevented us, at this stage of programming our MC is completed.

According to the scheme above, we collect everything on a separate development board already without a Rasspberry Pi and is powered by batteries. I had a case for three AA batteries. This is my first project and I did not strain too much with the layout on the board. This is what I ended up with:



Now it's time to receive the temperature data from the transmitter.

There is a good article about decoding X10 RF protocol. There are examples of programs for receiving the RF433 signal. For the receiver, the code from this article, namely X10RFSnifferBit.cpp, was taken as the basis . The program sits and waits for a signal of a certain duration, or rather, several consecutive signals, the so-called lock. Then begins the reception of the required number of bits.

We modify the lock for our values, add a checksum check and write the values ​​to a file. The resulting information is written to the file in the form of sensor id, battery charge, temperature and checksum.
This is my rf433recieve.cpp , and this is an example of a file to which we write the received data.

In my case, the RF 433 RX connected to GPIO 2 (13). When connecting to another pin, you need to change the GPIO number in the line:

 wiringPiISR(2, INT_EDGE_BOTH, &handleInterrupt); 

We collect and run our program ( Makefile ). You need to run as root:

 make sudo ./rf433recieve 

Now on raspberry pi we take our temperature and write it to the received data in a file.

Conclusion


While all this was collected and programmed, the data from the local database was transferred to the hosting. On the crown, I jerk the file where we write our data on temperature, battery charge and send the data to the hosting in the database. Then plotted over the past few days. This is the result:



When everything was assembled, it was necessary to check the data transmission distance at home with different antennas.
At the receiver, the antenna is a piece from a twisted pair about 17 cm long. For the transmitter I tried several variations: twisted pair 17 and 34 cm; copper conductor is about 1 mm in diameter and 34 cm long in the form of a straight line and in the form of a spiral. Worst of all in my experiments was a copper conductor in the form of a spiral, the remaining antennas gave approximately the same version, so I decided to leave a piece of twisted pair 17 cm. For a distance I managed to break through three brick walls and a distance of about 10 m. The increase in partitions and distances led to that the receiver did not receive the signal at all or tried to catch one two signals from the lock-a.

Now there is a period of testing and put the sensor on the balcony (also on the sunny side). In the open balcony, the sensor was not hidden from the sun, and the balcony was obviously worse ventilated than the sensor hidden behind the air conditioner body in the shade. Therefore, on sunny days, the wireless sensor gives out peaks and solar peaks can be monitored using these peaks.

What's next? Then I wanted to try a bunch of MK Attiny85 + RF 433 TX + DHT22, but that's another story ...

Source: https://habr.com/ru/post/252381/


All Articles