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

User avatar
By gmarocco
#24281 Hy, based on the web examples I have written a script that receives four measures from an external microcontroller and posts them on thingspeak.com. The program works fine when the external microcontroller sends integre values. But when it sends floating values I get the following error "readserial1.lua:36: attempt to concatenate global 'f3' (a nil value))".

On Lua loader I tried to send the following commands print(tonumber"12.3") and print(tonumber"12,3") but in both cases I received the "nil" value.

Below I post the script

f1=0
f2=0
f3=0
f4=0
--f5=0
--f6=0
received_value=0

gpio.write(5,gpio.HIGH) -- ready to receive data GPIO14

uart.on("data", "\r",
function(data)
print("receive:", data)
received_value=received_value+1
tmr.delay(100)
--print("received values="..received_value)
if(received_value==1) then
f1=tonumber(data)
tmr.delay(100)
elseif(received_value==2) then
f2=tonumber(data)
tmr.delay(100)
elseif(received_value==3) then
f3=tonumber(data)
tmr.delay(100)
elseif(received_value==4) then
f4=tonumber(data)
tmr.delay(100)
-- elseif(received_value==5) then
--f5=tonumber(data)
--tmr.delay(100)
--elseif(received_value==6)then
--f6=tonumber(data)
tmr.delay(100)
gpio.write(5,gpio.LOW) -- the other MCU stop to send data
print(f1..f2..f3..f4)
received_value=0
print("Sending data to thingspeak.com"..f1)
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=9OE2BHBVL1RWFNR7&field1="..f1.."&field2="..f2.."&field3="..f3.."&field4="..f4.."&field5="..f5.."&field6="..f6.." HTTP/1.1\r\n")
conn:send("GET /update?key=9OE2BHBVL1RWFNR7&field1="..f1.."&field2="..f2.."&field3="..f3.."&field4="..f4.." 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")
tmr.delay(100)
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
tmr.delay(500)
gpio.write(5,gpio.HIGH)
tmr.delay(500)
if wifi.sta.getip()== nil then
node.restart()
end

end
-- conection to thingspeak.com

end, 0)
[/i][/i]