This all started with a christmas gift to my brother. He wanted a weather station but did not want the ugly LCD wall panel associated with them.
I started searching for a web based weather station, but they were totally off my budget. So I started investigating if I could build it instead.
I had an old Raspberry Pi 1 rev B lying around to be used as a base. The idea was to use this as a web server connected by wire to a wifi-router. The sensor end of the system was one BMP180 break out board containing a temperature sensor and a barometer connected to the RPi by I2C.
The weather station would not be very much worth without remote sensors. First I looked into building all the sensors from scratch, but I soon realized that it would also become to expensive and I would not get the size down. After some googling I found out that most likely all budget weather stations use simple On-Off-Key (OOK) modulation on 433 MHz, which possibly could be hacked. So Ebay also supplied me with a 433 MHz superhetorodyne receiver with AGC. Since the RPi is not deterministic I chose to use an Arduino-like MCU, an Pro Micro, as decoder of the received signals. (If needed or not, I do not know. At least it works. :-) ) The Pro Micro also was connected to the RPi via I2C.
Since the receiver was operating on 5V I needed a 5V version of the Pro Micro, so I needed to add a bidirectional logic level converter in between the 3.3V RPi and the Pro Micro.
Remote sensors:
I found a cheap (if not the cheapest) remote temperature and humidity sensor on Ebay. For reference please see the image. I guess that these have not a very long life before they get End-Of-Life, but I guess most off them will work in the same maner.
Reverse engineering the sensor protocol:
I needed to figure out the protocol on which the remote sensors sent its information. I knew that the remote sensors was working on the 433 MHz band. Digging around the net it seemed like there were a couple of similarities between the different sensors:
- On-Off-Keying (OOK)
- Repetition of the message each transmission.
- Each message starting with a preamble.
- An ID which changes on power on. (I.e. battery change)
The sensor also revealed that it had a TX button, Celsius/Fahrenheit button and a three position "channel" selector.
The receiver and the Pro Micro was set up on a breadboard and connected via USB to my computer. I programmed an Arduino program that just sampled the incoming signal from the receiver to find out the characteristics of the signal. I found that the signal indeed seemed to be an OOK-signal. I then changed the program to react on the slopes of the OOK-signal and send the microsecond timestamp and the signal level over the USB to the Arduino IDE serial monitor. I then took the data and started analyzing it in Octave. The first graph were not that encouraging, see below, but there were something there at least. I then realized that all the other noise was a result of the AGC in the receiver.
So instead of plotting the signal level, which in itself is not that interesting, I plotted the time between the two samples, i.e. time between any change of the signal from high/low.
On the net I found several encodings for the bit stream. So what to do? The sensor was cheap and so probably the coding as well. I guessed that there were no fancy coding schemes. So I assumed that one bit was actually straight forward only represented itself. The bit error was only handled via the repetition of the messages.
I guessed that a time difference between 1.5-2 milliseconds encoded a 0 and a difference of 2-4 milliseconds encoded a 1. A time difference above 4 milliseconds is decoded as end-of-message.
Next task was to decode the information in the bitstream. I modified the Arduino program to directly translate the messages to bit streams. Each message contained 36 bits.
I started to record a lot of messages with as many combinations as possible. Making notes if I had removed the battery in between two recordings, pressing the TX-button, noting the temperature and humidity shown on the sensors local display, the position of the "channel"-selector and toggling between the Celsius/Fahrenheit setting. Thanks to this I could isolate all the different fields in the message.
Temp 31.3C, Humidity 34%, Auto transmission, Channel 1:
010110011110100000010011100100100010
Temp 31.3C, Humidity 34%, TX-button triggered, Channel 1:
010110011110110000010011100100100010
Temp 31.4C, Humidity 34%, TX-button triggered, Channel 2:
010110011110110100010011101000100010
Temp 31.4C, Humidity 34%, Auto transmission, Channel 2:
010110011110100100010011101000100010
Temp 31.5C, Humidity 34%, TX-button triggered, Channel 3:
010110011110111000010011101100100010
Temp 31.1C, Humidity 34%, Auto transmission, Channel 3:
010110011110101000010011011100100010
After battery change:
Temp 29.2C, Humidity 40%, TX-button triggered, Channel 3:
010111011111111000010010010000101000
Temp 29.7C, Humidity 34%, Auto transmission, Channel 3:
010111011111101000010010100100101000
From these I could conclude that the message that it was to be divided into:
0101 110111111 0 10 000100101001 00101000
Message fields:
I could also conclude that the Celsius/Fahrenheit-button only toggled the representation on the local LCD display of the sensor. In the message only Celsius is transmitted.
Now we clearly can see the two transmissions in the sampled data. Focusing on the last transmission, see below, it seemed like there were three different sizes of gaps or peaks. I knew from the information search that these sensors often repeated the message, so I guessed that the larger time difference was a gap between the messages, so I found that the message was repeated 7 times.
Plotting only the dots of the samples clearly shows the two different levels.
I guessed that a time difference between 1.5-2 milliseconds encoded a 0 and a difference of 2-4 milliseconds encoded a 1. A time difference above 4 milliseconds is decoded as end-of-message.
Next task was to decode the information in the bitstream. I modified the Arduino program to directly translate the messages to bit streams. Each message contained 36 bits.
I started to record a lot of messages with as many combinations as possible. Making notes if I had removed the battery in between two recordings, pressing the TX-button, noting the temperature and humidity shown on the sensors local display, the position of the "channel"-selector and toggling between the Celsius/Fahrenheit setting. Thanks to this I could isolate all the different fields in the message.
Temp 31.3C, Humidity 34%, Auto transmission, Channel 1:
010110011110100000010011100100100010
Temp 31.3C, Humidity 34%, TX-button triggered, Channel 1:
010110011110110000010011100100100010
Temp 31.4C, Humidity 34%, TX-button triggered, Channel 2:
010110011110110100010011101000100010
Temp 31.4C, Humidity 34%, Auto transmission, Channel 2:
010110011110100100010011101000100010
Temp 31.5C, Humidity 34%, TX-button triggered, Channel 3:
010110011110111000010011101100100010
Temp 31.1C, Humidity 34%, Auto transmission, Channel 3:
010110011110101000010011011100100010
After battery change:
Temp 29.2C, Humidity 40%, TX-button triggered, Channel 3:
010111011111111000010010010000101000
Temp 29.7C, Humidity 34%, Auto transmission, Channel 3:
010111011111101000010010100100101000
From these I could conclude that the message that it was to be divided into:
0101 110111111 0 10 000100101001 00101000
Message fields:
- Bit 1-4: Preamble.
- Fixed to 0101
- Bit 5-13: Sensor ID.
- Changes after battery change.
- Bit 14: TX-button triggered transmission.
- 0 = Automatic transmission
- 1 = TX-button pressed
- Bit 15-16: Channel selector.
- 00 = Channel 1
- 01 = Channel 2
- 10 = Channel 3
- Bit 17-28: Temperature in Celsius multiplied by 10.
- Bit 17 = sign bit
- Bit 29-36: Humidity
- Unsigned
Final assembly:
SW design:
I started setting up a ordinary LAMP stack, with Raspbian, Apache, MySQL, PHP and Python.
Python was used to interact with the base station and to handle the raw data. PHP the server side scripting, Ordinary HTML and Javascript for the client. I chose to use Bootstrap and the Bootswatch theme Flatly as design template. For drawing graphs I used D3.js.
From a user perspective I wanted the index page to show an overview of the current value of the different sensors. From that page I wanted to got to a detailed view of the historical data from a sensor.
Since the sensors change ID every time you take out a battery there was also a need for a function that associates the ID with the previous ID. Also since the sensors sends on the same frequency and with a non-distinguishable protocol (two sensors can send at the same time and the information only is received as nonsense) a function that helps the user to find a good timing between the sensors was programmed.
When it comes to data storage the idea was to save raw/non-aggregated data up to a month. After a month it would be aggregated to become a part of a data point in separate databases containing aggregated data for hours, days, months, years. The raw data that was older than one months was deleted regularly.
Bad, bad performance:
I do not know if I set things up incorrect, but when everything was working and as system collected data (each sensor sends data with an interval of 34-35 seconds) I noticed that my graphs were getting slow, really slow! Becoming slower as the data poured in. The non-aggregated data for a month took up to 250 seconds to load into a graph. (If the web server did not time out before.)
So after reading a lot of threads on stack overflow I guessed it "had to be" a problem with Apache taking to much memory. So I started by changing the Apache to Nginx. Didn't help. Still believing the web server was the problem I changed into Lighttpd. Didn't help.
After adding timers to my code I realized it was the data storage that was the problem. Blaming PHP for being inefficient I coded a C++ program that interfaced the MySQL database. Better but still ridiculously slow. (This took some time to code, so imaging my disappointment.)
Also the shear amount of data points was a killer for the javascript making the graphs. (Between each datapoint there were a drawn object in D3, resulting in approx. 100000 objects in total for a month of saved data.) I figured out that there were no point in sending all the data to the client since the only thing that was performed by the client was the displaying of a 200-300 pixel wide diagram. I.e. It would be good to aggregate the data before it was sent to the client.
From general purpose to very specific:
I solved the issue by coding a C++ program which handles the time series of data in a custom double linked lists and saving each data series, e.g. Sensor 1 temperature and Sensor 1 humidity, in separate data files and thereby decreases the amount of data that needs to be handled. The program also aggregated and saved data points for other timebases (e.g. Added a raw data point to this weeks average).
The output functionality of the program included; getting custom time periods, aggregating to a certain number of output data points, output in readable format or output in JSON format.
The backbone of this program was a double linked list structur with nodes containing a data point object with the following fields:
- Time stamp
- Value/Average. (Average if aggregated node)
- Min value.
- Max value.
Result:
Well, it works. Some things could be done better. I am not very proud of the code. After all tinkering it is pretty hard to read and understand how it works. I really need to comment my code better.
I have attached some screen shots of the finished weather station below. (At the moment only one remote sensor have batteries. They ran out during the testing phase).
This is the first page, the dashboard. It should provide a quick overview of all sensors.
On the screen shot above the maximum resolution is shown. The data is only aggregated to have one data point per pixel in the graph. The greyed area displays max and min values for the data point. If one wants to see larger patterns it is possible to aggregate into hours, days, months, years. Below is aggregated to hours.
Next page is the settings page. The first page provides an overview of the different sensors.
Clicking on "Redigera" (I.e. "Edit") opens a modal which allows change of name and description. It also have a button to identify the serial of the sensor as this is used as the discriminator between the sensors.
The other button, "Timing", opens another modal devised to set the timing between the remote sensors. The reason for this is that if the remote sensors sends information at the same time it will result in a collision, bit errors and the system would reject the information. The green lines move across the scale and shows the two next transmission of the sensor if the sensor is triggered by the TX-button. Pressing the TX-button will set the coming intervals of the sensor. The orange shows the last transmission and the next transmission from this sensor. The greyed are inactive sensors. (Inactive is equal to that they have not sent a transmission in 10 minutes). If there would have been active sensors their next transmissions would show up as dark blue lines. The idea is to look at the green lines and press the TX-button when they are in a suitable position as far away from the other lines as possible.
Also thanks to Bootstrap, here are some screen shots from my cellphone. On the cellphone it is not possible to view the graph. It is also not possible to access the settings page. (It was not possible to image the complete page.)
The last lesson:
As I was writing this article I was going to get the screenshots and I got no response from the web server. It happened to be the SD-card in the RPi that had corrupted. Fun! Fortunately I had the main code saved, but not the full image of the system. It took me a good 10 hours to set it up again. I will start saving the system images from now on.














Inga kommentarer:
Skicka en kommentar