forked from nodemcu/nodemcu-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip_example.lua
73 lines (58 loc) · 2.09 KB
/
gossip_example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- need a wifi connection
-- enter your wifi credentials
local credentials = {SSID = "SSID", PASS = "PASS"};
-- push a message onto the network
-- this can also be done by changing gossip.networkState[gossip.ip].data = {temperature = 78};
local function sendAlarmingData()
Gossip.pushGossip({temperature = 78});
print('Pushed alarming data');
end
local function removeAlarmingData()
Gossip.pushGossip(nil);
print('Removed alarming data from the network.');
end
-- callback function for when gossip receives an update
local function treatAlarmingData(updateData)
for k in pairs(updateData) do
if updateData[k].data then
if updateData[k].data.temperature and updateData[k].data.temperature > 30 then
print('Warning, the temp is above 30 degrees at ' .. k);
end
end
end
end
local function Startup()
-- initialize all nodes with the seed except for the seed itself
-- eventually they will all know about each other
-- enter at least one ip that will be a start seed
local startingSeed = '192.168.0.73';
-- luacheck: push allow defined
Gossip = require('gossip');
-- luacheck: pop
local config = {debug = true, seedList = {}};
if wifi.sta.getip() ~= startingSeed then
table.insert(config.seedList, startingSeed);
end
Gossip.setConfig(config);
-- add the update callback
Gossip.updateCallback = treatAlarmingData;
-- start gossiping
Gossip.start();
-- send some alarming data timer
if wifi.sta.getip() == startingSeed then
tmr.create():alarm(50000, tmr.ALARM_SINGLE, sendAlarmingData);
tmr.create():alarm(50000*3, tmr.ALARM_SINGLE, removeAlarmingData);
end
end
local function startExample()
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED,
function() print('Diconnected') end);
print("Connecting to WiFi access point...");
if wifi.sta.getip() == nil then
wifi.setmode(wifi.STATION);
wifi.sta.config({ssid = credentials.SSID, pwd = credentials.PASS});
end
print('Ip: ' .. wifi.sta.getip() .. '. Starting in 5s ..');
tmr.create():alarm(5000, tmr.ALARM_SINGLE, Startup);
end
startExample();