Created
February 6, 2020 21:22
-
-
Save rafalw/6519da87f3a9d50c750996d590e15286 to your computer and use it in GitHub Desktop.
ESP8266 (ESP-01, NodeMCU 1.5.4.1-final) + Blynk + IFTTT = Smart Home (cz. 3 – ciągle jeszcze przekaźnik...)
This file contains 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
local Blynk = require("bl_nodemcu") | |
local config = { | |
auth = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX", -- token aplikacji | |
ssid = "xxxxxxxxxxxxxxxxxxxx", -- SSID sieci, z którą łączy się sterownik | |
pwd = "xxxxxxxxxxxxxxxxxxxx", -- hasło do sieci WiFi | |
} | |
blynk = Blynk.new(config.auth, { | |
heartbeat = 10, -- default h-beat is 30 | |
--log = print, | |
}) | |
-- LED - miga podczas nawiązywania połączenia; jesli połączenie jest nawiązane, świeci ciągłym światłem | |
local led_test = 1 | |
local initial_tmr = tmr.create() | |
initial_tmr:alarm(200, tmr.ALARM_AUTO, function() | |
if led_test == 1 then | |
led_test = 0 | |
gpio.write(3, gpio.LOW) | |
else | |
led_test = 1 | |
gpio.write(3, gpio.HIGH) | |
end | |
end) | |
local function connectBlynk() | |
local host = "blynk-cloud.com" | |
local sock, port | |
print("Connecting Blynk...") | |
sock = net.createConnection(net.TCP) | |
port = 80 | |
sock:on("connection", function(s) blynk:connect(s) end) | |
sock:connect(port, host) | |
end | |
-- Połączenie z WiFi | |
print("Connecting WiFi...") | |
wifi.setmode(wifi.STATION) | |
wifi.sta.eventMonReg(wifi.STA_GOTIP, function() | |
initial_tmr:stop() | |
gpio.write(3, gpio.LOW) | |
print("IP / Mask / Gateway:") | |
print(wifi.sta.getip()) | |
connectBlynk() | |
end) | |
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function(previous_State) | |
if(previous_State == wifi.STA_GOTIP) then | |
initial_tmr:start() | |
end | |
end) | |
wifi.sta.eventMonStart() | |
wifi.sta.config(config) | |
blynk:on("connected", function(ping) | |
print("Ready. Ping: "..math.floor(ping*1000).."ms") | |
-- Po nawiązaniu połączenia synchronizacja portów wirtualnych | |
blynk:syncVirtual(0) -- przekaźnik | |
blynk:syncVirtual(1) -- tweet (nie do końca z sensem ze względu na charakter kontrolki w aplikacji) | |
end) | |
blynk:on("disconnected", function() | |
print("Disconnected.") | |
-- automatyczna próba ponownego połączenia | |
connectBlynk() | |
end) | |
-- callback dla zmian na V0 | |
blynk:on("V0", function(param) | |
print("V0:", tonumber(param[1]), "RELAY") | |
local state = tonumber(param[1]) | |
if state == 1 then | |
gpio.write(4, gpio.LOW) | |
else | |
gpio.write(4, gpio.HIGH) | |
end | |
end) | |
-- callback dla zmian na V1` | |
blynk:on("V1", function(param) | |
print("V1:", tonumber(param[1]), "TWEET") | |
local state = tonumber(param[1]) | |
if state == 1 then | |
-- print("Tweet me!") | |
blynk:tweet("Hello from #ESP8266 #IoT #Blynk remote relay device :D") | |
else | |
print("No tweet...") | |
end | |
end) | |
-- Blynk housekeeping | |
local periodic = tmr.create() | |
periodic:alarm(1000, tmr.ALARM_AUTO, function() | |
blynk:run() | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment