In this project we are going to establish a wireless communication between two ESPs and send data from three sensors to an Excel spreadsheet. This tutorial shows a wireless weather station with data logging that you can implement in your home.
Before you continue reading this project, please complete the following tutorials:
- How to get started with the ESP8266
- How to make two ESP8266 talk
- How to flash your ESP8266 with NodeMCU
If you like the ESP WiFi module and you want to build more projects you can download my eBook called “Home Automation using ESP8266” here. Let’s get started!
Summary
Here’s a Figure that describes exactly how everything works together:
Parts List
Here’s the hardware that you need to make the weather station:
- Recommended: 2x ESP-12E read Best ESP8266 Wi-Fi Development Boards
- Alternative: 2x ESP-201 + 1x FTDI Programmer
- 1x DS18B20
- 1x Breadboard
- 3x Pushbutton
- 3x 10k Ohm Resistor
- 1x 4700 Ohm Resistor
- 1x 10k Ohm Potentiometer
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
DS18B20 – One Wire Digital Temperature Sensor
In this project, we will be using the DS18B20 one wire digital temperature sensor. Now, before we get to the programming part, lets learn how to wire up our temperature sensor.
The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to GND and the VDD pin to 3.3V from the ESP8266.
Then connect the DQ pin to IO04 on the ESP8266. A 4K7 ohm pullup resistor is required on the DQ pin to pull it up to 3.3V.
Reading ADC Value
We’re going to use the ADC pin to read an analog value and when referring to the ESP ADC pin you will often hear these different terms interchangeably:
- ADC (Analog-to-digital Converter)
- TOUT
- Pin6
- A0
- Analog Pin 0
All these terms refer to the same pin in the ESP8266 that is highlighted in the next section (read this article for more information on the ADC pin).
Currently, TOUT (Pin6) has a 10-bit precision and its input voltage range is 0 to 1.0 V when TOUT is connected to an external circuit.
Accessing the ESP8266 Analog Pin
With the ESP-201 is very easy to access the ADC, you simply connect a jumper wire to the pin highlighted in the Figure below.
Flashing Boths ESPs with NodeMCU
We are going to use the NodeMCU firmware, so you have to flash your ESPs with NodeMCU firmare.
Downloading ESPlorer IDE
I recommend using the ESPlorer IDE which is a program created by 4refr0nt to upload scripts to your ESP8266.
Follow these instructions to download and install ESPlorer IDE:
1) Click here to download ESPlorer
2) Unzip that folder
3) Go to the main folder
4) Run ESPlorer.jar
5) Open the ESPlorer
Uploading a Script to Your ESP8266 (3.3V FTDI Programmer)
The schematics to upload scripts to an ESP are very straight forward. You only need to establish a serial communication between your FTDI programmer and your ESP8266 (repeat the schematics below for the Client and Server).
Here’s the connections:
- VCC – 3.3V
- GND – GND
- TX – RX
- RX – TX
- GPIO 0 – 3.3V
- GPIO 15 – GND
Writing Your Client Scripts
There are three scripts that you need to upload to your ESP8266 client:
Start by uploading the following script and name it counterMax.lua. It is just a numerical number 1 that does some counting and helps reset your board if it looses connection with the ESP server.
1
When you upload the counterMax.lua script it is expected to print the following error:
Upload the script below to read the temperature in ºC or upload this script to read temperature in ºF. Make sure you save the file to your ESP with the name ds18b20.lua.
--------------------------------------------------------------------------------
-- DS18B20 one wire module for NODEMCU
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <[email protected]>
-- Dramatic simplification: Peter Scargill
--------------------------------------------------------------------------------
-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
-- Table module
local table = table
-- String module
local string = string
-- One wire module
local ow = ow
-- Timer module
local tmr = tmr
-- Limited to local environment
setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation – you don’t get any shorter than this
--------------------------------------------------------------------------------
function readNumber(pin)
ow.setup(pin)
ow.reset(pin)
ow.write(pin, 0xCC, 1)
ow.write(pin, 0xBE, 1)
data = nil
data = ""
for i = 1, 2 do
data = data .. string.char(ow.read(pin))
end
t = (data:byte(1) + data:byte(2) * 256) / 16
if (t>100) then
t=t-4096
end
ow.reset(pin)
ow.write(pin,0xcc,1)
ow.write(pin, 0x44,1)
return t
end
-- Return module table
return M
Finally upload the main script to your ESP and name it init.lua.
-- Yves Arbour wireless weather station project with data logging to Excel sheets with Things Gateway
-- code based on the following
-- Rui Santos - ESP8266 Client
-- DS18B20 one wire module for NODEMCU
-- Vowstar <[email protected]>
-- Peter Scargill
-- Wimp Weather Station by sparkfun Nathan Seidle
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678")
wifi.sta.connect()
print("Looking for a connection")
temp=0
function readds18b20sensor()
t=require("ds18b20.lua")
-- print(t.readNumber(2)) for debuging
temp = (t.readNumber(2)) --GPIO 04
end
winDirection=0
gpio.mode(0,gpio.INPUT)
function readWindDirection ()
windDirection = adc.read(0)
if (windDirection < 26) then windDirection=90 --E
--if (windDirection < 17) then windDirection=113 -- ESE
--elseif (windDirection < 21) then windDirection=68 -- ENE
--elseif (windDirection < 26) then windDirection=90 -- E
--elseif (windDirection < 36) then windDirection=158 -- SSE
elseif (windDirection < 43) then windDirection=135 --SE
--elseif (windDirection < 60) then windDirection=203 -- SSW
elseif (windDirection < 85) then windDirection=180 --S
--elseif (windDirection < 98) then windDirection=23 -- NNE
elseif (windDirection < 120) then windDirection=45 --NE
--elseif (windDirection < 170) then windDirection=248 -- WSW
elseif (windDirection < 210) then windDirection=225 -SW
--elseif (windDirection < 275) then windDirection=338 -- NNW
elseif (windDirection < 380) then windDirection=0 --N
--elseif (windDirection < 500) then windDirection=293 -- WNW
elseif (windDirection < 750) then windDirection=315 --NW
elseif (windDirection < 1005) then windDirection=270 --W
else windDirection=-1
end
end
irqPin = 1 --GPIO5
windSpeed = 0
windClicks = 0
lastWindCheck = 0
function debounce (func)
local delay = 100000
return function (...)
now = tmr.now()
now = now - lastWindCheck
if now < delay then return end
lastWindCheck = tmr.now()
return func (...)
end
end
function windSpeedIrq ()
windClicks = windClicks+1 * 10000000
print (windClicks)
end
function calcWindSpeed ()
local deltaTime = tmr.now () - lastWindCheck
windSpeed = windClicks / deltaTime
print(deltaTime)
windSpeed = windSpeed * 1492 /10000 -- Replace " *1492/10000 " with " *24/100 " for Km/h instead of Mp/h
windClicks = 0
lastWindCheck = tmr.now()
end
gpio.mode(irqPin,gpio.INT)
gpio.trig(irqPin,"down",debounce(windSpeedIrq))
file.open("counterMax.lua","r")
counter=(file.read())
file.close()
tmr.alarm(0, 2000, 1, function()
if(wifi.sta.getip()~=nil) then
tmr.stop(0)
print("Client IP Address:",wifi.sta.getip())
cl=net.createConnection(net.TCP, 0)
cl:connect(80,"192.168.4.1")
tmr.alarm(1, 5000, 1, function()
counter=counter+1
readds18b20sensor()
readWindDirection()
calcWindSpeed()
print (temp)
print(windSpeed)
-- Next is the properly formated string that Things Gateway needs to show Date,Time,Wind speed,Wind direction and Temp.
cl:send("XLS,write,Example,A"..counter..",%date%\nXLS,write,Example,B"..counter..",%time%\nXLS,write,Example,C"..counter..","..windSpeed.."\nXLS,write,Example,D"..counter..","..windDirection.."\nXLS,write,Example,E"..counter..","..temp)
cl:on("disconnection",function(reconnect)
file.open("counterMax.lua","w+")
file.writeline(counter)
file.close()
end)
end)
else
print("Connecting...")
end
end)
function reconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678")
wifi.sta.connect()
end
Client Circuit
Follow this circuit to build your ESP client and if you are using the ESP-12, you can view the schematics here:
The right switch connected to IO05 is used to simulate wind speed. The program currently doesn’t give a real-time wind speed, but a last 5 seconds wind speed average.
So it doesn’t mater how fast you press right switch. If you push right switch 5 times in the 5 seconds sampling period… that translates into once per second average giving 1.492 MPH or 2.4 KMH (at this point we’re using NodeMCU integer version, so you’ll get 1492 MPH and 24 KMH. You’ll have to divide in the spreadsheet by 100 and 10 respectively).
Writing Your Server Script
Upload the following script to your ESP and name it init.lua.
-- Rui Santos - ESP8266 Server
-- Modified by Yves Arbour to ennable print string to go directly to an Excel sheet with Things Gateway
print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())
sv = net.createServer(net.TCP)
sv:listen(80, function(conn)
conn:on("receive", function(conn, receivedData)
--print("Received Data"..receivedData)
print(receivedData)-- string "Received Data" removed...
--Things Gateway ignores strings that do not start with proper command...
--XLS in this case for Excel sheet
end)
conn:on("sent", function(conn)
collectgarbage()
end)
end)
Final Server Circuit
Follow this circuit to make your ESP server and if you are using the ESP-12, you can view the schematics here.
We’ve created a custom PCB for the ESP-201. Here’s the details:
- Yellow wire: can either connect GPIO 0 to GND to flash NodeMCU firmware or to VCC to save scripts to ESP
- GPIO15 is hard wired to GND
- Reset button and 10K Ohm pull-up resistor to RST pin (in order to reset the board the RST pin has to be pulled down and then has to go back up)
Putting Everything Together
Power both your ESPs and make sure the ESP server still has the serial communication established with your computer, because that’s how you are going to post the data to your Excel spreadsheet.
Downloading and Installing Things Gateway
For this project we are going to use Things Gateway a software by Roberto Valgolio to write data on an Excel spreadsheet and display a self updating real time charts.
Things Gateway is a PC application that connects a microcontroller and makes it able to:
- Get data from Excel files
- Write Excel files
- Write CSV log files
- Send emails (when certain conditions are met)
- Show values and charts (charts are Excel independent)
Things Gateway can also connect a GPS and show on the screen:
- Speed, heading, altitude and other navigation info
- Real-time tracks in Google Maps
Go to the Things Gateway website, download the program and install it. This project was tested with the Beta4 version.
Launching Your Things Gateway Application
To launch the Things Gateway application is very straight forward. Go to Your PC > Documents > ThingsGateway and open the application.
Then follow these instructions:
- Go to the Config tab
- Select the Serial COM Port of your ESP8266 Server
- Set the Serial port Speed to 9600
Demonstration
Open the RealTime tab and select From Serial to Excel.
An Excel spreadsheet should appear and your data should start appearing there every few seconds!
Your data is being stored in the example Excel spreadsheet in that folder:
Future Improvements
In this tutorial we established a basic system to send data wirelessly to an Excel spreadsheet using 2 ESP8266 and Things Gateway software.
With this example, you’ll be able to explore more features from Things Gateway like:
- Real-time self updating graph (not Excel dependent)
- Data logging in .csv files
- Auto email sending when custom conditions are met (strong wind above so many kmh, temp to low… warning of heating system failure or freezer not cold enough etc)
- Data logging in stream.txt that can be replayed at speed 0.1, 0.5, 1, 5, 10, 60 or 3600 times faster
- GPS track can also be streamed on Google map at actual speed or 0.1, 0.5, 5, 10, 60 or 3600 times faster
Further development could also include:
- Other sensors like bmp180 barometric pressure sensor
- Light sensors
- Humidity sensor
- Any other digital or analog sensor with the use of multiplexing
Please note that Things Gateway was designed to use with Arduino board, but as long as you send the proper string via serial, it can be used with any board.
I hope you found this guide helpful and huge thanks to Yves Arbour that created this project!
Do you have any questions or feedback? Leave a comment down below.
Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.
Thanks for sharing this in-depth project.
I can’t wait to try it out
Thank you for your feedback!
thanks a lot 🙂 🙂
You’re welcome! 🙂
Can I use this code in Arduino IDE software
Hi.
No. The code in this project is not compatible with Arduino IDE.
Regards,
Sara
Great work!
Thanks to Yves
Great work, Rui!. A simple question: What’s the average range between two ESP8266 inside a house and/or outside?
Hi Xavier,
Thanks from bringing that up, because I totally forgot to mention that. The distance can greatly vary:
1) Obstacles between your ESP8266 (walls, etc)
2) Noise in the same frequency
It really depends in your own home and you should personally test to get a real result.
I hope this helps,
Rui
parabéns por partilhares este projecto bastante interessante, também estou a fazer um projecto no instructables,o projecto já esta a funcionar, mas ainda mal comecei a escrever, até podia aproveitar a parte de conectividade com o PC do teu projecto, se tiveres interessado em alguma informação diz qqr coisa.
Boas David,
Pode contactar-me na pagina de contacto!
Boa sorte com o projeto e abraço,
Rui
i have ESP8266-07 but…
stdin:1: attempt to call field ‘fsinfo’ (a nil value)
> dofile(“init.lua”)
not enough memory
this happens every time to me… 🙁
That happens when you try to upload which script?
Boa Tarde,
Eu carrego o script e quando faço dofile é que isso me acontece.
Obrigado
Alexander
Rui,
I purchased 2 ESP-12 units before I found out about the -201 unit. The 0.1″ spacing on the 201 would have made my efforts a lot easier. I’m using this presentation as a basis for my own version of a weather station. Thank you for your very complete information. I do notice that the fritzing diagram for the -12 version is the same for both the client and the server. It’s not hard to figure out what to do, but it might be confusing for a newbie. Thanks again, Jim.
Thank you Jim for letting me know. I’ve updated the blog post with the link to the right schematics for the ESP-12!
what is the total cost of this project am looking take for a final project? And, I will need your assistance in the process. Will you be available for me?
Thanks.
It won’t cost more than 20 dollars to get everything.
If you experience problems with the ESP please read this guide: https://randomnerdtutorials.com/esp8266-troubleshooting-guide/
Or post other questions as a comment.
Thank you,
Rui
Very interesting, i didn’t know that 8266 can send data without arduino. (no prior 8266 experience)
Unfortunately i just ordered 3 x esp8266 -01 and a dht 22. Hope i can get them to work like u did!
Awesome Justin,
Let me know your results!
Rui
Hi,
I’m currently working on something similar: git.io/ESPTemp/
Mine is battery-powered, reports via MQTT, and supports DS18B20 as well as DHT11/22/23 and NTCs.
Thanks for sharing Douglas!
Rui
thank a lots . but i want to do my project using arduino an ethernet shield . can i use this coding ?
No… This code is for the ESP8266, but It could be easily modified to work with the Arduino…
i AM
I am struggling with esplorer and so cannot load my NODEMSP v.9 with you code. I follow your instructions and copy the script into esplorer but the sendto rsp button is greyed out. Any suggestions> Also it would be nice to be able to increase the text of the terminal. I can alter the font of the script ok. Any help would be much appreciated as my frustration level is high.
Mckenzie
I am so impressed from you and your talent. You are very talented and social guy wawo.
Hello,
I don’t understand why you need two ESP8266 boards. Can’t you connect the one with the sensors directly to your computer over wi-fi?
The idea is to have an ESP8266 outside reading the sensors and send them wirelessly to the other ESP8266.
Hi…This coding will work for ESP-01 board?
Because I want to use ESP-01 as a server-side and ESP-12E as a client.
It’s possible?
can you send arduino code.Thanks in advance
Hi.
We don’t have arduino code for this particular project.
Regards,
Sara