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

User avatar
By cswiger
#14324 Hm, hope this is easy - I'm following an instructable project to read data from a ds18b20 which works fine (yea! :D ) - and then posts it to thingspeak, which works if I run this code line by line in ESPlorer - however running the function sendData() by itself fails to post it and no error reported, just no response. Have inserted tmr.delay(1000000) in various places but not luck.

--- send data to thingspeak.com
function sendData()
getData()
t1 = lasttemp / 10000
t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
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=YOURKEY&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")
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)

end


When successful by sending line by line or a block over usb I get:
HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sun, 12 Apr 2015 23:22:04 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Status: 200 OK

User avatar
By cswiger
#14325 ... and ignore the obvious YOURKEY setting, I've been changing deleting so much stuff - but fixed it and here's test run:


> sendData() <-- run the function, nothing

Temp:22.7500 C

Sending data to thingspeak.com
> Closing connection
Got disconnection...
<-- that's it


Now select the block and send it over:
>
t1 = lasttemp / 10000

> t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)

> print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
Temp:22.7500 C

> -- conection to thingspeak.com

> print("Sending data to thingspeak.com")

Sending data to thingspeak.com
> 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=DELETEDFORSECURITY&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")

> 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")

> HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Mon, 13 Apr 2015 00:15:11 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "6512bd43d9caa6e02c990b0a82652dca"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: d0248127-f2e1-4dc9-a225-93ec8e4e129f

2
11
0

>
conn:on("sent",function(conn)

print("Closing connection")

>> conn:close()

>> end)

> conn:on("disconnection", function(conn)

conn:on("disconnection", function(conn)

>> print("Got disconnection...")

end)

>>
User avatar
By jankop
#14378 Here's the code that I wrote and that is functional. Customize it for your needs .

Code: Select all-- send to https://api.thingspeak.com
function SendTS()
conn = net.createConnection(net.TCP, 0)
conn:connect(80,'184.106.153.149')
conn:on("connection",
   function(conn) print("Connected")
   conn:send('GET /update?key='..WRITEKEY..
   '&field1='..string.format("%.2f",humi)..
   '&field2='..string.format("%.2f",temp)..
   '&field3='..string.format("%.2f",vdd3)..
   ' 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("sent",
   function(conn)
   print("Data sent!")
   print("*************")
   conn:close() -- You can disable this row for recieve thingspeak.com answer
   end)
conn:on("receive",
   function(conn, payload)
   print(payload)
   conn:close()
   end)
conn:on("disconnection",
   function(conn)
   print("Disconnect")
   end)
end