-->
Page 1 of 2

Read web page every 60 secs

PostPosted: Sat Oct 03, 2015 5:36 pm
by simplesi
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)

Re: Read web page every 60 secs

PostPosted: Sun Oct 04, 2015 5:09 am
by simplesi
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)

Re: Read web page every 60 secs

PostPosted: Mon Oct 05, 2015 4:40 pm
by thingspeak
Also, note that you can use the ThingSpeak API to get the last color as a text string.

http://api.thingspeak.com/channels/1417/field/2/last.txt

This might be easier to use and parse.

Re: Read web page every 60 secs

PostPosted: Wed Oct 07, 2015 10:43 am
by simplesi
I'd forgotton about that feed :)
Ta
Simon