Posting to Thingspeak with ESP8266 and NodeMCU

This is just a short post to show some code to post to Thingspeak from an ESP8266 running NodeMCU.

The code only post to field1, but can easily get updated to post to multiple fields at the same time. To test this, you don’t need any extra sensors, because the example is just posing the internal voltage reading.

Only thing needed to be changed, is the “CHANNEL_API_KEY”, which you can find on the Thingspeak page after creating your channel, and the WiFi credentials.

wifi.setmode(wifi.STATION);
wifi.sta.config("SSID" ,"WIFIKEY");

function postThingSpeak(level)
	connout = nil
	connout = net.createConnection(net.TCP, 0)

	connout:on("receive", function(connout, payloadout)
		if (string.find(payloadout, "Status: 200 OK") ~= nil) then
			print("Posted OK");
		end
	end)

	connout:on("connection", function(connout, payloadout)

		print ("Posting...");

		local volt = node.readvdd33();		

		connout:send("GET /update?api_key=CHANNEL_API_KEY&field1=" .. (volt/1000) .. "." .. (volt%1000)
		.. " 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")
	end)

	connout:on("disconnection", function(connout, payloadout)
		connout:close();
		collectgarbage();
	end)

	connout:connect(80,'api.thingspeak.com')
end

tmr.alarm(1, 60000, 1, function() postThingSpeak(0) end)

40 thoughts on “Posting to Thingspeak with ESP8266 and NodeMCU”

    • Looks a bit like the project I just put final touches on… My board will also have a mains to 5V switching power supply on, and be made to fit inside a box, using the mounting holes in it. But more about that in the actual blog post. 😀

      Reply
    • I am too used to write PHP. 😉

      LUA does not mind them, so I just leave them in when the routine takes over and puts it at the end of a line.

      Reply
  1. doesn’t work. i got error message.

    > tmr.alarm(1, 60000, 1, function() postThingSpeak(0) end)
    > PANIC: unprotected error in call to Lua API (stdin:1: attempt to call global ‘postThingSpeak’ (a number value))
    [email protected]

    [email protected]??

    NodeMCU 0.9.6 build 20150406 powered by Lua 5.1.4
    lua: cannot open init.lua
    >

    i can’t find anything wrong as i am newbie in nodemcu and lua script. please guide me.

    Reply
    • Did you only copy that one line in with the timer?

      I just tried to copy the code onto one of my modules, to check if something got lost when putting it on the blog, but it works fine for me.

      Reply
  2. Hello Mickey,
    The post is useful.
    I tried using this code with my wifi credential and thingspeak channels.
    The code does not seem to update selected channel.
    I am using ESP12E module flashed with Node MCU v0.9.
    ESPlorer tool is the one pushing code on to the ESP12E module.
    Will you please be so kind as to suggest some pointers to help me?
    Best regards,

    Reply
  3. Great, just what I wanted. I also wanted to know, If I can capture data on the Serial Port and post it on thngspeak ?

    How difficult would that be.

    Reply
  4. Hello, thanks for this. It works fine just as expected.
    However I had to comment out the line:
    connout:close();
    It seems that ThingSpeak has already disconnected on its side when we reach this point. (After having received the recive OK and the disconnection messages, LUA says error not connected)
    Anyway after removing?commenting the line it works fine.

    Reply
  5. Hello, thanks for this. It works fine just as expected.
    However I had to comment out the line:
    connout:close();
    It seems that ThingSpeak has already disconnected on its side when we reach this point. (After having received the recive OK and the disconnection messages, LUA says error not connected)
    Anyway after removing?commenting the line it works fine.

    Reply
  6. Hello, thanks for this. It works fine just as expected.
    However I had to comment out the line:
    connout:close();
    It seems that ThingSpeak has already disconnected on its side when we reach this point. (After having received the recive OK and the disconnection messages, LUA says error not connected)
    Anyway after removing?commenting the line it works fine.

    Reply
  7. Great! Thanks! I’m lookink for 1 year any correct code for esp8266 but all was with mistakes. Good job, you make my day!

    Reply
  8. Great! Thanks! I’m lookink for 1 year any correct code for esp8266 but all was with mistakes. Good job, you make my day!

    Reply
  9. Great! Thanks! I’m lookink for 1 year any correct code for esp8266 but all was with mistakes. Good job, you make my day!

    Reply
  10. Hi, your solution worked for me perfectly! Thanks for the code but as someone has already pointed out, the connout:close() line has to be commented out in order to get NodeMCU to work properly (I got DNS errors earlier). Thanks!

    Reply
  11. Hi, your solution worked for me perfectly! Thanks for the code but as someone has already pointed out, the connout:close() line has to be commented out in order to get NodeMCU to work properly (I got DNS errors earlier). Thanks!

    Reply
  12. Hi, your solution worked for me perfectly! Thanks for the code but as someone has already pointed out, the connout:close() line has to be commented out in order to get NodeMCU to work properly (I got DNS errors earlier). Thanks!

    Reply

Leave a Comment