In my previous post about Weather Station project ESP8266 + DHT11 weather station I described how the device work and how to communicate with it.
Since that time I’ve get some feedback how it works and I figure out two major problems:
- epoch time – sometimes when library response for getting current epoch time from server it returns unix timestamp from the future (year 2036), which cause that my device stop records data until that day comes; only solution in such case was manually restart device – not good if it happens in night (each time I lost a lot of data)
- power save – when it is always connected to power through old phone charge it use power all the time; I think that in future it could be powered from battery (but it is still future)
These above problems caused that I started to think how to change my devices to make them more power save and more reliable. I discovered that this kind of board – ESP8266 Nodemcu v3 has something which is called deep sleep. It is mechanism which allow you to turn off almost everything besides internal clock. It of course allow to use less power but … disable to run HTTP server on device.
I count all pros and cons such solution and I discovered that it could also resolve my first problem with epoch time. I also saw that I didn’t use all functionality of the HTTP server – I didn’t look on the page which shows current values of the sensor. So It means that I can resign from that functionality.
In such case I decided to make change in how the device work. The main concept is:
- start device
- connect to WiFi
- get epoch time
- read data from log file and store it in list
- read data from sensor and add it to list
- try to send oldest 15 data records from list to server
- mark data as synced based on server response
- save not sync records in log file
- go deep sleep
- wake up and start again
Looks simple but I spend a lot of hours to write proper code and to test it, but after few days my program was ready and I can present it to all of you – look on github.
This time I will not describe all the project, because the main concept I presented above, I will focus only on necessary changes and communication with server.
Since version 1.0.0 in configuration there is few changes:
- PAGE_TITLE – this was removed
There are two additional parameters that have to be set.
- server address – you need to set IP or domain name of your server
const String host = “http://SERVER_ADRESS”;
- URL of your API
const String url = “/api/ws/sync”;
Communication with server
Communication with server is based on JSON. When the device sending data to server it has such format:
- ip – string that represent current device IP, so this is very important to assign static IP for each device
- data – list of records to sync
- time – unix timestamp in UTC
- temp – temperature value as string
- hum – humidity value as string
In response server should return list of pairs:
- time – unix timestamp in UTC wrom request
- sync – boolean value which means if record was saved or not
Each record which is mark as synced, will not be saved in log file of the device and will not be send to the server more time.
Epoch time and log time limit problem
Epoch time problem still exists, but it not stop the device. Such result is send to server but the server is protected for wrong data. Currently it mark such wrong data in response as sync and it is not stored in log file.
Log time limit is 150 records (single file and flash limit, I assume that one device can have to three sensors, two is more realistic: one inside and one outside, but this is still future). I expect that someone will try to make max 4 check device sensor in each hour so we can store records from last 37 hours, if we change the time to measure to 2 times per hour than we are able to store data from last 74 hours (it is more than last 3 days). I think that it is enough.
New version of that code is available on my github. It has worked for me since few days and now I don’t have gaps in my stored data (I use measure 4 times in hour, so probability that all 4 records from one hour has wrong timestamp – from future – is equal 0).
If you have any suggestions, any doubts or some new ideas and concepts how to make this project more valuable do not hesitate to contact me. You can write email or send a comment below.