-->
Page 1 of 1

capture serial outputs and broadcast with esp8266 web, mqtt

PostPosted: Sat Sep 10, 2016 9:43 am
by sash99
hi there here my simple project, I built it for emontx shield ( GTI limiter) project as a simple daughter board to transmit data from it to be displayed in Iot and mysql database but can be adapted to any pre-existing ardruino project easily' :)

in your arduino project add this line can be as many inputs as required cmd_1, cmd_2 ...ect
Code: Select allsomevalue=10 
Serial.print("cmd_1("); Serial.print(somevalue); Serial.println(")");
-- this will print the value 10 in serial com to be captured by the esp8266

now install lau firmware to the esp8266 with default plus mqtt , http, cjson depending how you want to send data.

then install these 3 scripts to your esp8266 ( i uses esptoy due to its 5v input and 3 colour lights as it can be powered directly off the arduino board)
due to a bug in firmware some of this code is just a work around (pcall section)
for me this captures 5 samples from my emon tx sheild takes an average and transmit the data about once every second - for longer send time just increase the sample
serial123.lua
Code: Select allvalue_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)



the next code sets up the mqtt ( it needs to be cleaned up a bit)
mqttW.lua
Code: Select allm = 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)


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


end)


the last bit of code is the init.lua - basically it sets up the wifi connection and uses the lights of the esptoy as visual debug ( red - no wifi connection -- blue setting up ip - green up and running). it also allows you a way to get out of panic - all you need to do is up plug the wifi reboot the device and when it stuck at finding connection you simply reflash new code)

Code: Select all-- 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("wii","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)



when working with serial inputs. you can not flash the esp8266 with the serial connected simply unplug the serial from your arduino project. flash or "send to esp" once finished sending /uploading simply reconnect the serial again . there is some harmless erronous errors displayed in the com window of esplorer, when it first boots (if connected to a serial at the time ) but that because I did not setup serial port in init. once it set up the serial later in the boot the error goes away..

good luck have fun