Got It! ESP8266 + LUA + MQTT + DHT22 Working Nicely!

I wasn’t ready to post anything until I got it working reliably. Since last post I received several different version of the ESP boards. I’ll be using the ESP-07 for this demo. Got 10 pcs. for $30. Not bad!

esp-07

  1. I FLASHED the ESP using ESP-Flasher with the latest binary from NodeMCU.
  2. Connected to ESP with LuaLoader with the minor glitch of LuaLoader not showing me the actual port that the ESP was connected to. I’m sure there is a better solution but what I ended up doing was going to my Win Devices and mathing my USB FTDI to the ports available on the LuaLoader. All is good!
  3. After many nights of trying to find the right combination of code that would successfully PUBLISH through the MQTT, I ffinally came upon a solution which I modified to meet my requirements. The original code is at:http://www.esp8266.com/viewtopic.php?p=7993#p7993 by a chap with the handle calgalli.
  4. I ended up experimenting using the test.mosquitto.org/gauge/ site to send data from the ESP to a MQTT broker. There will be interruptions from others users and experimenters, as well as the random data the site itself puts into the indicator.
  5. It works like a champ! I’ve been running it a few hours with no dropouts.

Here’s the Code

I make no claim to its reliability and little or no claim in making it work. This credit goes to the original author. I’m sure there are brighter people out there that can vastly improve this. Sorry for the formatting – it looks a lot better in notepad++

Filename: init.lua
Needs: dht22.lua

=====================================

wifi.setmode(wifi.STATION)
wifi.sta.config(“linksys”,””)
print(wifi.sta.getip())

m = mqtt.Client(“Whatever”, 120)
m:lwt(“/lwt”, “Whatever”, 0, 0)

— Variables
tempc=0
sf = 0
humi=”XX”
temp=”XX”
fare=”XX”
bimb=1
PIN = 4 — data pin, GPIO2

m:on(“offline”, function(con)
print (“Checking MQTT server…”)
connectionCheck()
print(node.heap())
end)

— on publish message receive event
m:on(“message”, function(conn, topic, data)
print(topic .. “:” )
if data ~= nil then
print(data)
end
end)

function Pub_DHT22()
if sf == 0 then
sf = 1
ReadDHT22()
m:publish(“temp/random”,tempc,0,0, function(conn)
sf = 0
end)
end
end
–load DHT22 module and read sensor
function ReadDHT22()
dht22 = require(“dht22″)
dht22.read(PIN)
t = dht22.getTemperature()
h = dht22.getHumidity()
–humi=(h/10)..”.”..(h%10)
temp=(t/10)..”.”..(t%10)
tempc=temp
–fare=(9*t/50+32)..”.”..(9*t/5%10)
–print(“Humidity: “..humi..”%”)
–print(“Temperature: “..temp..” deg C”)
–print(“Temperature: “..fare..” deg F”)
— release module
dht22 = nil
package.loaded[“dht22”]=nil
end

function connectionCheck()
tmr.stop(5)
if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then
m:connect(“test.mosquitto.org”, 1883, 0, function(conn)
print(“connected”)
m:subscribe(“temp/random”,0, function(conn)
tmr.alarm(3, 3000, 1, Pub_DHT22)
end)
end)
else
tmr.alarm(5,1000,1,connectionCheck)
print(“Retry!!”)
end
end

tmr.alarm(5,1000,1,connectionCheck)

==========================================================

What it Does

I am reading the value of the temperature (C) with the ReadDHT22 function.

This records the value in tempc variable. Subsequently this value is published to test.mosquitto.org for MQTT message transfer. The mosquitto site operates a graphic dial that reads incoming messages and displays the value on the dial. Keep in mind that this is open to everyone so you’ll see the dial jitter when other values are sent.

I later set up my own “dial” using a publicly available MQTT broker which I’ll describe in a later post.

mqttdial

The biggest hurdle for me understanding how to make this work was the use of delays and timers. I’ve been programming since the time I bought an ATARI 800 and got a copy of figFORTH. Best investment in both computer and software I ever made. I owe all the basics of my learning to the the ATARI 800, especially when I started to use the joystick ports as I/O and making stepper motors move and lights blink.

The delays in LUA, as also the case in say Delphi, consume all the processing time of the cpu. Delphi has a call that allows tou to break-in and process data from time to time. It looks like LUA (as far as ESP8266) does not. So, as I gather, many programmers are using the tmr.alarm call. This will allow a short delay while still allowing interrupts to take place. as in the code above the timr.alarm call:

tmr.alarm(5,1000,1,connectionCheck)

waits for 1 second with the connectionCheck function stopping the timer upon entry. Doing it this way (I assume) allows the

m:on(“message”, function(conn, topic, data)
print(topic .. “:” )
if data ~= nil then
print(data)
end
end)

to still cause an interrupt anytime a message is available – both sent from us and sent by others to the test.mosquiito.org site.

The     tmr.alarm(3, 3000, 1, Pub_DHT22)    line will publish the temperature every 3 seconds. The sf=1 and sf=0 if statement essentially checks and see if the data was published from the last call. The interrupt cause by m:on(“offline”, function(con)….. call checks the WiFi connection any disables the publishing until the WiFi reestablishes a connection.

Most of my time was staring at the screen until an image started to appear as to how Lua and ESP8266 works. I’m not familiar with the LUA syntax and that often got in the way. But so far this project has taught me a lot.

DHT22 connected to +, – and GPIO2
CH_PD connected to +V
FTDI connected to +, -, TXD, RXD
REST connected to +V

Hope this helps. I currently have open comments turned off. Got hit by a lot of junk (Dah!) If you don’t mind registering, please leave a comment.

Working on my next post of connecting to your own indicator dial.

Outline: ESP8266, Lua, MQTT, & Temp/Humid/OLED Project

Wow! What a mouthful.

Here’s the objective:
Design and build a compact devise utilizing the components listed above (title) to develop an easy to construct, stand alone, sensor capable of detecting temperature and humidity, and having it display locally and remotely on like devices.

So what does this really mean?

I’m looking to design & build a circuit board that allows for a plug-in of a ESP8266 module (don’t know which one yet), plug-in of a OLED (display) module, and have it communicate over WiFi through a MQTT broker. Each module would subscribe to every other module’s information such that each module would display the temp and humidity of other locations within the network.

With the use of an “Internet” based broker, this network need not be local…in fact it could be world-wide. I know there are project out there like this, but I’m looking to develop the hardware assembly in a form that makes it easy for kids to build.

My goal is to take this start to finish, eith the selection of components, the drawing of the PCB, the making of the board (etching and drilling), the assembly of components, the programming of the module, and the development of the broker.

Package this up as a set of directions that kids can follow and make it a segment of “Explorer Series” of the T-Recs summer camp.

Looking for ideas and suggestions, so please feel free to comment.

Thanks all!

 

Learning More About ESP8266 , LUA, and MQTT

I got to attribute my sudden interest in the ESP8266 cheap WiFi module yo Peter Scargill’s work on putting together a really good blog. He mentions that a fellow is merging MQTT code with an easy to use interpretive language called LUA. I would love to see a Forth language formed for the ESP8266…how cool would that be?

For the MQTT/LUA thing you can visit https://github.com/tuanpmt/nodemcu-firmware. I think the BIN file is ready to flash onto the ESP chip. I’m still waiting for my modules to arrive. Hopefully soon and then I can give a play-by-play about how to make stand alone devices….as I’m hoping to do with the kids in my camp this summer.

 

Summer T-Recs Camp for Kids!

Usually at this time of the year we start planning technology activities for kids that attend our camp. We always include rocket building and launching, CO2 powered dragsters, sculpted from wood and powered by CO2 seltzer cartridges. We include 3D printing with design and invention opportunities. Last year we introduced both manual and computer controlled xylophones made from emt (electrical conduit) pipe, some foam, and a set of creative “jigs” that demonstrated a mass production manufacturing sequence.

Keep in mind, the campers are 3rd through 8th graders, so this is a real hoot for the teachers as well as the campers. Lots-o-fun and lots-o-work. We start and end everyday with big smiles.

We mix outdoor activities with exciting hands-on activities. I developed a number of different electronic activities that have included AVR based POV wands, an electronic 8 Ball fortune teller, robotic (programmable) cars, crystal radios, and a host of other projects kids just don’t typically get to do. They love it.

One of the best compliments I remember (The camp is entering its 16th year) is when a mother told us that she just couldn’t get her son (4th grader) out of bed in the summer. He’d wake up at 10 or 11 and not want to do anything but play video games (indoors). The second day of camp, she said he got up at 5 AM and stormed into her bedroom and asked if “it was time to go to camp yet?” She credited the activities at camp for this showing of energy and interest. Yahooo!

Looking at coming up with IoT projects where the kids can build a simple temp/humidity sensor project maybe with a cheap display that they can monitor with their phone/pc. This would be for the explorers group (7th/8th graders). Keep posted, I’ll let you know what I’m working on and will certainly have my eyes and ears out for any ideas or pointers. The ESP8266 and related projects looks like it could be a winner.

See you in a bit.

Welcome to the world of T-Recs

Thank you for taking the time to visit.

I’ve recently been following the blog from Peter Scagill at https://scargill.wordpress.com/.

Excellent! Just excellent. He’s been following the implementation and use of the ESP8266; a low cost means of producing IoTs (Internet of Things). These cheap devices are <$3.00 and as Peter says, are the cheapest uProcessors available.

There’s a lot to learn but with the distribution of the ESP8266 SDK, a number of very sharp people have put together programming languages and projects that make it easy (??) for people like me to build really cool projects. SDK installation, LUA, MQTT and more. Way to go.

I hope to start keeping everyone informed about my progress on this and other electronic project that pique my interest(s).

Good blogging and hope to see you soon.