force update
This commit is contained in:
parent
5f26731a3f
commit
f3d60b9adb
|
@ -1,21 +1,17 @@
|
||||||
#include <ESP8266WiFi.h> // Enables the ESP8266 to connect to the local network (via WiFi)
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h> // Allows us to connect to, and publish to the MQTT broker
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// WiFi
|
// WiFi
|
||||||
// Make sure to update this for your own WiFi network!
|
|
||||||
const char *ssid = "internet_of_insecure_things";
|
const char *ssid = "internet_of_insecure_things";
|
||||||
const char *wifi_password = "pass";
|
const char *wifi_password = "iamnotacrook";
|
||||||
|
|
||||||
// MQTT
|
// MQTT
|
||||||
const char *clientID = "window_0"; // Unique to this device
|
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_server = "mqtt.kitsunehosting.net";
|
||||||
const char *mqtt_topic = "home/upstairs_lab/actuators/window_0";
|
const char *mqtt_topic = "home/upstairs_lab/actuators/window_0";
|
||||||
const char *mqtt_username = "user";
|
const char *mqtt_username = "admin";
|
||||||
const char *mqtt_password = "pass";
|
const char *mqtt_password = "iamnotacrook";
|
||||||
|
|
||||||
// Initialise the WiFi and MQTT Client objects
|
// Initialise the WiFi and MQTT Client objects
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
|
@ -23,10 +19,10 @@ PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(ledPin, OUTPUT);
|
// pinMode(ledPin, OUTPUT);
|
||||||
|
|
||||||
// Switch the on-board LED off to start with
|
// Switch the on-board LED off to start with
|
||||||
digitalWrite(ledPin, HIGH);
|
// digitalWrite(ledPin, HIGH);
|
||||||
|
|
||||||
// Begin Serial on 115200
|
// Begin Serial on 115200
|
||||||
// Remember to choose the correct Baudrate on the Serial monitor!
|
// Remember to choose the correct Baudrate on the Serial monitor!
|
||||||
|
@ -46,14 +42,17 @@ void setup()
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugging - Output the IP Address of the ESP8266
|
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
// Connect to MQTT Broker
|
// Configure client callback
|
||||||
// client.connect returns a boolean value to let us know if the connection was successful.
|
client.setCallback(callback);
|
||||||
// If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
|
}
|
||||||
|
|
||||||
|
void reconnect()
|
||||||
|
{
|
||||||
|
/* Reconnects to the MQTT Server */
|
||||||
if (client.connect(clientID, mqtt_username, mqtt_password))
|
if (client.connect(clientID, mqtt_username, mqtt_password))
|
||||||
{
|
{
|
||||||
Serial.println("Connected to MQTT Broker!");
|
Serial.println("Connected to MQTT Broker!");
|
||||||
|
@ -62,26 +61,32 @@ void setup()
|
||||||
{
|
{
|
||||||
Serial.println("Connection to MQTT Broker failed...");
|
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()
|
void loop()
|
||||||
{
|
{
|
||||||
digitalWrite(ledPin, LOW);
|
delay(600);
|
||||||
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(400);
|
// digitalWrite(ledPin, HIGH);
|
||||||
|
|
||||||
digitalWrite(ledPin, HIGH);
|
|
||||||
if (client.publish(mqtt_topic, "1"))
|
if (client.publish(mqtt_topic, "1"))
|
||||||
{
|
{
|
||||||
Serial.println("Button pushed and message sent!");
|
Serial.println("Button pushed and message sent!");
|
||||||
|
@ -89,10 +94,12 @@ void loop()
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
|
Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
|
||||||
client.connect(clientID, mqtt_username, mqtt_password);
|
reconnect();
|
||||||
delay(10); // This delay ensures that client.publish doesn't clash with the client.connect call
|
delay(10);
|
||||||
client.publish(mqtt_topic, "-1");
|
client.publish(mqtt_topic, "-1");
|
||||||
}
|
}
|
||||||
|
|
||||||
delay(400);
|
tone(5, 1200, 300);
|
||||||
|
|
||||||
|
client.loop(); // Runs cleanup, etc on client
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#define WINDOW_CLOSE = 5
|
||||||
|
#define WINDOW_OPEN = 6
|
Binary file not shown.
Loading…
Reference in New Issue