Ühenduse katkestamiseks on tarviline lasta oranz juhe läbi relee
ESP juhtimine on juba maitseasi, kas otse Blynkist või Blynk->Nodered->MQTT->ESP
Milleks seda vaja oli ?
Krt. seda teab, igavusest vist.
Metsavend vaatas mind veidi imelikult, küllap ei tahtnud halvasti ütelda ...
/***** ESP01a *********/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "sdfgsfdger";
const char* pass = "rtdgfgfdfd";
const char* mqtt_server = "hhh.jjj.öö.pp"; // Raspberry IP
WiFiClient kunde_1;
PubSubClient client(kunde_1);
const int pin = 0; //GPIO0
// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
if(topic=="yyy"){
Serial.print("Changing ...: ");
if(messageTemp == "1"){
digitalWrite(pin, LOW);
}
else if(messageTemp == "0"){
digitalWrite(pin, HIGH);
}
}
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
while (!client.connected()) { // Loop until we're reconnected
if (client.connect("kunde_1")) {
// Subscribe or resubscribe to a topic. You can subscribe to more topics
client.subscribe("yyy");
} else {
delay(5000);
}
}
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function -receives messages and controls pins
void setup() {
pinMode(pin, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("kunde_1");
}