Connecting your ESP to Thingspeak with a program written in your Arduino IDE is quite easy and those programs come a dime a dozen.
However, when I went to look for a program that did the same in LUA I came a cross a number of programs that simply didn’t work.
So I had to give it a go myself.
The below program is more or less a skeleton for thingspeak connection. It reads two values and posts those on Thingspeak. As it was just for testing I didnt want to add a lot of sensors, so th eonly two values read are the Internal reference voltage and the analog value from ADC0.
To make the program easily adaptable, I readthe sensors in a seperate function.
If you want to add a variable resistor to test the ADC, mind you that it can only take 1 Volt input so you need a voltage divider that limits it to 1 volt instead of 3.3 Volt.
When using the program dont forget your network credentials and Thingspeak API Key
WRITEKEY="T3I6T9YF67JE527" -- set your thingspeak.com key wifi.setmode(wifi.STATION) wifi.sta.config("YourSSID","YourPW") wifi.sta.connect() tmr.delay(1000000) volt=0 --Internal reference voltage ana=0 -- analog port A0 Mind you, this one has a 1 Volt max --read sensor function ReadSensor() volt = node.readvdd33() ana=adc.read(0) -- 1V=1023 print("Internal Ref.Voltage: " ..(volt/1000).."."..(volt%1000).." Volt") print("Analoog: "..ana) end -- send to https://api.thingspeak.com function sendTS() conn = nil conn = net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload)success = true print(payload)end) conn:on("connection", function(conn, payload) print("Connected") conn:send('GET /update?key='..WRITEKEY..'&field1='..(volt/1000)..'.'..(volt%1000)..'&field2='..ana..'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,'18.104.22.168') end ReadSensor() sendTS() tmr.alarm(1,6000,1,function()ReadSensor()sendTS()end)
Now I haven’t been completely honest here, as it isnt really good practice to read the internal Voltage and the A0 port as it reads either the one or the other.
When using the A0 to read internal voltage in the Arduino IDE rather than in LUA ADC_MODE(ADC_VCC); in combination with ESP.getVcc() is now the correct way to do it. Using
readvdd33 causes problems