Simple daughter board to transmit emontx shield data, web, mqtt etc

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.

here a zip file as i can not seam to get your code </>to work me

init.lua.zip (2.1 KB)

thanks jon for fixing up the page i can seam to get code </> to work for me

1 Like

here a snapshot of my current IoT dashboard if someone is curious . still configuring it a bit over from html pages to mqtt. solar list a 0 as i need a new CTs for it so i have everything on wind currently … next on the agenda is set up mqtt to rrd and insert historical data in. then from there recompile all my other IoT devices to use Mqtt and insert it in my dashboard – but not looking too shabby so far… :slight_smile:

( I notice that I had to adjust the delay of the Mqtt form script above so I set delay at 2, 3 and 5 count intervals, as the swarm of publish would cause my broker to to reject incoming from this devices after a few hours, but since adjusting to a staggared publish it has not stopped yet)

1 Like

Nice work. Am I allowed to ask if you can sell one of your units ? this is my first post and I don’t want to break too many rules.

Don’t worry, Dave (and welcome aboard). You are allowed to ask. We have “For Sale” & “Wanted” tags for private individuals, what’s not acceptable is blatant commercial advertising.

Thanks Dave for the compliment - never thought about selling it… but was planning to post a fritz sketch of a shield that plugs on top of emontx shield . when even I get every thing said and done…

but onto that here is my histoical data insert sample - it can show by minutes, hours days week months and even year it also has weekly comparison.

I made a simple power meter a few years ago from a ted1001 I just reused my info from there and inserted into here

here a sample shot of the last 10 minutes as I only had it running to a couple hours…

Here a copy of pl file to capture mqtt to rrd and some explanation of the workings. plus my old ted project where you can grab the shell scripts to create the historical data you want you just need to edit them a little to work with what ever configuration you have

iot.zip (582.5 KB)

– next on the agenda to build wireless solar panel monitors and pwm that tell the volts and amps of each panel and incorporate into my GTI sketch. to power back panels when not required ( but I think that might be a winter project as I got alot on my plate and I running out of time)… but in a few days or so I will upload how to install and configure the IoT that I am using … – currently for this i am just using an old acer mini laptop running ubuntu . then once that is working fine on there I will move it over to a orange pi( raspberry pi compatible)

just thought I mention again if you use the ESPTOY 1.24 ( it the newest one) - all you need to do is connect the 5v and gnd of the esptoy to 5v and gnd of the emontx shield and the rx on the esptoy to the tx on the emontx sheild ( with extended header) or solider on if just basic emontx shield ( do not solider permanently otherwise you can not prgram it the the rx/tx has to be able to disconnect for programming)

upload the above script and away you go :slight_smile:

here an up dated mqqtw.lua file for those who might be interested

it now re-establish the wifi and mqtt connection on failure - ie rebooting router, radio interference etc …
and updated the error code of the light module . now you know the type of failure from the colour

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.150", 1883, 0, function(conn)
      print("connected") mq=0 end)

tmr.alarm(1, 20000, 1, function()



if mq == 1 then

m:lwt("/lwt", "offline", 0, 0)

m:on("connect", function(client) print ("connected") mq=0 end)
m:on("offline", function(client) print ("offline") mq=1 end)

-- on publish message receive event
m:on("message", function(client, topic, data) 
 print(topic .. ":" ) 
if data ~= nil then
print(data)
end
end)

-- for TLS: m:connect("192.168.168.150", secure-port, 1)
m:connect("192.168.168.150", 1883, 0, function(client) print("connected") 
  mq=0  
  pwm.setduty(pin_green,100) 
 pwm.setduty(pin_blue, 0)
 end, 
 function(client, reason) print("failed reason: "..reason)
 mq=1
                                 pwm.setduty(pin_green,50)
                                 pwm.setduty(pin_blue, 50)
                                 end)
 
    end


--print("hello??")

if wifi.sta.getrssi() == nil then

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config("wii1","")
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
 tmr.alarm(2, 2000, 1, function()
if wifi.sta.getip() == nil then
    print("Waiting for IP address...")
    pwm.setduty(pin_green,0)
    pwm.setduty(pin_red, 100)
 else
    tmr.stop(2)
    print("WiFi connection re established, IP address: " .. wifi.sta.getip())
    pwm.setduty(pin_blue,100)
    pwm.setduty(pin_red, 0)
    tmr.alarm(0, 2000, 0, function()
    print("reconnecting mqtt")


  m:lwt("/lwt", "offline", 0, 0)

  m:on("connect", function(client) print ("connected") end)
   m:on("offline", function(client) print ("offline") end)

  -- on publish message receive event
   m:on("message", function(client, topic, data) 
     print(topic .. ":" ) 
     if data ~= nil then
    print(data)
     end
   end)

   -- for TLS: m:connect("192.168.11.118", secure-port, 1)
   m:connect("192.168.168.150", 1883, 0, function(client) print("connected")
                     mq=0
                 pwm.setduty(pin_green,100)
                 pwm.setduty(pin_blue, 0)
                end, 
                function(client, reason) print("failed reason: "..reason)
                mq=1
              pwm.setduty(pin_green,50)
             pwm.setduty(pin_blue, 50)
             end)


      end)

   end
   end)



end
--print(reason)
--print(mq)

end)

it working well though it still needs a bit of cleanup to remove some redundant stuff. - but installed and running fine

mqtt.lua?

from above the 3 Lua scripts. posted to get esp8266 working with emon shield via the serial port
init.lua
serial123.lua
mqqtW.lua ( w only denoted it was working for me but you can rename it what ever you like just edit the dofile in init.lua if you do )

new update - reinstalled my orange pi with newest os as the previous one was a bit buggy. (network related) anyway if any curious - I just an updated rrd graph scripts I only did the grid and wind currently . they tell you the peak ,avg, min and total usage for a determine period of time , they also project the the future usage based on ie the 8hr graph it will project the 24 hr usage , 24hr or weekly graphs will project the the monthly usage. they also compare current week to previous weeks.

freeboard-rrd.zip (7.2 KB)

for easier manipulation of data and the micro controller itself suggest installing webmin - gives you remote control access to configure it easily in a nice gui page

sample views:

though you do not really see the two week comparison as the RRD container only has ~24hrs of data ( also the week projection is off as it is using the 24 hour period and extrapolating the data based on that small time period)

1 Like

update - to mqttW.lua after a few days of running it still stalled occasionally usually in the middle of the night for some strange reason–. so i added a call back and connection watch dog. ( you can adjust the timing just increase the wd count). now it will pub and subscribe to a topic if it does not successfully hear back or publish after ~3 minutes and several attempts it will then reboot the wireless to correct the issue. also sometimes the timing between when the wireless esp8266 gets an ip and the orange pi are out of sync after a power outage or any disruption . now when it first initializes the mqtt it will retry for 3 minutes also to make a connection . and if it fails reboot the radio and try again for another 3 minutes. but you can adjust the watch dog longer if you want currently tries 7 times at +20 second intervals )

mqttW.lua.zip (1.0 KB)

for those who might use one of my various energy monitor /diverter or GTI controllers on a arduino here a new sketch based on arduino instead of LUA… GTI_SERIAL_READ.zip (1.7 KB)

it basically a com port reader so you can use it on other arduino projects to transmit data via MQTT with an esp8266… simply include a println with your data enclosed in the brackets " cmd_1(value) " it will transmit the value via MQTT to your specified topic . it currently handle 3 inputs values . but more can be easily added cmd_1(), cmd_2(), cmd_3()… etc