Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <MQTT.h>
  3. #include "Adafruit_MCP23017.h"
  4. #include <ArduinoJson.h>
  5. #include <Ticker.h>
  6.  
  7. WiFiClient espMQTT;
  8. MQTTClient client(1024);
  9. Ticker checkTimer;
  10.  
  11. Adafruit_MCP23017 mcpIN;
  12. Adafruit_MCP23017 mcpOUT;
  13.  
  14. unsigned long tIN = 0;
  15. unsigned long tOUT = 0;
  16.  
  17. // ------------------------------------ define ip ------------------------------------
  18. IPAddress ip(192, 168, 0, 50);
  19. IPAddress gateway(192, 168, 0, 1);
  20. IPAddress subnet(255, 255, 254, 0);
  21. IPAddress dns(8, 8, 8, 8);
  22. // -----------------------------------------------------------------------------------
  23.  
  24. const String TOPIC_OUT_R = "garage/output/read/";
  25. const String TOPIC_OUT_S = "garage/output/set";
  26. const String TOPIC_IN =    "garage/input/";
  27.  
  28. volatile uint16_t INvar = 0;
  29. volatile uint16_t OUTvar = 0xffff;
  30. volatile uint16_t OUTvar_old = 0xffff;
  31. volatile bool flag = false;
  32.  
  33. String payloadGlobal = "";
  34.  
  35. void setup() {
  36.   Serial.begin(115200);
  37.   WiFi.mode(WIFI_STA);
  38.   WiFi.config(ip, dns, gateway, subnet);
  39.   WiFi.begin(SSID0, PASS0);
  40.   //WiFi.begin(SSID1, PASS1);
  41.  
  42.   Serial.print("Connecting");
  43.   while (WiFi.status() != WL_CONNECTED) {
  44.     delay(250);
  45.     Serial.print(".");
  46.   }
  47.   Serial.println();
  48.   Serial.println("Connected!");
  49.  
  50.   Wire.setClock(400000L);
  51.  
  52.   mcpIN.begin();
  53.   mcpOUT.begin(1);
  54.  
  55.   for (byte i = 0; i < 16; i++) {
  56.     mcpOUT.pinMode(i, OUTPUT);
  57.     mcpIN.pinMode(i, INPUT);
  58.     mcpIN.pullUp(i, HIGH);
  59.   }
  60.   mcpOUT.writeGPIOAB(0xFFFF);
  61.  
  62.   Serial.println("Seting up MQTT");
  63.   client.begin(MQTT_SERVER_IP, 1883, espMQTT);
  64.   client.onMessage(callback);
  65.  
  66.   if (!client.connected()) {
  67.     connect();
  68.   }
  69.   //checkInputs();
  70.  
  71.   checkTimer.attach_ms(100, check);
  72.  
  73.   Serial.println("setup done");
  74. }
  75.  
  76. void loop() {
  77.   if (!client.loop()) { //get MQTT in and out off air, save payload in global
  78.     connect(); //something fucked up, fix it
  79.   }
  80.   processMQTT(); //process the global payload
  81. }
  82.  
  83. void check() {
  84.   checkInputs(); //check inputs and post them into MQTT
  85.   if (flag) { //there is a change in output buffer
  86.     flag = false;
  87.     mcpOUT.writeGPIOAB(OUTvar); //write to MCP23017 via I2C
  88.     Serial.println("MCP write");
  89.   }
  90.   updateOutputs(); //post changes in output to MQTT
  91.   Serial.print(".");
  92. }
  93.  
  94. void checkInputs() {
  95.   uint16_t ins = mcpIN.readGPIOAB();
  96.   if (ins != INvar) {
  97.     for (byte x = 0; x < 16; x++) {
  98.       if (bitRead(ins, x) != bitRead(INvar, x)) {
  99.         String top = TOPIC_IN;
  100.         String pay = "";
  101.         top += String(x);
  102.         if (bitRead(ins, x)) pay = F("OFF");
  103.         else pay = F("ON");
  104.         client.publish(top, pay);
  105.       }
  106.     }
  107.     INvar = ins;
  108.   }
  109. }
  110.  
  111. void callback(String & topic, String & payload) {
  112.   payloadGlobal = payload;
  113.   Serial.println(payload);
  114. }
  115.  
  116. void processMQTT() {
  117.   if (payloadGlobal) {
  118.     StaticJsonBuffer<100> jsonBuffer;
  119.     JsonObject& root = jsonBuffer.parseObject(payloadGlobal);
  120.     if (root.success()) {
  121.       int relay = root["relay"];
  122.       bool state = root["state"];
  123.       bitWrite(OUTvar, relay, !state);
  124.       flag = true;
  125.       Serial.println("MQTT processed");
  126.     }
  127.     jsonBuffer.clear();
  128.   }
  129.   payloadGlobal = "";
  130. }
  131.  
  132. void updateOutputs() {
  133.   if (OUTvar != OUTvar_old) {
  134.     for (byte x = 0; x < 16; x++) {
  135.       if (bitRead(OUTvar, x) != bitRead(OUTvar_old, x)) {
  136.         String top = "";
  137.         String pay = "";
  138.         bool state = !bitRead(OUTvar, x);
  139.         top += TOPIC_OUT_R;
  140.         top += String(x);
  141.         if (state) {
  142.           pay = F("ON");
  143.         }
  144.         else {
  145.           pay = F("OFF");
  146.         }
  147.         client.publish(top, pay);
  148.         Serial.print("outputs updated:");
  149.         Serial.println(top + " " + pay);
  150.       }
  151.     }
  152.     OUTvar_old = OUTvar;
  153.   }
  154. }
  155.  
  156. void connect() {
  157.   Serial.println("Connecting to MQTT");
  158.   while (!client.connect("GarageV10.2b")) {
  159.     if (!client.connected()) {
  160.       Serial.print(".");
  161.       delay(1000);
  162.     }
  163.     else {
  164.       Serial.println("Connected!");
  165.     }
  166.   }
  167.   Serial.println("Subscribing...");
  168.   client.subscribe(TOPIC_OUT_S);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement