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 "window_control_settings.h"
#define window_name "window_0"
#define mqtt_root "home/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.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);
@ -41,7 +38,7 @@ void setup_wifi() {
while (WiFi.status() != WL_CONNECTED) {
delay(950);
tone(2, 1200, 50);
tone(buzzer, 1200, 50);
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];
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();
}
@ -93,9 +90,9 @@ void loop() {
client.loop();
// 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);
client.subscribe(mqtt_root "/in");
tone(2, 2000, 500);
client.subscribe(MQTT_ROOT "/in");
tone(buzzer, 2000, 500);
delay(1000);
}

View File

@ -5,3 +5,6 @@
#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