for those who wish to transmit data directly from emontx shield - to mqtt json, http or directly to a database. via wifi connection -
i used the esptoy as it supports 5V and can be plug in directly to the arduino board. as a broker use mosca over mosquitto as it does websockets and can be easily configurable to android and other devices. - I use this and freeboard as a local iot and various mobile iot for remote access.
on your esp8266 board install lua and its default float firmware plus mqtt, cjson, http depending how you wish to transmit data
in your arduino code add these: i.e. for CT_1 (…
Serial.print("cmd_1("); Serial.print(value); Serial.println(")");
it can be as many times as you like – cmd_1, cmd_2, cmd_3, cmd_4 … etc for your cts temperature sensor or what ever else you might have connected as you freed up a lot of pins that would’ve been used by the built in RF chip -plus 10 more pins on the esp8266
install these three scripts to your esp8266 - init, mqttW and serial123
serial123.lua captures the data and transmits it for you (reads data from serial of emontx captures and creates webpage and sends mqtt data to broker (adjust to your needs - my serial samples the emontx 5 times takes an average then transmits by mqtt to broker )
serial123.lua
_________
value_2=0
value=0
value_3=0
i=0
g=0
w=0
cnt1=0
cnt2=0
cnt3=0
gs1=0
gs2=0
gs3=0
uart.setup(0,9600,8,0,1)
print ("uart.setup(0,9600,8,0,1) finished")
print ("waiting for serial data....")
function cmd_1(value)
-- print(value)
i= value
-- print(i)
payload_1=(value)
-- print(payload_1)
if pcall(payload_1) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
m:publish("/invert",payload_1,0,0 )
m:close()
else
print(" error -inverter" )
end
if cnt1<5 then
cnt1=(cnt1+1)
gs1=(gs1+payload_1)
print(cnt1)
print(gs1)
end
if cnt1>4 then
gs1=(gs1/5)
cnt1=0
payload_1=gs1
gs1=0
--m:subscribe("/invert",0, function(conn)print("connected") end)
m:publish("/invert",payload_1,0,0, function(conn) print("sent") end)
end
end
function cmd_2(value_2)
g=value_2
payload_2=value_2
if pcall(payload_2) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
m:publish("/grid",payload_2,0,0 )
m:close()
else
print(" error - grid" )
end
if cnt2<5 then
cnt2=(cnt2+1)
gs2=(gs2+payload_2)
print(cnt2)
print(gs2)
end
if cnt2>4 then
gs2=(gs2/5)
cnt2=0
payload_2=gs2
gs2=0
-- m:subscribe("/grid",0, function(conn)print("connected") end)
m:publish("/grid",payload_2,0,0, function(conn) print("sent") end)
end
end
function cmd_3(value_3)
-- print("cmd_3("..value_3..") received")
w=value_3
payload_3=value_3
if pcall(payload_3) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
m:publish("/wind",payload_1,0,0 )
m:close()
else
print(" error - wind" )
end
if cnt3<5 then
cnt3=(cnt3+1)
gs3=(gs3+payload_3)
print(cnt3)
print(gs3)
end
if cnt3>4 then
gs3=(gs3/5)
cnt3=0
payload_3=gs3
gs3=0
-- m:subscribe("/wind",0, function(conn)print("connected") end)
m:publish("/wind",payload_3,0,0, function(conn) print("sent") end)
--end)
end
end
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn, payload)
print(payload)
conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 10\n\n" ..
"<!DOCTYPE HTML>" ..
"<html><body>" ..
"<h1>GTI LIMITER</h1>" ..
"<b>Solar Output : " ..i.. " watts </b><br>" ..
"<b>Grid Level : " ..g.. " watts </b><br>" ..
"<b>Wind Output : " ..w.. " watts </b><br>" ..
"</html></body>")
end)
conn:on("sent",function(conn)
conn:close()
end)
end)
_____________________
mqttW.lua -- sets up mqtt
___________________
m = mqtt.Client(wifi.sta.getmac(), 120, "user", "password")
m:lwt("/lwt", wifi.sta.getmac(), 0, 0)
-- on publish message receive event
m:on("message", function(conn,topic, data)
end)
m:connect("192.168.168.41", 1883, 0, function(conn)
print("connected") end)
--m:close()
tmr.alarm(1, 20000, 1, function()
---dofile("serial123.lua")
end)
------------------------------
init.lua -- setup wifi and . with the esptoy it uses the onboard lights to debug wifi connection and styus- red no wifi, blue, connecting, green connected and running
_________________
-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
r=0
g=0
b=0
pin_red=2
pin_green=4
pin_but=3
pin_blue=1
gpio.mode(pin_but, gpio.INPUT, gpio.PULLUP)
pwm.setup(pin_red, 100, 512)
pwm.setup(pin_green,100,512)
pwm.setup(pin_blue,100, 0)
pwm.start(pin_red)
pwm.start(pin_green)
pwm.start(pin_blue)
tmr.delay(200000)
pwm.setduty(pin_red,0)
pwm.setduty(pin_green,0)
pwm.setduty(pin_blue,0)
function startup()
if file.open("init.lua") == nil then
print("init.lua deleted or renamed")
pwm.setduty(pin_green, 10)
else
print("Running")
pwm.setduty(pin_blue, 0)
pwm.setduty(pin_green, 100)
file.close("init.lua")
dofile("mqttW.lua")
dofile("serial123.lua")
-- the actual application is stored in 'application.lua'
-- dofile("application.lua")
end
end
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config("wifi","1234567890")
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.alarm(1, 2000, 1, function()
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
pwm.setduty(pin_red, 100)
else
tmr.stop(1)
print("WiFi connection established, IP address: " .. wifi.sta.getip())
pwm.setduty(pin_blue,100)
pwm.setduty(pin_red, 0)
print("You have 10 seconds to abort")
print("Waiting...")
tmr.alarm(0, 10000, 0, startup)
end
end)
--------------
working well to freeboard IoT and to various handheld moblie IoT devices
Oh yeah, just so you know. The pcall in the serial123 code is there because of some sort of bug in lua firmware. If they correct the bug, and pcall works afterward, you might have to edit those lines slightly. (take out the m:close() and comment out tthe m:publish after the pcall routine. For some reason, when calling the serial input value, it comes up nil for mqtt.(nothing else, just mqtt) After it passes through the pcall routine, it is no longer a nil value - just for future reference.