Created
February 1, 2016 04:41
-
-
Save jeanfbrito/afa9764118062a9b7f55 to your computer and use it in GitHub Desktop.
Estufa v0.3
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 timesRunned = 0 | |
SDA_PIN = 6 -- sda pin, GPIO12 | |
SCL_PIN = 5 -- scl pin, GPIO14 | |
-- 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") | |
mqtt_sub() | |
end) | |
mqtt:on("offline", function(con) | |
--print(tmr.now()) | |
print ("Mqtt Reconnecting...") | |
tmr.alarm(1, 1000, 0, function() | |
m:connect(HOST, 1883, 0, function(conn) | |
print("Mqtt Connected to:" .. HOST) | |
mqtt_sub() --run the subscription function | |
end) | |
end) | |
end) | |
--mqtt:on("message", function(conn, topic, data) | |
-- print(topic .. ":" ) | |
-- if data ~= nil then | |
-- print(data) | |
-- end | |
--end) | |
-- on publish message receive event | |
mqtt:on("message", function(conn, topic, data) | |
print("Recieved:" .. topic .. ":" .. data) | |
if (data=="ON") then | |
print("Enabling Output") | |
gpio.write(1,gpio.HIGH) | |
mqtt:publish("greenhouse/actuators/" .. DEVICE .. "/led/state","ON",0,0) | |
elseif (data=="OFF") then | |
print("Disabling Output") | |
gpio.write(1,gpio.LOW) | |
mqtt:publish("greenhouse/actuators/" .. DEVICE .. "/led/state","OFF",0,0) | |
else | |
print("Invalid - Ignoring") | |
end | |
end) | |
function mqtt_sub() | |
mqtt:subscribe("greenhouse/actuators/" .. DEVICE .. "/led/control",0, function(conn) | |
print("Mqtt Subscribed to OpenHAB feed for device " .. DEVICE) | |
end) | |
end | |
mqtt:connect(HOST, 1883, 0, function(conn) | |
print("Connected to broker") | |
mqtt_sub() | |
-- 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 | |
local lux = 0 | |
bh1750 = require("bh1750") | |
bh1750.init(SDA_PIN, SCL_PIN) | |
bh1750.read() | |
lux = bh1750.getlux() | |
lux = lux / 100 | |
lux = tonumber(string.format("%.2f",lux)) | |
--print(lux) | |
-- release module | |
bh1750 = nil | |
package.loaded["bh1750"]=nil | |
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) | |
mqtt:publish("sensors/" .. DEVICE .. "/luminosity",lux,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