A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By simplesi
#30439 I'm trying to make a standalone cheerlights receiver using NodeMCU instead of a RaspberryPi (as it will be a lot cheaper :)

I've put together this code to read one of the cheerlights feeds
http://api.thingspeak.com/channels/1417 ... /last.json
but I'd like to make it poll every 60 secs and I'm just going around in circles trying to understand how to do this
Can anyone help please?

Simon

Code: Select all-- get current cheerlights colour
print("Setting the device up as a STATION")
wifi.setmode(wifi.STATION)

print("Connecting to the AP")
wifi.sta.config("CYCY", "")
wifi.sta.connect()

print("Waiting a little bit to get an IP address...")

function main()
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c)
        field2 = string.find(c, 'field2')
        if field2 ~= nil then
            pkt = string.sub(c,field2 + 10,field2 + 15)
            print ("pkt:" .. pkt)
        end
    end )
   
    sk:connect(80,"144.212.80.11")
    sk:send("GET /channels/1417/field/2/last.json HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end


tmr.alarm(1, 1000, 1, function()
   if wifi.sta.getip()=="0.0.0.0" or wifi.sta.getip() == nil then
      print("Connect AP, Waiting...")
   else
      print("Wifi AP connected. Wicon IP:")
      print(wifi.sta.getip())
      main()
      tmr.stop(1)
   end
end)
User avatar
By simplesi
#30457 As I was waiting for my post to be approved - I came up with a working method :)

Code: Select all-- get current cheerlights colour
conn = ""
print("Setting the device up as a STATION")
wifi.setmode(wifi.STATION)

print("Connecting to the AP")
wifi.sta.config("CYCY", "")
wifi.sta.connect()

print("Waiting a little bit to get an IP address...")

function main()
    sk=net.createConnection(net.TCP, 0)
    conn = sk
    sk:on("receive", function(sck, c)
        field2 = string.find(c, 'field2')
        if field2 ~= nil then
            pkt = string.sub(c,field2 + 10,field2 + 15)
            print ("pkt:" .. pkt)
        end
    end )
   
    sk:connect(80,"144.212.80.11")
end


tmr.alarm(1, 1000, 1, function()
   if wifi.sta.getip()=="0.0.0.0" or wifi.sta.getip() == nil then
      print("Connect AP, Waiting...")
   else
      print("Wifi AP connected. Wicon IP:")
      print(wifi.sta.getip())
      main()
      tmr.stop(1)
   end
end)

tmr.alarm(2, 60000, 1, function()
    conn:send("GET /channels/1417/field/2/last.json HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)