Skip to content

Instantly share code, notes, and snippets.

@jeanfbrito
Created January 6, 2016 18:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeanfbrito/5c983f0841e1939e1078 to your computer and use it in GitHub Desktop.
Save jeanfbrito/5c983f0841e1939e1078 to your computer and use it in GitHub Desktop.
NodeMCU and InfluxDB
-- Your access point's SSID and password
local SSID = "greenhouse"
local SSID_PASSWORD = "senhasupersecreta"
local DEVICE = "device001"
local temperature = 27.5
-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)
local HOST = "192.168.13.91"
local URI = "/write?db=greenhouse"
function build_post_request(host, uri, data_table)
local data = data_table.Data_type .. ",device=" .. data_table.Device .. " " .. "value=" .. data_table.Value
--for param,value in pairs(data_table) do
-- data = data .. param.."="..value.."&"
-- end
print(data)
request = "POST "..uri.." HTTP/1.1\r\n"..
"Host: "..host.."\r\n"..
"Connection: close\r\n"..
"Content-Type: application/x-www-form-urlencoded\r\n"..
"Content-Length: "..string.len(data).."\r\n"..
"\r\n"..
data
print(request)
return request
end
local function display(sck,response)
print(response)
end
-- When using send_sms: the "from" number HAS to be your twilio number.
-- If you have a free twilio account the "to" number HAS to be your twilio verified number.
-- The numbers MUST include the country code.
-- DO NOT add the "+" sign.
local function send_data(data_type,device,value)
local data = {
Data_type = data_type,
Device = device,
Value = value
}
socket = net.createConnection(net.TCP,0)
socket:on("receive",display)
socket:connect(8086,HOST)
socket:on("connection",function(sck)
local post_request = build_post_request(HOST,URI,data)
sck:send(post_request)
end)
end
function check_wifi()
local ip = wifi.sta.getip()
if(ip==nil) then
print("Connecting...")
else
tmr.stop(0)
print("Connected to AP!")
print(ip)
--send_data("15551234567","12223456789","Hello from your ESP8266")
local t, h = getTempHumi()
print("Temp:"..t .." C\n")
print("Humi:"..h .." RH\n")
send_data("temperature", DEVICE, t)
send_data("humidity", DEVICE, h)
tmr.alarm(0,10000,1,check_wifi)
end
end
tmr.alarm(0,2000,1,check_wifi)
function getTempHumi()
pin = 4
local status,temp,humi,temp_decimial,humi_decimial = dht.read(pin)
if( status == dht.OK ) then
-- Float firmware using this example
print("DHT Temperature:"..temp..";".."Humidity:"..humi)
elseif( status == dht.ERROR_CHECKSUM ) then
print( "DHT Checksum error." );
elseif( status == dht.ERROR_TIMEOUT ) then
print( "DHT Time out." );
end
return temp, humi
end
@wnasich
Copy link

wnasich commented Jul 31, 2016

thanks @jeanbrito, this file was a source of inspiration for this project -> https://github.com/wnasich/nodemcu-collector

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment