Compare commits
4 Commits
4a4ddc1d28
...
795ac28caa
Author | SHA1 | Date |
---|---|---|
|
795ac28caa | |
|
75c456f9e2 | |
|
7ca6199fd6 | |
|
74a5ff2d1a |
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -3,54 +3,43 @@
|
|||
// Include the mqtt client lib
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Include any secrets.
|
||||
#include "window_control_secrets.h"
|
||||
// Include the settings.h.
|
||||
#include "window_control_settings.h"
|
||||
|
||||
#define wifi_ssid SECRET_SSID
|
||||
#define wifi_password SECRET_PASS
|
||||
|
||||
#define mqtt_server "mqtt.kitsunehosting.net"
|
||||
#define mqtt_port 1883
|
||||
#define mqtt_user SECRET_MQTT_USER
|
||||
#define mqtt_password SECRET_MQTT_PASS
|
||||
|
||||
|
||||
#define window_name "window_0"
|
||||
#define mqtt_root "/house/upstairs_lab/actuators/" window_name
|
||||
|
||||
#define in_topic "/testing/in"
|
||||
#define out_topic "/testing/out"
|
||||
// Replace by 2 if you aren't enable to use Serial Monitor... Don't forget to Rewire R1 to GPIO2!
|
||||
#define in_led 2
|
||||
#define buzzer 2
|
||||
#define motorForward 1
|
||||
#define motorReverse 0
|
||||
#define sense 3
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
client.setClient(espClient);
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
client.setCallback(callback);
|
||||
|
||||
// initialize digital pin LED_BUILTIN as an output.
|
||||
pinMode(in_led, OUTPUT);
|
||||
digitalWrite(in_led, HIGH);
|
||||
// Initalize some pins!
|
||||
// Initalize buzzer pin
|
||||
pinMode(buzzer, OUTPUT);
|
||||
digitalWrite(buzzer, LOW);
|
||||
}
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.println();
|
||||
WiFi.hostname("ESP-" window_name);
|
||||
WiFi.hostname("ESP-" WINDOW_NAME);
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(wifi_ssid);
|
||||
Serial.println(WIFI_SSID);
|
||||
|
||||
WiFi.begin(wifi_ssid, wifi_password);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.println("Not yet connected.. Waiting 500ms to check again..");
|
||||
delay(950);
|
||||
tone(buzzer, 1200, 50);
|
||||
Serial.println("Not yet connected.. Waiting 1s to check again..");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
|
@ -64,7 +53,7 @@ void reconnect() {
|
|||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
|
||||
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
||||
Serial.println("connected");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
|
@ -84,22 +73,26 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||
char receivedChar = (char)payload[i];
|
||||
Serial.print(receivedChar);
|
||||
if (receivedChar == '0')
|
||||
digitalWrite(in_led, LOW);
|
||||
//digitalWrite(in_led, LOW);
|
||||
if (receivedChar == '1')
|
||||
digitalWrite(in_led, HIGH);
|
||||
//digitalWrite(in_led, HIGH);
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// If we ever disconnect, reconnect.
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
|
||||
// Run the client loop
|
||||
client.loop();
|
||||
|
||||
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
|
||||
client.publish(out_topic, String(random(2)).c_str(), true);
|
||||
client.publish(MQTT_ROOT "/random", String(random(2)).c_str(), true);
|
||||
delay(1000);
|
||||
client.subscribe(in_topic);
|
||||
tone(2, 2000, 500);
|
||||
client.subscribe(MQTT_ROOT "/in");
|
||||
tone(buzzer, 2000, 500);
|
||||
delay(1000);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#define WIFI_SSID "muner"
|
||||
#define WIFI_PASS "apollo11"
|
||||
|
||||
#define MQTT_SERVER "mqtt.kitsunehosting.net"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "device"
|
||||
#define MQTT_PASS "iamnotacrook"
|
||||
|
||||
#define WINDOW_NAME "window_0"
|
||||
#define MQTT_ROOT "home/upstairs_lab/actuators/" WINDOW_NAME
|
Loading…
Reference in New Issue