ESP8266: DHT22 to MYSQL and HighCharts





Introduction: ESP8266: DHT22 to MYSQL and HighCharts

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:


ESP8266 ESP-01

ESP8266flasher, also called NODEMCU FIRMWARE PROGRAMMER, attached in Step 1


LUA Loader, attached in Step 3

If you want to use the sleep function you need to solder before start, see step 2



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)

Step 1: Flash ESP8266 With Firmware


ESP8266 -> TTL





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


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

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

Disconnect GPIO0 from GND


ESP8266 -> DHT22


GPIO2 -> Data


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

Step 3: Upload Test Code to ESP8266

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, 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, change to IP where your webserver is installed

test it in your webserver by typing, e.g. "" [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

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



    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest
    • Oil Contest

      Oil Contest

    29 Discussions

    Can you make a video of this tutorial ...

    Thanks in Advance...

    hi, thanx for this very interesting post :) , I just wonder where is your webserver ? is it the same PC that you're working on , or some external Webserver. I just installed XAMPP in my PC and for the IP that I use to execute php files I use "localhost" instead of "" that you're using, what do I have to do to work with it... and thanx in advance

    Hi , I'm having the following problem if use LUALoader to run readdht22_test.lua , it does not return the temperature test and humudidade . I downloaded the Espplorer as recommended by one of the visitors, and send it returns dht22.lua > dofile ( " dht22.lua " ) ; dht22.lua : 12 : table index is nil


    2 years ago

    You might want to update the PHP script, as it uses the long outdated "mysql" functions. Now you are supposed to use "mysqli". Other than that, this is a really well written Instructable.

    Finally I get it working... DHT22 is OK :)
    I build custom nodeMCU firmware in:
    Flashed float version with build in support for DHT modules, after that there is no need to use dht22.lua :) ...and test.lua is this:
    pin = 4

    status, temp, humi, temp_dec, humi_dec =

    if status == dht.OK then

    print("DHT Temperature:"..temp..";".."Humidity:"..humi)

    elseif status == dht.ERROR_CHECKSUM then

    print( "DHT Checksum error." )

    elseif status == dht.ERROR_TIMEOUT then

    print( "DHT timed out." )

    For uploading LUA files I used ESPlorer at

    It's much more user friendly than LuaLoader... and it's much more faster :)

    Hi flopp999,

    I have some trouble at Step 3. Firmware has been flashed successfully, both LUA files were also uploaded with no problems, but if I press dofile I get only "dofile(readdht22_test.lua)" and date-time but no values.

    Is it possible to somehow test DHT22 sensor? Or, write some RAW data from it instead of calculating temp and humi?

    Many thanks ;)

    For creating MySQL table better use this code:

    `temp` varchar(255) NOT NULL,

    `humi` varchar(255) NOT NULL,

    PRIMARY KEY (`dt`)


    Yours exported table will also work, but you have there exported one row of data.
    Also very strange stuff is the primary key and never ever use DATE for filed NAME. It is the name of MySQL function and may couse troubles in the future ;)

    2 replies

    Thanks kubik256. As you may understand I am very new to MySQL, very good information.

    Do you mean that primary key cannot be "DATE" column or it is the name of column that is strange as primary key?

    *filed do you mean field?

    I mean field ;)

    A key can be that field, but you should better choose another name and don't use names of MySQL inner functions, coz it can cause some troubles ;)

    And in your sql file it is not PRIMARY KEY... but you have there these two lines at the bottom of a table spec:
    UNIQUE KEY `datum_2` (`date`),
    KEY `datum` (`date`)
    But there is no field with name "datum_2" ;)
    Better use something like this:

    PRIMARY KEY (`dt`)

    And also quotes can be omitted if you are using short names with no special chars ;) After that it can look like this:

    PRIMARY KEY (dt)

    The PRIMARY KEY have to be used, because Unique key columns are allowed to hold NULL values. The values in a PRIMARY KEY column, however, can never be NULL ....and you don't want any NULL value in date-time stamp ;)

    hmm why is this guy connecting the last pin for deep sleep ?

    3 replies

    I am an idiot. You are using the 8th pin. I was focusing on the 7. ... Sorry man. Makes sense now :)

    I am just concerned that maybe there are different versions of this wifi shield, because i've never seen the deep sleep hack done with the 7th pin.


    2 years ago

    I tried everything but the thing does not want to send data to the webserver

    when i print the cod in the command line of the esp it works , but when the script runs it it does not

    1 reply

    I dont know what the problem can be. It works for me and others.

    Try to empty the ESP and rewrite the code again to ESP


    2 years ago

    how can i change from 6sec update to 1min? is from tmr.alarm(2, 12000, 1, function() sendData() end ) ?

    1 reply

    Great article, thanks.

    I found the php script not working, i re-wrote it (adding the chipid as an additional argument), find it here:
    Moreover, on init.lua you disabled the deep sleep function but the script contain an error (please remove a "end)" from it or re-enable the deep sleep function. Even correcting this error the script result in a esp8266 starting, sending a single read and then nothing else, probably because you used the deep speep function to "restart" the module after each reading... Can you correct it?

    Many thanks again for this guide.

    2 replies

    Can you please tell me what was not working with the PHP script, I cant find the error.