As the title says... Chat on...

User avatar
By freedom2000
#15725 Hi,

I have a problem with thingspeak. I am trying to upload 4 fields and it fails...
With 3 fields this code is OK

Code: Select all-- send to https://api.thingspeak.com
function sendTS(APIkey, moisture, temp, humi)
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("receive"..payload) node.dsleep(30000000) end) --send and deep sleep
conn:on("connection",
   function(conn, payload)
      print("Connected TS")
      print ('moisture ' ..moisture)
      print ('temperature ' ..temp)
      print ('humidity ' ..humi)
      print ('voltage ' ..vdd)
      conn:send('GET /update?key='..APIkey..'&field1='..moisture..'&field2='..temp..'&field3='..humi..'HTTP/1.1\r\n\
      Host: api.thingspeak.com\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
conn:on("disconnection", function(conn, payload) print('Disconnected')  end)
conn:connect(80,'184.106.153.149')
end


But the same with 4 fields doen't work... Node restarts...

Any idea please ?

Code: Select all-- send to https://api.thingspeak.com
function sendTS(APIkey, moisture, temp, humi, bat)
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("receive"..payload) node.dsleep(30000000) end) --send and deep sleep
conn:on("connection",
   function(conn, payload)
      print("Connected TS")
      print ('moisture ' ..moisture)
      print ('temperature ' ..temp)
      print ('humidity ' ..humi)
      print ('voltage ' ..vdd)
      conn:send('GET /update?key='..APIkey..'&field1='..moisture..'&field2='..temp..'&field3='..humi..'&field4='..bat..'HTTP/1.1\r\n\
      Host: api.thingspeak.com\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')
   end)
conn:on("disconnection", function(conn, payload) print('Disconnected')  end)
conn:connect(80,'184.106.153.149')
end


JP
User avatar
By TerryE
#15774 vdd vs bat ?

You are also passing data into the connection function as inherited (upvalue) values. Not a good idea, IMO. Far safe to use globals, eg. add
Code: Select allTSdata={APIkey, moisture, temp, humi, bat}
to put these into a global end then in the connection routine do something like
Code: Select allfunction(conn, payload)
  locat req=("GET /update?key=%s&field1='%s&field2=%s&field3=%s&field4=%s HTTP/1.1"):format(unpack(TSdata))
  TSdata=nil
  print(req)   
  req={req,"Host: api.thingspeak.com\r\nAccept: */*",
    "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1","",""}
  conn:send(table.concat(req,"\r\n"))
end


You'll have to use the right variable for field 4 and note that I've added the space between the URI and the HTTP as you need, and you may need to tweak the format specifiers, e.g. replace %s by %d or %.3f, whatever.
User avatar
By Dimple Arora
#48918 Hi,
I am posting this reply quite late but may be helpful to the others who follow the post.

I was facing the same issue. My Lua code was as follows:
connout:send("POST /update?api_key=THINGSPEAK_KEY&field1="
..(tostring(math.floor(temprature)))
.."&field2="
..(tostring(math.floor(humi)))
.. " HTTP/1.1\r\n"
.. "Host: api.thingspeak.com\r\n"
.. "Connection: close\r\n"
.. "Accept: */*\r\n"
.. "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
.. "\r\n")
And every time I ran this it use to reset my ESP. Then, I look up at my JSON that was being created with help of following URL:
http://api.thingspeak.com/channels/[CHANNEL_ID]/feed.json?key=[THINGSPEAK_KEY]

I realized that one particular field "field2" title was "Relay Stat". I just changed the Title and removed its space and made it "RelayStat". After this I ran my code and it worked perfectly fine without reset of ESP.

Thanks,
Dimple