Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. -- Config Variables --
  2. DEVICE_NAME = "front-door"
  3. WIFI_NAME = "xxxxxxxx"
  4. WIFI_KEY = "xxxxxxxx"
  5. MQTT_IP = "192.168.1.xxx"
  6. MQTT_PORT = 1883
  7. MQTT_TOPIC = "sensors"
  8.  
  9. -- Internal Variables --
  10. WIFI_STATUS = 255
  11. SENSOR_STATUS = 1
  12. BROKER_STATUS = 0
  13.  
  14. BROKER = mqtt.Client(DEVICE_NAME .. "-sensor", 120)
  15.  
  16. -- Translation Tables --
  17. WIFI_STATUS_TABLE = {}
  18. WIFI_STATUS_TABLE[wifi.STA_IDLE] = "Idle"
  19. WIFI_STATUS_TABLE[wifi.STA_CONNECTING] = "Connecting..."
  20. WIFI_STATUS_TABLE[wifi.STA_WRONGPWD] = "Auth Error"
  21. WIFI_STATUS_TABLE[wifi.STA_APNOTFOUND] = "AP Not Found"
  22. WIFI_STATUS_TABLE[wifi.STA_FAIL] = "Failure"
  23. WIFI_STATUS_TABLE[wifi.STA_GOTIP] = "Connected"
  24. WIFI_STATUS_TABLE[255] = "Disabled"
  25.  
  26. SENSOR_STATUS_TABLE = {}
  27. SENSOR_STATUS_TABLE[0] = "OFF"
  28. SENSOR_STATUS_TABLE[1] = "ON"
  29.  
  30. BROKER_STATUS_TABLE = {}
  31. BROKER_STATUS_TABLE[0] = "Disconnected"
  32. BROKER_STATUS_TABLE[1] = "Connected"
  33.  
  34. -- MQTT Management --
  35. function broker_published()
  36.     print("MQTT: Published Update")
  37. end
  38.  
  39. function broker_connect()
  40.     BROKER_STATUS = 1
  41.     print("MQTT Status: " .. BROKER_STATUS_TABLE[BROKER_STATUS])
  42.     BROKER:publish(MQTT_TOPIC .. "/" .. DEVICE_NAME, SENSOR_STATUS_TABLE[SENSOR_STATUS], 0, 0, broker_published)
  43. end
  44.  
  45. function broker_offline()
  46.     BROKER_STATUS = 0
  47.     print("MQTT Status: " .. BROKER_STATUS_TABLE[BROKER_STATUS])
  48.     BROKER:connect(MQTT_IP, MQTT_PORT, 0, nil)
  49. end
  50.  
  51. -- Main Loop --
  52. function main()
  53.     if wifi.sta.status() ~= WIFI_STATUS then
  54.         WIFI_STATUS = wifi.sta.status()
  55.         if WIFI_STATUS == wifi.STA_GOTIP then
  56.             print("MQTT Status: Connecting...")
  57.             BROKER:connect(MQTT_IP, MQTT_PORT, 0, nil)
  58.         end
  59.         print("WiFi Status: " .. WIFI_STATUS_TABLE[WIFI_STATUS])
  60.     end
  61.     if WIFI_STATUS == wifi.STA_GOTIP then
  62.         if SENSOR_STATUS ~= gpio.read(1) then
  63.             SENSOR_STATUS = gpio.read(1)
  64.             print("Sensor Status: " .. SENSOR_STATUS_TABLE[SENSOR_STATUS])
  65.             if BROKER_STATUS ~= 0 then
  66.                 BROKER:publish(MQTT_TOPIC .. "/" .. DEVICE_NAME, SENSOR_STATUS_TABLE[SENSOR_STATUS],0, 0, broker_published)
  67.             end
  68.         end
  69.     end
  70.     tmr.alarm(1,500,0,main)
  71. end
  72.  
  73. -- Init Code --
  74. -- Wifi Config
  75. config = {}
  76. config.ssid = WIFI_NAME
  77. config.pwd = WIFI_KEY
  78. config.connected_cb = connected
  79. wifi.setmode(wifi.STATION)
  80. wifi.sta.sethostname(DEVICE_NAME .. "-sensor")
  81. wifi.sta.config(config)
  82. -- GPIO Config
  83. gpio.mode(1, gpio.INPUT, gpio.PULLUP)
  84. -- Broker Config
  85. BROKER:on("connect", broker_connect)
  86. BROKER:on("offline", broker_offline)
  87. -- Loop Entry
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement