Update buzzer code.

This commit is contained in:
Kenwood 2021-04-27 22:43:26 -04:00
parent 7ca6199fd6
commit 75c456f9e2
2 changed files with 18 additions and 18 deletions

View File

@ -6,34 +6,31 @@
// Include the settings.h. // Include the settings.h.
#include "window_control_settings.h" #include "window_control_settings.h"
#define window_name "window_0" #define buzzer 2
#define mqtt_root "home/upstairs_lab/actuators/" window_name #define motorForward 1
#define motorReverse 0
#define in_topic "/testing/in" #define sense 3
#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
WiFiClient espClient; WiFiClient espClient;
PubSubClient client; PubSubClient client;
void setup() { void setup() {
Serial.begin(115200);
setup_wifi(); setup_wifi();
client.setClient(espClient); client.setClient(espClient);
client.setServer(MQTT_SERVER, MQTT_PORT); client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback); client.setCallback(callback);
// initialize digital pin LED_BUILTIN as an output. // Initalize some pins!
pinMode(in_led, OUTPUT); // Initalize buzzer pin
digitalWrite(in_led, HIGH); pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
} }
void setup_wifi() { void setup_wifi() {
delay(10); delay(10);
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
Serial.println(); Serial.println();
WiFi.hostname("ESP-" window_name); WiFi.hostname("ESP-" WINDOW_NAME);
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.println(WIFI_SSID); Serial.println(WIFI_SSID);
@ -41,7 +38,7 @@ void setup_wifi() {
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(950); delay(950);
tone(2, 1200, 50); tone(buzzer, 1200, 50);
Serial.println("Not yet connected.. Waiting 1s to check again.."); Serial.println("Not yet connected.. Waiting 1s to check again..");
} }
@ -76,9 +73,9 @@ void callback(char* topic, byte* payload, unsigned int length) {
char receivedChar = (char)payload[i]; char receivedChar = (char)payload[i];
Serial.print(receivedChar); Serial.print(receivedChar);
if (receivedChar == '0') if (receivedChar == '0')
digitalWrite(in_led, LOW); //digitalWrite(in_led, LOW);
if (receivedChar == '1') if (receivedChar == '1')
digitalWrite(in_led, HIGH); //digitalWrite(in_led, HIGH);
} }
Serial.println(); Serial.println();
} }
@ -93,9 +90,9 @@ void loop() {
client.loop(); client.loop();
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2)) // Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
client.publish(mqtt_root "/random", String(random(2)).c_str(), true); client.publish(MQTT_ROOT "/random", String(random(2)).c_str(), true);
delay(1000); delay(1000);
client.subscribe(mqtt_root "/in"); client.subscribe(MQTT_ROOT "/in");
tone(2, 2000, 500); tone(buzzer, 2000, 500);
delay(1000); delay(1000);
} }

View File

@ -5,3 +5,6 @@
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define MQTT_USER "device" #define MQTT_USER "device"
#define MQTT_PASS "iamnotacrook" #define MQTT_PASS "iamnotacrook"
#define WINDOW_NAME "window_0"
#define MQTT_ROOT "home/upstairs_lab/actuators/" WINDOW_NAME