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

<p>Can you make a video of this tutorial ...</p><p>Thanks in Advance...</p>
<p>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 &quot;localhost&quot; instead of &quot;; that you're using, what do I have to do to work with it... and thanx in advance</p>
<p>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 &gt; dofile ( &quot; dht22.lua &quot; ) ; dht22.lua : 12 : table index is nil</p>
<p>You might want to update the PHP script, as it uses the long outdated &quot;mysql&quot; functions. Now you are supposed to use &quot;mysqli&quot;. Other than that, this is a really well written Instructable.</p>
<p>Finally I get it working... DHT22 is OK :)<br>I build custom nodeMCU firmware in: <a href="http://nodemcu-build.com/" rel="nofollow">http://nodemcu-build.com/</a><br>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:<br>---------------------------<br>pin = 4</p><p>status, temp, humi, temp_dec, humi_dec = dht.read(pin)</p><p>if status == dht.OK then</p><p> print(&quot;DHT Temperature:&quot;..temp..&quot;;&quot;..&quot;Humidity:&quot;..humi)</p><p>elseif status == dht.ERROR_CHECKSUM then</p><p> print( &quot;DHT Checksum error.&quot; )</p><p>elseif status == dht.ERROR_TIMEOUT then</p><p> print( &quot;DHT timed out.&quot; )</p><p>end<br>---------------------------<br>For uploading LUA files I used ESPlorer at <a href="http://esp8266.ru/esplorer/" rel="nofollow"> http://esp8266.ru/esplorer/</a></p><p>It's much more user friendly than LuaLoader... and it's much more faster :)</p>
<p>Hi <a href="https://www.instructables.com/member/flopp999" rel="nofollow">flopp999</a>,</p><p><br>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 &quot;dofile(readdht22_test.lua)&quot; and date-time but no values.<br><br>Is it possible to somehow test DHT22 sensor? Or, write some RAW data from it instead of calculating temp and humi?<br><br>Many thanks ;)</p>
<p>For creating MySQL table better use this code:<br>-------------------------------------------------------------------------------------------<br>CREATE TABLE IF NOT EXISTS `dht22` (<br>`dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,</p><p>`temp` varchar(255) NOT NULL,</p><p>`humi` varchar(255) NOT NULL,</p><p>PRIMARY KEY (`dt`)</p><p>) ENGINE=InnoDB DEFAULT CHARSET=utf8;</p><p>-------------------------------------------------------------------------------------------<br>Yours exported table will also work, but you have there exported one row of data.<br>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 ;)</p>
<p>Thanks kubik256. As you may understand I am very new to MySQL, very good information.</p><p>Do you mean that primary key cannot be &quot;DATE&quot; column or it is the name of column that is strange as primary key?</p><p>*filed do you mean field?</p>
I mean field ;)<br><br>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 ;)<br><br>And in your sql file it is not PRIMARY KEY... but you have there these two lines at the bottom of a table spec:<br>----------------------------------------------<br>UNIQUE KEY `datum_2` (`date`),<br>KEY `datum` (`date`)<br>----------------------------------------------<br>But there is no field with name &quot;datum_2&quot; ;)<br>Better use something like this:<br><br>PRIMARY KEY (`dt`)<br><br>And also quotes can be omitted if you are using short names with no special chars ;) After that it can look like this:<br><br>PRIMARY KEY (dt)<br><br>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 ;)
<p>hmm why is this guy connecting the last pin for deep sleep ?</p><p>http://www.bntdumas.com/2015/07/23/how-to-battery-powered-temperature-and-humidity-sensors/</p>
I am an idiot. You are using the 8th pin. I was focusing on the 7. ... Sorry man. Makes sense now :)
Hi, you better ask him.
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.
<p>I tried everything but the thing does not want to send data to the webserver</p><p>when i print the cod in the command line of the esp it works , but when the script runs it it does not</p>
<p>I dont know what the problem can be. It works for me and others.</p><p>Try to empty the ESP and rewrite the code again to ESP</p>
<p>how can i change from 6sec update to 1min? is from tmr.alarm(2, 12000, 1, function() sendData() end ) ?</p>
<p>Please read more about tmr.alarm here</p><p><a href="http://www.nodemcu.com/docs/timer-module/" rel="nofollow">http://www.nodemcu.com/docs/timer-module/</a></p><p>12000, means 12 seconds</p>
<p>Great article, thanks.<br><br>I found the php script not working, i re-wrote it (adding the chipid as an additional argument), find it here: http://pastebin.com/LsAiGsjQ<br>Moreover, on init.lua you disabled the deep sleep function but the script contain an error (please remove a &quot;end)&quot; 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 &quot;restart&quot; the module after each reading... Can you correct it?<br><br>Many thanks again for this guide.</p>
<p>Can you please tell me what was not working with the PHP script, I cant find the error.</p>
<p>I have now updated the INIT.LUA, thanks.</p><p>I will look at the other error.</p>
<p>I made the following change to readdht22.lua to have the readings to update every six seconds, otherwise it only read the dht22 once and then stopped.<br></p><p>original - tmr.alarm(2, 12000, 0, function() sendData() end )</p><p>new - tmr.alarm(2, 12000, 1, function() sendData() end )</p>
<p>Thanks for seeing this. I have today update the files.</p>
<p>Thanks for this very good tutorial!!</p><p>But i get a error message, what could be the reason?</p><p>ERROR Message:</p><p>Executing readdht22.lua</p><p>PANIC: unprotected error in call to Lua API (readdht22.lua:23: attempt to call global 'sendData' (a nil value))</p><p>$J&rsaquo;C&rdquo;&yacute;3j&Otilde;&thorn;&sup1;RG&rsquo;A&Acirc;NS&shy;&Oslash;&brvbar;&yuml;XH&oslash;</p><p>NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4</p><p>*** You've got 3 sec to stop timer 0 ***</p>
<p>I found the error and have now correct it.</p><p>Please notice that I have changed name for the files when you test the sensor</p><p>Thanks for letting me know about your problem</p>
<p>Thank you very much flopp999 for your efforts- well done!!</p>
<p>Hi,</p><p>It is given an error :s on step 2, on readdht22.lua file.</p><p>Please could you say what's going on?</p><p> PANIC: unprotected error in call to Lua API (readdht22.lua:23: attempt to call global 'sendData' (a nil value))</p>
<p>I found the error and have now correct it.</p><p>Please notice that I have changed name for the files when you test the sensor</p><p>Thanks for letting me know about your problem</p>
<p>Thanks for uploading this. I'm trying to build something similar and have so far built something that uses an Arduino Pro Mini 3.3v. I take the reading and send it to thingspeak.com, but im having issues with power draw.</p><p>Im using a single 18650 3.7v battery along with a charging/protection board. I also have a 1.5w 12v solar panel connected to a buck convertor. The buck convertor is plugged into the charger. It all then passes through a boost convertor to raise the voltage to 5v which is then regulated to the 3.3v the ESP and Arduino need to run. Im certain this is probably the most inefficient way to get this to run so I was looking at alternatives.</p><p>I came across your instructable when I was searching for ways to lower my power consumption by eliminating the Arduino aspect and only using the ESP8266.</p><p>Now my question for you is on my Arduino board I have to put a resister between the VCC and Data pins on the DHT22 before I can get any response from it. Do you need to do the same thing here?</p><p>Do you happen to know a way to regulate 3.7v to 3.3 so I can remove the regulator and boost convertor completely?</p>
<p>No, i dont have to use a resistor for DHT22.</p><p>Maybe this can help you to regulate to 3.3? http://www.ebay.com/itm/AMS1117-3-3V-DC-DC-Step-Down-Voltage-Regulator-Adapter-Convertor-AMS1117-3-3-/131588593936?hash=item1ea34a9910</p>

About This Instructable




More by flopp999:ESP8266: DHT22 to MYSQL and HighCharts 
Add instructable to: