Created
February 6, 2020 21:32
-
-
Save rafalw/3bf4cf8db87e102c79ec5d518055e0d3 to your computer and use it in GitHub Desktop.
ESP8266 (ESP-01, NodeMCU 1.5.4.1-final) + Blynk (lokalny serwer) + 2*PCF8574 (I2C/TWI -> 2*8 i/O) = Smart Home (no, nie do końca – chyba, że podłączymy 16 przekaźników...)
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
dofile("libPCF8574.lua") | |
-- V0 - układ PCF8574 pod adresem addr_1 | |
-- V1 - układ PCF8574 pod adresem addr_2 | |
local Blynk = require("bl_nodemcu") | |
local config = { | |
auth = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", -- token aplikacji i urządzenia | |
ssid = "xxxxxxxxxxxxxxxxxxxx", | |
pwd = "xxxxxxxxxxxxxxxxxxxx", | |
} | |
blynk = Blynk.new(config.auth, { | |
heartbeat = 10, -- default h-beat is 30 | |
--log = print, | |
}) | |
local function connectBlynk() | |
local host = "192.168.0.2" | |
local sock, port | |
print("Connecting local Blynk server...") | |
sock = net.createConnection(net.TCP) | |
port = 8088 -- port, pod którym dostępny jest lokalny serwer Blynk (bez szyfrowania) | |
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() | |
print("IP / Mask / Gateway:") | |
print(wifi.sta.getip()) | |
connectBlynk() | |
end) | |
wifi.sta.eventMonStart() | |
wifi.sta.config(config) | |
blynk:on("connected", function(ping) | |
print("Ready. Ping: "..math.floor(ping*1000).."ms") | |
-- synchroniazcja portów wirtualnych V0, V1 | |
blynk:syncVirtual(0) -- IC1 | |
blynk:syncVirtual(1) -- IC2 | |
end) | |
blynk:on("disconnected", function() | |
print("Disconnected.") | |
-- Próba ponownego połączenia z serwerem Blynk | |
connectBlynk() | |
end) | |
-- callback dla zmian dla V0 | |
blynk:on("V0", function(param) | |
local state = tonumber(param[1]) | |
-- proste zabezpieczenie przed wartoscią | |
-- parametru poza bajtem | |
if (state > 255) then | |
state = 255 | |
end | |
if (state < 0) then | |
state = 0 | |
end | |
state = 255 - state | |
print("-> V0: " .. state) | |
write_pcf8574(addr_1, state) | |
end) | |
-- callback dla zmian V1 | |
blynk:on("V1", function(param) | |
local state = tonumber(param[1]) | |
-- proste zabezpieczenie przed wartoscią | |
-- parametru poza bajtem | |
if (state > 255) then | |
state = 255 | |
end | |
if (state < 0) then | |
state = 0 | |
end | |
state = 255 - state | |
print("-> V1: " .. state) | |
write_pcf8574(addr_2, state) | |
end) | |
-- callback dla żądania wartości V0 | |
blynk:on("readV0", function(param) | |
local value = read_pcf8574(addr_1) | |
blynk:virtualWrite(0, string.byte(value)) | |
print("<- V0: " .. value) | |
end) | |
-- callback dla żądania wartości V1 | |
blynk:on("readV1", function(param) | |
local value = read_pcf8574(addr_2) | |
blynk:virtualWrite(1, string.byte(value)) | |
print("<- V1: " .. value) | |
end) | |
-- Blynk housekeeping | |
local periodic = tmr.create() | |
periodic:alarm(1000, tmr.ALARM_AUTO, function() | |
blynk:run() | |
end) |
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
-- Biblioteka obslugi ekspanderow I2C na plytce prototypowej | |
-- R. Wileczek, 2015 - 2016 | |
-- Uklady PCF8574 dostepne sa pod adresami addr_1 i addr_2 | |
busid = 0 -- I2C Bus ID. Zawsze rowny zero | |
sda= 4 -- GPIO2 pin mapping is 4 | |
scl= 3 -- GPIO0 pin mapping is 3 | |
addr_1=0x38 -- adresy ekspanderow I2C | |
addr_2=0x39 | |
-- Konfiguracja magistrali I2C. | |
i2c.setup(busid,sda,scl,i2c.SLOW) | |
-- Odczyt danych z pcf8574 | |
function read_pcf8574(dev_addr) | |
i2c.start(busid) | |
i2c.address(busid, dev_addr , i2c.RECEIVER) | |
bdata = i2c.read(busid,1) -- Odczyt pojedynczego bajtu | |
i2c.stop(busid) | |
return bdata | |
end | |
-- Zapis danych do pcf8574 (1 bajt) | |
function write_pcf8574(dev_addr, value) | |
i2c.start(busid) | |
i2c.address(busid, dev_addr, i2c.TRANSMITTER) | |
i2c.write(busid,value) | |
i2c.stop(busid) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment