Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3403da5bce | ||
|
|
2ed7394d3a |
@@ -13,3 +13,4 @@ platform = espressif8266
|
|||||||
board = nodemcuv2
|
board = nodemcuv2
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
lib_deps = knolleary/PubSubClient@^2.8
|
||||||
|
|||||||
127
src/main.cpp
127
src/main.cpp
@@ -1,27 +1,126 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
#define LED 2
|
#define LED 2
|
||||||
|
#define DOWN D1
|
||||||
|
#define UP D2
|
||||||
|
#define STOP D3
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef STASSID
|
||||||
|
#define STASSID "CurrywurstPommesMajo"
|
||||||
|
#define STAPSK "1976071219770620"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length);
|
||||||
|
void wifiSetup();
|
||||||
|
void mqttSetup();
|
||||||
|
|
||||||
|
void initiateAction(int);
|
||||||
|
|
||||||
|
void wifiSetup() {
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(STASSID, STAPSK);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("Connected! IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void mqttReconnect() {
|
||||||
|
while (!client.connected()) {
|
||||||
|
Serial.println("Connecting to MQTT...");
|
||||||
|
|
||||||
|
if (client.connect("ESP8266Client", "mosqhd", "ieN2K7YtGgkK" )) {
|
||||||
|
|
||||||
|
Serial.println("connected");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.publish("esp32/test", "Hello from ESP8266");
|
||||||
|
client.subscribe("esp32/output");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void initiateAction(int act) {
|
||||||
|
|
||||||
|
Serial.print("Action ");
|
||||||
|
Serial.println(act);
|
||||||
|
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
digitalWrite(act, HIGH);
|
||||||
|
delay(500);
|
||||||
|
digitalWrite(LED, HIGH);
|
||||||
|
digitalWrite(act, LOW);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic, byte* message, unsigned int length) {
|
||||||
|
Serial.print("Message arrived on topic: ");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print(". Message: ");
|
||||||
|
String messageTemp;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < length; i++) {
|
||||||
|
Serial.print((char)message[i]);
|
||||||
|
messageTemp += (char)message[i];
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
if (String(topic) == "esp32/output") {
|
||||||
|
Serial.print("Changing output to ");
|
||||||
|
if(messageTemp == "up"){
|
||||||
|
Serial.println("up");
|
||||||
|
initiateAction(UP);
|
||||||
|
}
|
||||||
|
else if(messageTemp == "down"){
|
||||||
|
Serial.println("down");
|
||||||
|
initiateAction(DOWN);
|
||||||
|
}
|
||||||
|
else if (messageTemp == "stop") {
|
||||||
|
Serial.println("stop");
|
||||||
|
initiateAction(STOP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// put function declarations here:
|
|
||||||
int myFunction(int, int);
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
pinMode(LED, OUTPUT);
|
pinMode(LED, OUTPUT);
|
||||||
|
pinMode(DOWN, OUTPUT);
|
||||||
|
pinMode(UP, OUTPUT);
|
||||||
|
pinMode(STOP, OUTPUT);
|
||||||
|
digitalWrite(LED, LOW);
|
||||||
|
digitalWrite(DOWN, LOW);
|
||||||
|
digitalWrite(UP, LOW);
|
||||||
|
digitalWrite(STOP, LOW);
|
||||||
|
wifiSetup();
|
||||||
|
client.setServer("muckibude.fritz.box", 1883);
|
||||||
|
client.setCallback(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
|
||||||
myFunction(1,2);
|
if (!client.connected()) {
|
||||||
|
mqttReconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// put function definitions here:
|
|
||||||
int myFunction(int x, int y) {
|
|
||||||
digitalWrite(LED, HIGH);
|
|
||||||
Serial.println("LED is on");
|
|
||||||
delay(1000);
|
|
||||||
digitalWrite(LED, LOW);
|
|
||||||
Serial.println("LED is off");
|
|
||||||
delay(1000);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user