Last active
January 25, 2016 17:23
-
-
Save jeanfbrito/619198e95fd5ff15be41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Your access point's SSID and password | |
local SSID = "greenhouse" | |
local SSID_PASSWORD = "senhasupersecreta" | |
local DEVICE = "undefined" | |
local temperature = 27.5 | |
local timesRunned = 0 | |
-- configure ESP as a station | |
wifi.setmode(wifi.STATION) | |
wifi.sta.config(SSID,SSID_PASSWORD) | |
wifi.sta.autoconnect(1) | |
DEVICE = string.gsub(wifi.sta.getmac(), ":", "") | |
local HOST = "159.203.103.139" | |
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") | |
-- initiate the mqtt client and set keepalive timer to 120sec | |
mqtt = mqtt.Client("DEVICE", 120, "", "") | |
mqtt:on("connect", function(con) print ("connected") end) | |
mqtt:on("offline", function(con) print ("offline") end) | |
mqtt:on("message", function(conn, topic, data) | |
print(topic .. ":" ) | |
if data ~= nil then | |
print(data) | |
end | |
end) | |
mqtt:connect(HOST, 1883, 0, function(conn) | |
print("Connected to broker") | |
-- subscribe topic with qos = 0 | |
mqtt:subscribe("sensors/" .. DEVICE .. "/#",0, function(conn) | |
-- publish a message with data = my_message, QoS = 0, retain = 0 | |
-- mqtt:publish("sensors/device002/temperature","67.5",0,0, function(conn) | |
-- print("sent") | |
-- end) | |
end) | |
end) | |
tmr.alarm(1,10000,1,sendData) | |
end | |
end | |
function sendData() | |
local t, h = getTempHumi() | |
local n = node.heap() | |
local times = timesRunned | |
timesRunned = timesRunned + 1 | |
mqtt:publish("sensors/" .. DEVICE .. "/temperature",t,0,0, function(conn) | |
--print("sent") | |
end) | |
mqtt:publish("sensors/" .. DEVICE .. "/humidity",h,0,0, function(conn) | |
--print("sent") | |
end) | |
mqtt:publish("sensors/" .. DEVICE .. "/runned",times,0,0, function(conn) | |
--print("sent") | |
end) | |
mqtt:publish("sensors/" .. DEVICE .. "/heap",n,0,0, function(conn) | |
--print("sent") | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment