Dotaz na změnu programu
Napsal: 24 čer 2020, 19:47
Dobrý den,
potřebuji od Vás pomoc.
Našel jsem program pro spínání relé za pomoci wemos d1 esp8266. Spínání je realizováno přes MQTT (Loxone - nodered - wemos).
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxx";
const char* mqtt_server = "10.0.0.27";
WiFiClient espClient;
PubSubClient client(espClient);
int HeatingPin = 15;
String switch1;
String strTopic;
String strPayload;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
strTopic = String((char*)topic);
if(strTopic == "ha/switch1")
{
switch1 = String((char*)payload);
if(switch1 == "ON")
{
Serial.println("ON");
digitalWrite(HeatingPin, HIGH);
}
else
{
Serial.println("OFF");
digitalWrite(HeatingPin, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("ha/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(HeatingPin, OUTPUT);
digitalWrite(HeatingPin, HIGH);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
Spínání 1 relé za pomocí tohoto programu (program je určen pro spínání 1 výstupu) je OK. Vše funguje. Moje otázka zní, jak upravit program, abych mohl spínat další relé (výstupy), tj. dvě relé samostatně.
Programovat bohužel neumím a tak budu rád za radu, popřípadě upravený program. Klidně si za to zaplatím. Velice děkuji.
potřebuji od Vás pomoc.
Našel jsem program pro spínání relé za pomoci wemos d1 esp8266. Spínání je realizováno přes MQTT (Loxone - nodered - wemos).
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxx";
const char* mqtt_server = "10.0.0.27";
WiFiClient espClient;
PubSubClient client(espClient);
int HeatingPin = 15;
String switch1;
String strTopic;
String strPayload;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
strTopic = String((char*)topic);
if(strTopic == "ha/switch1")
{
switch1 = String((char*)payload);
if(switch1 == "ON")
{
Serial.println("ON");
digitalWrite(HeatingPin, HIGH);
}
else
{
Serial.println("OFF");
digitalWrite(HeatingPin, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("ha/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(HeatingPin, OUTPUT);
digitalWrite(HeatingPin, HIGH);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
Spínání 1 relé za pomocí tohoto programu (program je určen pro spínání 1 výstupu) je OK. Vše funguje. Moje otázka zní, jak upravit program, abych mohl spínat další relé (výstupy), tj. dvě relé samostatně.
Programovat bohužel neumím a tak budu rád za radu, popřípadě upravený program. Klidně si za to zaplatím. Velice děkuji.