Hola. tengo otro problema, despues de solucionar uno.
tengo el modulo con rele(el baratito) y me esta funcionando mediante node red(el broker mqtt lo tengo en la raspberry pi). como tengo un pin libre(gpio2) quero añadirle un pulsador para poder activar y desactivar el rele de los dos partes( mediante node red o mediante pulsador). el programa lo he hecho en SCL y me funciona(lo he probado en sysmac studio) el problema es que peleo para pasarlo a arduino. no soy un experto en arduino, yo soy programador plc, pero me gustaria poder hacerlo. os subire el codigo SCL y un pequeño video para que veais como quero que funcione. aparte que subire el codigo tambien. En el SCL, la variable Arduino es lo que me viene de node red(0 u 1), la variable pulsador es el pin GPIO2 y la variable Led es el pin GPIO1. las otras variables son internas.
codigo:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "";
const char* password = "***";
const char mqtt_server = "192.168.1.13"; /// example 192.168.0.19
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(0, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
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) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*
- }*
- Serial.println();*
- // Switch on the LED if an 1 was received as first character*
- if ((char)payload[0] == '0') {*
- Serial.println("LOW");*
- digitalWrite(0, LOW); // Turn the LED on (Note that LOW is the voltage level*
- // but actually the LED is on; this is because*
- // it is acive low on the ESP-01)*
- }*
if ((char)payload[0] == '1') { - Serial.println("HIGH");*
- digitalWrite(0, HIGH); // Turn the LED off by making the voltage HIGH*
- }*
}
void reconnect() { - // Loop until we're reconnected*
- while (!client.connected()) {*
- Serial.print("Attempting MQTT connection...");*
- // Attempt to connect*
- if (client.connect("ESP8266Client1")) {*
- Serial.println("connected");*
- // Once connected, publish an announcement...*
- client.publish("persoanamas", "conected");*
- // ... and resubscribe*
- client.subscribe("persianamas");*
- } else {*
- Serial.print("failed, rc=");*
- Serial.print(client.state());*
- Serial.println(" try again in 5 seconds");*
- // Wait 5 seconds before retrying*
- delay(5000);*
- }*
- }*
}
void loop() { - if (!client.connected()) {*
- reconnect();*
- }*
- client.loop();*
- long now = millis();*
- if (now - lastMsg > 2000) {*
- lastMsg = now;*
- ++value;*
- snprintf (msg, 75, "conected #%ld", value);*
- Serial.print("Publish message: ");*
- Serial.println(msg);*
- client.publish("persianamas", msg);*
- }*
}
codigo SCL:
Android <> AuxAndroid THEN - Pulso:=TRUE;*
ELSE - Pulso:=FALSE;*
END_IF;
AuxAndroid := Android;
IF Pulso = TRUE AND Pulsador = FALSE AND Led = FALSE THEN - AuxLed:=TRUE;*
END_IF;
IF Pulso = TRUE AND Pulsador = FALSE AND Led = TRUE THEN - AuxLed:=FALSE;*
END_IF;
IF Pulso = FALSE AND Pulsador = TRUE AND AuxLed = FALSE THEN - AuxLed:= TRUE;*
END_IF;
IF Pulso = FALSE AND Pulsador = TRUE AND Led = TRUE THEN - AuxLed:= FALSE;*
END_IF;
Led:= AuxLed;