Introduction: ESP8266 Weather Station With Arduino – #2 Software
This instructable is for the software of my Weater Station Project. Please read it first to understand all explanations.
Because of the both MCUs the software concept is based on two parts: The ESP8266 runs on Nodemcu and does the Internet connection and thingspeak.com related parts. The Arduino does all the sensor related things. The communication between Arduino and ESP8266 is via serial connection whereby the Arduino uses the SoftwareSerial Library on pins D2 (RX) and D3 (TX).
The configuration is divided into the both units:
- All wifi and thingspeak.com relevant parameters are stored in the ESP8266 in a config file.
- All sensor and measuring related parameter are set in the Arduino.
Why? I build up a lot of different units which only differ in the thingspeak.com and wifi parameters. And I build a neat programmer (image) for the ESP01. So I only have to save the new config file to the ESP01 and keep all other things same. And can easily change the thingspeak channel by changing only the ESP01 config.
There's two modes in the Arduino software: standard and low power. The standard version powering the ESP01 once and sends data regularly (short periods) to thingspeak.com. The low power one sends the date also regularly but within longer periods. Between two sendings the ESP01 is powered down. Why using the standard version and not always save power? If you want a more reliable, equidistant time stamp on thingspeak.com reconnection to wifi takes too long and is not equal from one reconnection to the other. And if you don't run the system on battery, power saving is not the most important aspect.
Step 1: The ESP01 Software
If not already done, flash the Nodemcu firmware to the ESP01. For flashing see this other ESP01 project (link).
Extract all file from the the attached archive. To upload and communicate with the ESP01 I use ESPlorer.
- First, change the Wifi and thingspeak.com parameters in config.lua to yours:
wifiStation = "yourssid" wifiPW = "yourwifipw" devName = "name_of_your_device" thingsSpeakKey = "ts_write_key" tsChannel = "ts_channel_nb"
- Save localServer.lua, tempArduino.lua, wifiConfig.lua and init_real.luaand compile them. After compiling you can delete the *.lua files.
- Save the config.lua and init.lua to the ESP01. Do not compile these files.
- Restart the device.
- Test the connection with ESPlorer: Wait until Wifi settled down and send sendData() via serial to the ESP01. This should do an update of the thingspeak.com channel. If no positive return check your config.
The both LEDs show the status of the ESP01:
- RED on: ESP01 is running, looks for Wifi connection
- GREEN on: Wifi connected
- RED blinking: No Wifi connection found. Module uses AP mode:
- SSID: ESP+ DeviceID
- PW: DeviceID
- IP address: 192.168.1.1
If red and green are reversed reverse the LEDs in the header.
Step 2: The Arduino Software
The Arduino is the measuring device. It reads all sensor values and calculates the average over a specific time period (moving or rolling average).It also sets the ESP01 to sleep and wake it up again.
There are few things to setup in the header individually. First the working mode:
// ------------- mode setup ------------------
#define lowPowerMode 1 // lowPower = 1, standard = 0
Second different times:
// ------------- time setup ------------------
#define wifiSendPeriod 300000 // send data every x ms to thingspeak
#define meanPeriod 300000 // calculate the average of the sensor values of the last x ms
#define avAnzDef 60 // number of values within the meanPeriod for averaging
And also the data of your thermistor:
// ------------- thermistor setup ------------------
float thermr = 10000; // reference resistance of the thermistor
int beta = 3950; // beta value
float t0 = 298.15; // reference temperature of the thermistor
float pad = 10000; // balance/pad resistor value
Step 3: Communication Between ESP8266 and Arduino
- A→E: Send command from Arduino to ESP01
- A←E: Answer from ESP01 to Arduino
- E→A: Send status from ESP01 to Arduino
E←A: Answer from Arduino to ESP01
Send data to thingspeak.com:
D0 // not successful
D1 // successfully send to thingspeak.com
Update values on the ESP01:
field01=<value> // temperature from thermistor
field02=<value> // temperature from DHT11
field03=<value> // humidity
field04=<value> // illuminance
field05=<value> // resistance of the thermistoir
field06=<value> // temperature from BMP180
field07=<value> // pressure from BMP180
WS1 // Wifi on and connected
Step 4: Stability
To see the stability of the system here's the link to a chart from my thingspeak.com account showing the temperature and the illuminance somewhere in Germany:
Actually it shows a one week overview. The system runs since mid of August without a break.
So, if there are any questions ask them in the comments.
And for further instructables follow me.
2 People Made This Project!
We have a be nice policy.
Please be positive and constructive.