diff --git a/window_control/firmware/firmware.ino b/window_control/firmware/firmware.ino index aefe531..d99e687 100644 --- a/window_control/firmware/firmware.ino +++ b/window_control/firmware/firmware.ino @@ -1,21 +1,17 @@ -#include // Enables the ESP8266 to connect to the local network (via WiFi) -#include // Allows us to connect to, and publish to the MQTT broker - -const int ledPin = 0; // This code uses the built-in led for visual feedback that the button has been pressed -const int buttonPin = 13; // Connect your button to pin #13 +#include +#include // WiFi -// Make sure to update this for your own WiFi network! const char *ssid = "internet_of_insecure_things"; -const char *wifi_password = "pass"; +const char *wifi_password = "iamnotacrook"; // MQTT const char *clientID = "window_0"; // Unique to this device -// Make sure to update this for your own MQTT Broker! + const char *mqtt_server = "mqtt.kitsunehosting.net"; const char *mqtt_topic = "home/upstairs_lab/actuators/window_0"; -const char *mqtt_username = "user"; -const char *mqtt_password = "pass"; +const char *mqtt_username = "admin"; +const char *mqtt_password = "iamnotacrook"; // Initialise the WiFi and MQTT Client objects WiFiClient wifiClient; @@ -23,10 +19,10 @@ PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port void setup() { - pinMode(ledPin, OUTPUT); + // pinMode(ledPin, OUTPUT); // Switch the on-board LED off to start with - digitalWrite(ledPin, HIGH); + // digitalWrite(ledPin, HIGH); // Begin Serial on 115200 // Remember to choose the correct Baudrate on the Serial monitor! @@ -46,14 +42,17 @@ void setup() Serial.print("."); } - // Debugging - Output the IP Address of the ESP8266 Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); - // Connect to MQTT Broker - // client.connect returns a boolean value to let us know if the connection was successful. - // If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable) + // Configure client callback + client.setCallback(callback); +} + +void reconnect() +{ + /* Reconnects to the MQTT Server */ if (client.connect(clientID, mqtt_username, mqtt_password)) { Serial.println("Connected to MQTT Broker!"); @@ -62,26 +61,32 @@ void setup() { Serial.println("Connection to MQTT Broker failed..."); } + + client.publish("test", "0"); + + // What to listen for + client.subscribe("test"); +} + +void callback(char *topic, byte *payload, unsigned int length) +{ + Serial.print("Message arrived in topic: "); + Serial.println(topic); + + Serial.print("Message:"); + for (int i = 0; i < length; i++) + { + Serial.print((char)payload[i]); + } + + Serial.println(); } void loop() { - digitalWrite(ledPin, LOW); - if (client.publish(mqtt_topic, "0")) - { - Serial.println("Button pushed and message sent!"); - } - else - { - Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again"); - client.connect(clientID, mqtt_username, mqtt_password); - delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call - client.publish(mqtt_topic, "-1"); - } + delay(600); - delay(400); - - digitalWrite(ledPin, HIGH); + // digitalWrite(ledPin, HIGH); if (client.publish(mqtt_topic, "1")) { Serial.println("Button pushed and message sent!"); @@ -89,10 +94,12 @@ void loop() else { Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again"); - client.connect(clientID, mqtt_username, mqtt_password); - delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call + reconnect(); + delay(10); client.publish(mqtt_topic, "-1"); } - delay(400); + tone(5, 1200, 300); + + client.loop(); // Runs cleanup, etc on client } diff --git a/window_control/firmware/pinout.h b/window_control/firmware/pinout.h new file mode 100644 index 0000000..e01cc53 --- /dev/null +++ b/window_control/firmware/pinout.h @@ -0,0 +1,2 @@ +#define WINDOW_CLOSE = 5 +#define WINDOW_OPEN = 6 diff --git a/window_control/models/Automatic Gearbox Electronics Mount.FCStd b/window_control/models/Automatic Gearbox Electronics Mount.FCStd index fd75f12..93f4258 100644 Binary files a/window_control/models/Automatic Gearbox Electronics Mount.FCStd and b/window_control/models/Automatic Gearbox Electronics Mount.FCStd differ