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

User avatar
By WilliamMcC
#12349 Here is the code I wrote to get wind speed using an interrupt and send data once a minute to Thingspeak. It was written for a maplin sensor. I am having some problems with the code so it is not complete. I have posted this under lua examples. Seems to be a timing issue when wind speed is high. Not sure if it is debounce or timing issue. Anyway here is the code. I also got a very good init.lua to work with this (I think from this forum). I connects to your wifi and then call dofile("YOUR Code Here"). Sorry don't have the credit for this one.
Code: Select all-- Measure wind speed and post data to thingspeak.com
-- by William H. McClintic
     cnt=0
     rp=0
     pin=4
     du=0
     st=0
     ws=0
    gpio.mode(pin,gpio.INPUT)
    function onStart()
    if cnt == 0 then st=tmr.now()
    end
    rp=gpio.read(pin)
    --print(rp)
    cnt=cnt+1
    if cnt == 10 then et=tmr.now()
    du=et-st
    ws=(60-(du/30000))
    ws=ws/2
    if ws<1 then ws=0
    end
   print("Wind Speed mph is "..ws)   
    cnt=0
    du=0
    st=0
    et=0
    end
    end
     gpio.mode(pin,gpio.INT)
    gpio.trig(pin, 'down', onStart)
--- Get wind speed and send data to thingspeak.com
function sendData()
-- 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=YOURAPIKEYHERE&field1="..(ws).." 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
-- send data every X ms to thing speak 10,000 = 10 seconds
tmr.alarm(0, 60000, 1, function() sendData() end
)


Here is the init lua to connect to your local wi-fi and then run your code. I did not write this but I will give proper credit as it works very well.

Code: Select all--init.lua

print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("YOURSSID","YOURPASSWORD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("SendWind.lua")
end
end)