// This sketch works fine with Node-RED broker at... 192.168.1.146 #include #include #include #include #include "config.h" WiFiClient espClient; PubSubClient client(espClient); const int blueLED = 2; //D4 on Wemos const int L289_C = 13; //D7 on Wemos const int L289_D = 12; //D6 on Wemos //------------------------------ void setup() { //------------------------------ Serial.begin(9600); pinMode(blueLED, OUTPUT); pinMode(L289_C, OUTPUT); pinMode(L289_D, OUTPUT); digitalWrite(L289_C, LOW); //These are the input pins on the L289 H-Bridge digitalWrite(L289_D, LOW); // Make sure motor is stopped at the start setup_wifi(); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); } //----------------------------- //------------------------------ void loop() { //------------------------------ if (!client.connected()) { reconnect_mqtt(); } client.loop(); } //------------------------------ //----------------------------- void callback(char* mqtt_topic, byte* message, unsigned int length) { //----------------------------- // // All the clever work is done in here // Serial.print("Message arrived on topic: "); Serial.println(mqtt_topic); StaticJsonDocument<256> doc; deserializeJson(doc, message, length); String gateID = doc["gateID"]; String cmd = doc["cmd"]; Serial.print("GateID = "); Serial.print(gateID); Serial.print(" Command = "); Serial.println(cmd); if (gateID == gateLocation) { // gateLocation is defined in config.h if (cmd == "stop") { digitalWrite(L289_C, LOW); //These are the input pins on the L289 H-Bridge digitalWrite(L289_D, LOW); Serial.println("C=L D=L"); digitalWrite(blueLED, HIGH); // GPIO-2 on the Wemos Used for testing // Serial.println("BlueLED is OFF"); } else if (cmd == "open") { digitalWrite(L289_C, LOW); //These are the input pins on the L289 H-Bridge digitalWrite(L289_D, HIGH); Serial.println("C=L D=H"); digitalWrite(blueLED, LOW); // GPIO-2 on the Wemos Used for testing // Serial.println("BlueLED is ON"); } else if (cmd == "close") { digitalWrite(L289_C, HIGH); //These are the input pins on the L289 H-Bridge digitalWrite(L289_D, LOW); Serial.println("C=H D=L"); digitalWrite(blueLED, LOW); // GPIO-2 on the Wemos Used for testing // Serial.println("BlueLED is ON"); } else { Serial.println("nothing recognised"); } } } //----------------------------- //----------------------------- void setup_wifi() { //----------------------------- delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(WiFi_ssid); // WiFi.hostname(WiFi_hostname); WiFi.begin(WiFi_ssid, WiFi_password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("WiFi connected to "); Serial.println(WiFi.SSID()); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } //----------------------------- //----------------------------- void reconnect_mqtt() { //----------------------------- // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println(" connected"); // Subscribe client.subscribe(mqtt_topic); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } //------------------------------