ESP8266 WiFi Project – DHT22 naar MYSQL en HighCharts

ESP8266 chip icon

Website / Bron

Informatie (ENG)


Step 1: Flash ESP8266 with firmware

I will tell you how I did to get an ESP8266-01 to read data from DHT22 and send it to an MYSQL database then view the data with HighCharts

Features that I will add later is HighStocks

You need below items:

  • DHT22
  • ESP8266 ESP-01
  • ESP8266flasher, also called NODEMCU FIRMWARE PROGRAMMER, attached in Step 1
  • USB->TTL
  • LUA Loader, attached in Step 3
  • If you want to use the sleep function you need to solder before start, see step 2

Links:

GITHUB for NODEMCU

NODEMCU website

HighCharts demo

EDIT 150826: Updated INIT.LUA. added “–” before “end)” line after — node.dsleep(sleep*60*1000*1000)

EDIT 150924: new ESP8266Flasher version, corrected errors in readdht22.lua, renamed to readdht22_test.lua for testing purpose

EDIT 151005: updated readdht22_test.lua(removed timer) and readdht22.lua(change so timer repeats)

TTL verbinden met ESP8266

NodeMCU flasher screen

Connect:

ESP8266 -> TTL
VCC -> VCC
CH_PD -> VCC
GND -> GND
GPIO0 -> GND
TX -> RX
RX -> TX

The USB-TTL that I used had the possibility to select power, I used 3.3V

Plug-in USB-TTL to your computer, wait until your PC have installed drivers for USB-TTL

Start ESP8266Flasher.

I used version 1.0.5619.5450 of ESP8266Flasher EDIT 20150924

I used version 0.9.5 20150318 firmware, build-in ESP8266Flasher

Config->INTERNAL://NODEMCU

Advanced->Baudrate 230400, Flash size 4MByte, Flash speed 40MHz, SPI Mode DIO

Select COM-port and press Flash. If the connection is correct you will get a QR-code at bottom left

When it is ready, green check appear in bottom left, close ESP8266Flasher


Step 2: Connect ESP8266 to DHT22

TTL verbinden met ESP8266 en DHT22

Unplug USB-TTL, so the power is disconnected, we dont want to damage anything

Disconnect GPIO0 from GND

Connect:
ESP8266 -> DHT22
VCC -> VCC
GPIO2 -> Data
GND -> GND

If you want to use Sleep function you need to solder RESET to leg 8 on ESP8266 chip:

ESP8266 ESP-01 slaap functie gebruiken


Step 3: Upload test code to ESP8266

LuaLoader screen

Before we send any data to an MYSQL we must test so that the ESP8266 can read from DHT22

I used LUA Loader version 0.87

Plug-in USB-TTL to your computer

Start LUA Loader

Select COM-port, Settings->Comm Port Settings

Click on Connect, upper right field should be green if you have connection

Click on Restart, middle right side

You should now see some text poping up, if not you have something wrong with the firmware in ESP8266 or the connection is not correct

To upload a file click on Upload file, bottom right

Upload dht22.lua and readdht22_test.lua

dht22.lua: to communication to dht22 sensor

readdht22_test.lua: read data from sensor and print it to the screen in LUA

When you have uploaded those two files select readdht22_test.lua from the yellow section and click dofile

Now you should see two lines

Humidity: xx.x %

Temperature: xx.x deg C

For every press on dofile you should see values from sensor


Step 4: Prepare MYSQL database

I am using a WAMPServer 2.5 with Apache 2.4.9, PHP 5.5.12 and MYSQL 5.6.17

I don’t know so much about PHP and MYSQL so please comment if some information is missing or wrong

Create a user named “user” and the password must be “password”, these can be changed later on if wanted

Attached is an export from the database that I am using

Database name is dht22 and datatable name is temphumi

column 1 is date

column 2 is temp

column 3 is humidity


Step 5: Test PHP file

This is my dht22.php, save it in the root of www on your webserver.

change IP adress inside file, now it is 123.123.123.123, change to IP where your webserver is installed

test it in your webserver by typing, e.g. “123.123.123.123/dht22.php?temp=20&humi=30” [ENTER]

open your MYSQL database and now you must see a date, temp and humi values


Step 6: Upload final code to ESP8266

change WiFi settings in top of file, SSID, Password and IP

readdht22.lua: read data from sensor and write to MYSQL

if you want to use Sleep function you have to remove — from init.lua file

–tmr.alarm(1, gsleep*1000, 0, function()

— print(“timer1 trigger dsleep”)

— node.dsleep(sleep*60*1000*1000)


Step 7: View data with HighCharts

JQuery

Download files. Save .js files to a sub folder named JS and php files in the folder above JS

Access temphumi.php with a web browser, if everything is OK you should see data that ES8266 has sent

will be updated soon…