unable to read last field value with Lua NodeMCU | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
unable to read last field value with Lua NodeMCU
No permission to create posts
May 9, 2015
9:41 am
Avatar
freedom2000
New Member
Members
Forum Posts: 1
Member Since:
May 2, 2015
sp_UserOfflineSmall Offline

Hi,

I have upadated a channel without any problem and I would like to read it back with another ESP8266.
Here is my code (replace MyAPIkey with the right value)

wifi.setmode(wifi.STATION)
wifi.sta.autoconnect(1)
function DoIt()
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("receive"..payload) end) --send and deep sleep
conn:on("connection",
function(conn, payload)
print("Connected TS")
conn:send('GET /channels/37123/fields/1/last?key=MyAPIKEY'..'HTTP/1.1
\
Host: api.thingspeak.com
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

')
end)
conn:on("disconnection", function(conn, payload) print('Disconnected') end)
conn:connect(80,'184.106.153.149')
end
-- every 5 second, check thingspeak
tmr.alarm(0, 5000, 1, function() DoIt() end)

This code is working but always returns "-1" as the field1 value...
If I ask the same request directly from my browser then it works...

So should be a problem into the GET syntax but where ?

Thanks for your help
JP

May 19, 2015
11:57 am
Avatar
nsadventurer
New Member
Members
Forum Posts: 3
Member Since:
May 19, 2015
sp_UserOfflineSmall Offline

freedom2000 said

I have upadated a channel without any problem and I would like to read it back with another ESP8266.
Here is my code (replace MyAPIkey with the right value)

conn:send('GET /channels/37123/fields/1/last?key=MyAPIKEY'..'HTTP/1.1
\
Host: api.thingspeak.com
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

')
end)

This code is working but always returns "-1" as the field1 value...
If I ask the same request directly from my browser then it works...

So should be a problem into the GET syntax but where ?

Thanks for your help
JP

Interesting because I will by trying something similar soon. I dont have a definitive answer because I have not tried it out, however in your conn:send string after you send your API key would it need a delimiter (if that is the correct term) before sending 'HTTP1/1 ??

Dave

May 19, 2015
8:24 pm
Avatar
nsadventurer
New Member
Members
Forum Posts: 3
Member Since:
May 19, 2015
sp_UserOfflineSmall Offline

Just an update, what was missing was a couple of carriage returns. I removed all the extra formatting to cut down on bandwidth. What works for me was:

edit: on the line ..READKEY..' should read 'slash r slash n single quote bracket. Not sure how to include a couple of cr\lf without it becoming mangled in this post.

WRITEKEY="YOUR_WRITE_KEY" -- set your thingspeak.com keys
READKEY="YOUR_READ_KEY"
CHANNEL="YOUR_CHANNEL_ID"
wifi.setmode(wifi.STATION)
--wifi.sta.config("yourSSID","yourPASSWORD")
wifi.sta.connect()
tmr.delay(1000000)

function readTSfield()
conn = nil
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)success = true print("received "..payload) end)
conn:on("connection",
function(conn, payload)
print("Connected TS")
conn:send('GET /channels/'..CHANNEL..'/fields/1/last?key='..READKEY..'

')
end)
conn:on("disconnection", function(conn, payload) print('Disconnected') end)
conn:connect(80,'184.106.153.149')
end

tmr.alarm(0, 5000, 1, function() readTSfield() end)

HTH, Dave

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

AlienAboth, KeslarAboth, EnigmBioge, debbiepe18, MatthewmUb, cathrynww1

Administrators: Hans: 387, lee: 457