Open nickfysiek opened 4 years ago
You have confused pinMode and digitalWrite! You have to use pinMode in setup function (currently there is no pinMode!), and digitalWrite in your on/off function!
sivar thx i did this i put this in setup pinMode(relay, OUTPUT);
and then these
digitalWrite(relay, LOW); digitalWrite(relay, HIGH); and also const int relay = 5;
now it is working, how does it come that the previous code worked with a four module relay?
I don't know your previous code. But i'm sure you will find out.
Hi, I have the same problem with the relays this is my code:
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
const int relayPin1 = 2; const int relayPin2 = 3;
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void turnOn(String deviceId) {
if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(relayPin1, LOW); // turn on relay1 with voltage HIGH
}
else if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
digitalWrite(relayPin2, LOW); // turn on relay2 with voltage HIGH
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(relayPin1, HIGH); //turn off relay with voltage LOW
}
else if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
digitalWrite(relayPin2, HIGH); //turn off relay2 with voltage LOW
}
else {
Serial.print("Turn off for unknown device id: ");
Serial.println(deviceId);
}
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
break;
case WStype_CONNECTED: {
isConnected = true;
Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
Serial.printf("Waiting for commands from sinric.com ...\n");
}
break;
case WStype_TEXT: {
Serial.printf("[WSc] get text: %s\n", payload);
// Example payloads
// For Switch types
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}}
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject((char*)payload);
DynamicJsonDocument json(1024);
deserializeJson(json, (char*) payload);
String deviceId = json ["deviceId"];
String action = json ["action"];
if(action == "action.devices.commands.OnOff") { // Switch
String value = json ["value"]["on"];
Serial.println(value);
if(value == "true") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else if (action == "test") {
Serial.println("[WSc] received test command from sinric.com");
}
}
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
break;
default: break;
} }
void setup() { Serial.begin(115200);
pinMode(relayPin1, OUTPUT); //colocar o modo de saida nos PIN's do microcontrolador pinMode(relayPin2, OUTPUT); //colocal o modo de saida nos PIN's do microcontrolador
WiFiMulti.addAP(MySSID, MyWifiPassword); Serial.println(); Serial.print("Connecting to Wifi: "); Serial.println(MySSID);
// Waiting for Wifi connect while(WiFiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } if(WiFiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFi connected. "); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }
// server address, port and URL webSocket.begin("iot.sinric.com", 80, "/"); //"iot.sinric.com", 80
// event handler webSocket.onEvent(webSocketEvent); webSocket.setAuthorization("apikey", MyApiKey);
// try again every 5000ms if connection has failed webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets }
void loop() { webSocket.loop();
if(isConnected) { uint64_t now = millis();
// Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
heartbeatTimestamp = now;
webSocket.sendTXT("H");
}
}
}
I don't see the problem in the code, but in my nodeMCU v3 the code is uploaded the serial in the arduino IDE respond with the correct commands the relay on the digital pin2 light up the esp8266 blue led when is on and turn of the esp8266 led when is off, but the relay attached to the pin's don't work and I don't know why.
Can somebody please help
Can you turn on/off the relay using a simple sketch?
On Thu, 11 Jun 2020 at 5:06 PM Jose Lapao notifications@github.com wrote:
Hi, I have the same problem with the relays this is my code:
include
include
include
include
// https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
include
// https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
include
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
define MyApiKey "MyApiKey" // TODO: Change to your sinric API Key.
Your API Key is displayed on sinric.com dashboard
define MySSID "MySSID" // TODO: Change to your Wifi network SSID
define MyWifiPassword "MyWifiPassword" // TODO: Change to your Wifi
network password
define HEARTBEAT_INTERVAL 300000 // 5 Minutes
const int relayPin1 = 2; const int relayPin2 = 3;
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void turnOn(String deviceId) { if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relayPin1, LOW); // turn on relay1 with voltage HIGH } else if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); digitalWrite(relayPin2, LOW); // turn on relay2 with voltage HIGH } else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId); } }
void turnOff(String deviceId) { if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of first device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relayPin1, HIGH); //turn off relay with voltage LOW } else if (deviceId == "5exxxxxxxxxxxxxxxxxxxxxxxxx") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); digitalWrite(relayPin2, HIGH); //turn off relay2 with voltage LOW } else { Serial.print("Turn off for unknown device id: "); Serial.println(deviceId); } }
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: isConnected = false; Serial.printf("[WSc] Webservice disconnected from sinric.com!\n"); break; case WStype_CONNECTED: { isConnected = true; Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n"); } break; case WStype_TEXT: { Serial.printf("[WSc] get text: %s\n", payload); // Example payloads
// For Switch types // {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff // {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}}
if ARDUINOJSON_VERSION_MAJOR == 5
DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject((char*)payload);
endif
if ARDUINOJSON_VERSION_MAJOR == 6
DynamicJsonDocument json(1024); deserializeJson(json, (char*) payload);
endif
String deviceId = json ["deviceId"]; String action = json ["action"];
if(action == "action.devices.commands.OnOff") { // Switch String value = json ["value"]["on"]; Serial.println(value); if(value == "true") { turnOn(deviceId); } else { turnOff(deviceId); } } else if (action == "test") { Serial.println("[WSc] received test command from sinric.com"); }
} break; case WStype_BIN: Serial.printf("[WSc] get binary length: %u\n", length); break; default: break;
} }
void setup() { Serial.begin(115200);
pinMode(relayPin1, OUTPUT); //colocar o modo de saida nos PIN's do microcontrolador pinMode(relayPin2, OUTPUT); //colocal o modo de saida nos PIN's do microcontrolador
WiFiMulti.addAP(MySSID, MyWifiPassword); Serial.println(); Serial.print("Connecting to Wifi: "); Serial.println(MySSID);
// Waiting for Wifi connect while(WiFiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } if(WiFiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFi connected. "); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }
// server address, port and URL webSocket.begin("iot.sinric.com", 80, "/"); //"iot.sinric.com", 80
// event handler webSocket.onEvent(webSocketEvent); webSocket.setAuthorization("apikey", MyApiKey);
// try again every 5000ms if connection has failed webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets }
void loop() { webSocket.loop();
if(isConnected) { uint64_t now = millis();
// Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) { heartbeatTimestamp = now; webSocket.sendTXT("H"); }
} }
I don't see the problem in the code, but in my nodeMCU v3 the code is uploaded the serial in the arduino IDE respond with the correct commands the relay on the digital pin2 light up the esp8266 blue led when is on and turn of the esp8266 led when is off, but the relay attached to the pin's don't work and I don't know why.
Can somebody please help
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kakopappa/sinric/issues/415#issuecomment-642547798, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZQGLKNE5CNC7L2RKK3RWCUBHANCNFSM4MQZOWGQ .
Hi @kakopappa
This is a good question I'm don't have much experience with the nodeMCU and the esp's stuff, I have some codes for normal arduinos nothing with webserver or webpages, but I google for a simple sketch for do that and yes I can turn on/off the relay using the sketch I found, but I find is:
CODE---MCU
pin0---D3 pin1---TX pin2---D4 pin3---RX
The sketch if found is for ESP01 with 4 relay channel, now I put in my 2 devices the pin 0 and 2, and connect the relay's in the D3 and D4 and work.
So the problem is identify the correct pinout of the MCU.
I am using this code with succes to power my coffee machine but it has 4 relais. I bought one because this is enough and now it's not working. When i use a basic sketch to check the relais, it is working, now it is doing nothing I use a 5v relay and external power. Ground fro the nodemcu and positive from the source.
code i use / Version 0.3 - March 06 2018 /
include
include
include
include // get it from https://github.com/Links2004/arduinoWebSockets/releases
include // get it from https://arduinojson.org/ or install via Arduino library manager
include
ESP8266WiFiMulti WiFiMulti; WebSocketsClient webSocket; WiFiClient client;
define MyApiKey "c33f314a-12bc-49a6-bd48-24e3614bd91f" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
define MySSID "hidden" // TODO: Change to your Wifi network SSID
define MyWifiPassword "hidden" // TODO: Change to your Wifi network password
define MYPIN1 14 //D5
define MYPIN2 2 //D4
define MYPIN3 3 //RX
define MYPIN4 4 //D2
define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0; bool isConnected = false;
void setPowerStateOnServer(String deviceId, String value); void setTargetTemperatureOnServer(String deviceId, String value, String scale);
// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here
void turnOn(String deviceId) { if (deviceId == "5ea454ef2efe834986449a14") // Device ID of first device {
Serial.print("Turn on device id: "); Serial.println(deviceId); pinMode(MYPIN1, HIGH); } else if (deviceId == ".........") // Device ID of second device { Serial.print("Turn on device id: "); Serial.println(deviceId); pinMode(MYPIN2, HIGH);
} else if (deviceId == ".........") // Device ID of third device { Serial.print("Turn on device id: "); Serial.println(deviceId); pinMode(MYPIN3, HIGH);
} else if (deviceId == ".........") // Device ID of fourth device { Serial.print("Turn on device id: "); Serial.println(deviceId); pinMode(MYPIN4, HIGH);
} else { Serial.print("Turn on for unknown device id: "); Serial.println(deviceId);
}
}
void turnOff(String deviceId) { if (deviceId == "5ea454ef2efe834986449a14") // Device ID of first device {
Serial.print("Turn off Device ID: "); Serial.println(deviceId); pinMode(MYPIN1, LOW); } else if (deviceId == ".........") // Device ID of second device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); pinMode(MYPIN2, LOW);
} else if (deviceId == ".........") // Device ID of third device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); pinMode(MYPIN3, LOW);
} else if (deviceId == ".........") // Device ID of fourth device { Serial.print("Turn off Device ID: "); Serial.println(deviceId); pinMode(MYPIN4, LOW);
} else { Serial.print("Turn off for unknown device id: "); Serial.println(deviceId);
} }
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n"); break; case WStype_CONNECTED: { isConnected = true; Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload); Serial.printf("Waiting for commands from sinric.com ...\n");
} break; case WStype_TEXT: { Serial.printf("[WSc] get text: %s\n", payload); // Example payloads
} }
void setup() { Serial.begin(115200);
WiFiMulti.addAP(MySSID, MyWifiPassword); Serial.println(); Serial.print("Connecting to Wifi: "); Serial.println(MySSID);
// Waiting for Wifi connect while(WiFiMulti.run() != WL_CONNECTED) { delay(500); Serial.print("."); } if(WiFiMulti.run() == WL_CONNECTED) { Serial.println(""); Serial.print("WiFi connected. "); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }
// server address, port and URL webSocket.begin("iot.sinric.com", 80, "/");
// event handler webSocket.onEvent(webSocketEvent); webSocket.setAuthorization("apikey", MyApiKey);
// try again every 5000ms if connection has failed webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets }
void loop() { webSocket.loop();
if(isConnected) { uint64_t now = millis();
}
}
// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server // so it will reflect on Alexa app. // eg: setPowerStateOnServer("deviceid", "ON") void setPowerStateOnServer(String deviceId, String value) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["deviceId"] = deviceId; root["action"] = "setPowerState"; root["value"] = value; StreamString databuf; root.printTo(databuf);
webSocket.sendTXT(databuf); }
//eg: setPowerStateOnServer("deviceid", "CELSIUS", "25.0") void setTargetTemperatureOnServer(String deviceId, String value, String scale) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); root["action"] = "SetTargetTemperature"; root["deviceId"] = deviceId;
JsonObject& valueObj = root.createNestedObject("value"); JsonObject& targetSetpoint = valueObj.createNestedObject("targetSetpoint"); targetSetpoint["value"] = value; targetSetpoint["scale"] = scale;
StreamString databuf; root.printTo(databuf);
webSocket.sendTXT(databuf); }