ESP8266: DHT22 to MYSQL and HighCharts

by flopp999 in Circuits > Wireless

110015 Views, 144 Favorites, 0 Comments

ESP8266: DHT22 to MYSQL and HighCharts

start.jpg

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)

Flash ESP8266 With Firmware

esp8266TTL.png
esp8266flasher.png

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

Downloads

Connect ESP8266 to DHT22

esp8266DHT22.png
ESP8266_sleep.jpg

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

Upload Test Code to ESP8266

lualoader.gif

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

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

Downloads

Test PHP File

This is my dht22.php, download dht22.txt and rename to dht22.php, it seems to be blocked to download .php files, 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

Downloads

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)

VIew Data With HighCharts

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

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

will be updated soon