ESP8266 – Wireless Weather Station with Data Logging to Excel

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.

This project is a collaboration between Yves Arbour and Rui Santos.

 

Before you continue reading this project, please complete the following tutorials: 

If you like the ESP WiFi module and you want to build more projects you can download my eBook called “Home Automation using ESP8266” hereLet’s get started!

Summary

Here’s a Figure that describes exactly how everything works together:

introduction esp8266 wireless station

Parts List

Here’s the hardware that you need to make the weather station:

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.

DS18B20 pinout

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.

esp-201

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

View raw code

When you upload the counterMax.lua script it is expected to print the following error:

uploading counter max

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

View raw code

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  

View raw code

Client Circuit

Follow this circuit to build your ESP client and if you are using the ESP-12, you can view the schematics here:

weatherStation-201 Sketch_bb

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)

View raw code

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.

WirelessWeatherStationServer_201_bbWe’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)

ESP8266 with PCBs

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.

ThingsGatewaydownload

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.

things gateway app

Then follow these instructions:

  1. Go to the Config tab
  2. Select the Serial COM Port of your ESP8266 Server
  3. Set the Serial port Speed to 9600

things config

Demonstration

Open the RealTime tab and select From Serial to Excel.

ThingsGateway

An Excel spreadsheet should appear and your data should start appearing there every few seconds!

ThingsGateway excel

Your data is being stored in the example Excel spreadsheet in that folder:

things gateway example

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.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

33 thoughts on “ESP8266 – Wireless Weather Station with Data Logging to Excel”

    • 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

      Reply
  1. 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.

    Reply
  2. 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… 🙁

    Reply
  3. 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.

    Reply
  4. 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.

    Reply
  5. 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!

    Reply
  6. 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.

    Reply
  7. 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

    Reply
  8. 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?

    Reply
  9. 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?

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.