Post your best Lua script examples here

User avatar
By kniazio
#29200 Big, big thanks

How to configure Thingspeak to work with the Notify My Android.
Some examples?

I already did.
Here's an example Thinghttp:
URL - https://www.notifymyandroid.com/publicapi/notify
Method - POST
Content type - application/x-www-form-urlencoded
Host - notifymyandroid.com

Body - apikey=your_api_key&application=your_aplikation_name&event=your_message&description=your_mesage_description&priority=0
User avatar
By toyorg
#29737 Can someone help me? What's wrong with this code? I always got this "Got disconnection...", WiFi connection is OK.

Code: Select allapiKey = "xXx" -- Add thingspeak api key
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,"184.106.153.149")
conn:send("GET /update?key="..apiKey.."&field1="..tempin.."&field2="..hum.."&field3="..tempout.."")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
print("Closing connection")
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
User avatar
By bbx10node
#29745 184.106.153.149 might be an old or wrong IP address. Connect to "api.thingspeak.com" instead.

If this fails, try "ping api.thingspeak.com" and use the IP address it returns. This is less desirable since IP addresses frequently change so it may work today but fail in the future.
User avatar
By jankop
#29765 It's the wrong time sequence of commands in your program. First, you have to wait for a connection, send, and you can not close the connection before everything is sent.
There is a functional sketch.

Code: Select all--    Tested with Lua NodeMCU 0.9.6 (nodemcu_float_0.9.6-dev_20150704.bin)
--    Minimal period for data send to api.thingspeak.com is 15s
--****************************************************************
   ssid="yourSSID"    -- your router SSID
        pass="yourPassword"   -- your router password
   wifi.setmode(wifi.STATION)
   wifi.sta.config(ssid,pass,1)

   apiKey="yourAPIwriteKey" -- set your thingspeak.com key
   tempin=21.3   
   hum=52.5   
   tempout=18.7

-- send data to https://api.thingspeak.com
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'api.thingspeak.com') -- This row is good, but only for newer firmware
--conn:connect(80,'184.106.153.149') -- This is worse, but it also works well with the older firmware.
conn:on("connection",
   function(conn)
   print("Connection!")
   conn:send('GET /update?key='..apiKey..
   '&headers=false'..
   '&field1='..tempin..
   '&field2='..hum..
   '&field3='..tempout..
   ' HTTP/1.1\r\n'..
   'Host: api.thingspeak.com\r\nAccept: */*\r\n'..
   'User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
conn:on("sent",
   function(conn)
   print("Sent!")
   --conn:close() -- You can enable this row for skip thingspeak.com answer
   end)
conn:on("receive",
   function(conn, payload)
   print("Receive!")
   print(payload)
   conn:close()
   end)
conn:on("disconnection",
   function(conn)
   print("Disconnection!")
   end)