171 lines
4.4 KiB
C++
171 lines
4.4 KiB
C++
#ifdef CORE_DEBUG_LEVEL
|
|
#undef CORE_DEBUG_LEVEL
|
|
#endif
|
|
|
|
#define CORE_DEBUG_LEVEL 3
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
|
|
|
#include <WiFi.h>
|
|
#include <ArduinoJson.h>
|
|
#include <HTTPClient.h>
|
|
#include <base64.h>
|
|
#include <ESPmDNS.h>
|
|
#include <Servo.h>
|
|
#include <PubSubClient.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
|
|
// WiFi Info
|
|
const char* ssid = "Unknown Network";
|
|
const char* password = "HDUUpdcbEc3w";
|
|
|
|
// MQTT
|
|
const char *mqtt_broker = "192.168.0.42";
|
|
const char *topic = "esp32/switch";
|
|
const int mqtt_port = 1883;
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient mqttClient(espClient);
|
|
long lastMsg = 0;
|
|
char msg[50];
|
|
int value = 0;
|
|
|
|
// Servo Vars
|
|
int lightPin = 13;
|
|
int pos = 0;
|
|
|
|
Servo leftServo;
|
|
Servo rightServo;
|
|
|
|
void callback(char* topic, byte* message, unsigned int length) {
|
|
Serial.print("Message arrived on topic: ");
|
|
Serial.print(topic);
|
|
Serial.print(". Message: ");
|
|
String messageTemp;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
Serial.print((char)message[i]);
|
|
messageTemp += (char)message[i];
|
|
}
|
|
Serial.println();
|
|
|
|
// Feel free to add more if statements to control more GPIOs with MQTT
|
|
|
|
// If a message is received on the topic esp32/switch, you check if the message is either "on" or "off".
|
|
// Changes the output state according to the message
|
|
if (String(topic) == "esp32/switch") {
|
|
Serial.print("Changing switch to ");
|
|
if(messageTemp == "on"){
|
|
Serial.println("lights on");
|
|
leftServo.write(15);
|
|
rightServo.write(30);
|
|
}
|
|
else if(messageTemp == "off"){
|
|
Serial.println("lights off");
|
|
leftServo.write(30);
|
|
rightServo.write(15);
|
|
}
|
|
}
|
|
}
|
|
|
|
void setupWifi() {
|
|
delay(10);
|
|
// We start by connecting to a WiFi network
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
Serial.println("Connecting");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.print("Connected to WiFi network with IP Address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void setupOTA(){
|
|
// Arduino OTA Info
|
|
// Port defaults to 3232
|
|
// Hostname defaults to esp3232-[MAC]
|
|
ArduinoOTA.setPasswordHash("b2cb7bf46d7afe5ad2ed16d87093d342");
|
|
ArduinoOTA
|
|
.onStart([]() {
|
|
String type;
|
|
if (ArduinoOTA.getCommand() == U_FLASH)
|
|
type = "sketch";
|
|
else // U_SPIFFS
|
|
type = "filesystem";
|
|
|
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
|
Serial.println("Start updating " + type);
|
|
})
|
|
.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
})
|
|
.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
})
|
|
.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("Starting up");
|
|
|
|
setupWifi();
|
|
setupOTA();
|
|
|
|
Serial.print("Subscribing to ");
|
|
Serial.println(mqtt_broker);
|
|
mqttClient.setServer(mqtt_broker, 1883);
|
|
mqttClient.subscribe(topic);
|
|
mqttClient.setCallback(callback);
|
|
|
|
Serial.println("Attaching servo at pin 13");
|
|
leftServo.attach(13);
|
|
Serial.println("Attaching servo at pin 12");
|
|
rightServo.attach(12);
|
|
|
|
leftServo.write(0);
|
|
rightServo.write(45);
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!mqttClient.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (mqttClient.connect("ESP8266Client")) {
|
|
Serial.println("connected");
|
|
// Subscribe
|
|
mqttClient.subscribe(topic);
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(mqttClient.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
if (!mqttClient.connected()) {
|
|
reconnect();
|
|
}
|
|
|
|
mqttClient.loop();
|
|
}
|