Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <MQTT.h>
- #include "Adafruit_MCP23017.h"
- #include <ArduinoJson.h>
- #include <Ticker.h>
- WiFiClient espMQTT;
- MQTTClient client(1024);
- Ticker checkTimer;
- Adafruit_MCP23017 mcpIN;
- Adafruit_MCP23017 mcpOUT;
- unsigned long tIN = 0;
- unsigned long tOUT = 0;
- // ------------------------------------ define ip ------------------------------------
- IPAddress ip(192, 168, 0, 50);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 254, 0);
- IPAddress dns(8, 8, 8, 8);
- // -----------------------------------------------------------------------------------
- const String TOPIC_OUT_R = "garage/output/read/";
- const String TOPIC_OUT_S = "garage/output/set";
- const String TOPIC_IN = "garage/input/";
- volatile uint16_t INvar = 0;
- volatile uint16_t OUTvar = 0xffff;
- volatile uint16_t OUTvar_old = 0xffff;
- volatile bool flag = false;
- String payloadGlobal = "";
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.config(ip, dns, gateway, subnet);
- WiFi.begin(SSID0, PASS0);
- //WiFi.begin(SSID1, PASS1);
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(250);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("Connected!");
- Wire.setClock(400000L);
- mcpIN.begin();
- mcpOUT.begin(1);
- for (byte i = 0; i < 16; i++) {
- mcpOUT.pinMode(i, OUTPUT);
- mcpIN.pinMode(i, INPUT);
- mcpIN.pullUp(i, HIGH);
- }
- mcpOUT.writeGPIOAB(0xFFFF);
- Serial.println("Seting up MQTT");
- client.begin(MQTT_SERVER_IP, 1883, espMQTT);
- client.onMessage(callback);
- if (!client.connected()) {
- connect();
- }
- //checkInputs();
- checkTimer.attach_ms(100, check);
- Serial.println("setup done");
- }
- void loop() {
- if (!client.loop()) { //get MQTT in and out off air, save payload in global
- connect(); //something fucked up, fix it
- }
- processMQTT(); //process the global payload
- }
- void check() {
- checkInputs(); //check inputs and post them into MQTT
- if (flag) { //there is a change in output buffer
- flag = false;
- mcpOUT.writeGPIOAB(OUTvar); //write to MCP23017 via I2C
- Serial.println("MCP write");
- }
- updateOutputs(); //post changes in output to MQTT
- Serial.print(".");
- }
- void checkInputs() {
- uint16_t ins = mcpIN.readGPIOAB();
- if (ins != INvar) {
- for (byte x = 0; x < 16; x++) {
- if (bitRead(ins, x) != bitRead(INvar, x)) {
- String top = TOPIC_IN;
- String pay = "";
- top += String(x);
- if (bitRead(ins, x)) pay = F("OFF");
- else pay = F("ON");
- client.publish(top, pay);
- }
- }
- INvar = ins;
- }
- }
- void callback(String & topic, String & payload) {
- payloadGlobal = payload;
- Serial.println(payload);
- }
- void processMQTT() {
- if (payloadGlobal) {
- StaticJsonBuffer<100> jsonBuffer;
- JsonObject& root = jsonBuffer.parseObject(payloadGlobal);
- if (root.success()) {
- int relay = root["relay"];
- bool state = root["state"];
- bitWrite(OUTvar, relay, !state);
- flag = true;
- Serial.println("MQTT processed");
- }
- jsonBuffer.clear();
- }
- payloadGlobal = "";
- }
- void updateOutputs() {
- if (OUTvar != OUTvar_old) {
- for (byte x = 0; x < 16; x++) {
- if (bitRead(OUTvar, x) != bitRead(OUTvar_old, x)) {
- String top = "";
- String pay = "";
- bool state = !bitRead(OUTvar, x);
- top += TOPIC_OUT_R;
- top += String(x);
- if (state) {
- pay = F("ON");
- }
- else {
- pay = F("OFF");
- }
- client.publish(top, pay);
- Serial.print("outputs updated:");
- Serial.println(top + " " + pay);
- }
- }
- OUTvar_old = OUTvar;
- }
- }
- void connect() {
- Serial.println("Connecting to MQTT");
- while (!client.connect("GarageV10.2b")) {
- if (!client.connected()) {
- Serial.print(".");
- delay(1000);
- }
- else {
- Serial.println("Connected!");
- }
- }
- Serial.println("Subscribing...");
- client.subscribe(TOPIC_OUT_S);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement