Closed Soorajsharma123 closed 2 years ago
I think the sample code used does not fit very well with your project. Several adjustments are needed here.
Can you describe your project a bit more detailed?
I have to made a home automation using esp32 which contain manual switches and ir remote with Google home (Sinric pro) but the problem is ones I connect the esp 32 to wifi than manual switches and ir remote work it doesn't work without internet .
Here is my code
Edit: Code removed (allready posted above)
Ok, here is what i can see:
onPowerState
function that meets the logic of the (other) sketch
toggleState_X
logicRelayPin6
) outputs a PWM signal at bootSwitchPin2
must be HIGH
at boot, otherwise boot will fail)Please share your original Sketch (without SinricPro) that worked before.
Sir here is my previous code but in this code the same problem is arises, the manual switches is not working without connecting to the wifi ones the esp32 connects then the manual switches work automatically Here is my code
// Uncomment the following line to enable serial debug output//#define ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG#endif
"SinricProSwitch.h"
"YOUR-WIFI-PASSWORD"#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" //Enter the device IDs here#define device_ID_1 "SWITCH_ID_NO_1_HERE"#define device_ID_2 "SWITCH_ID_NO_2_HERE"#define device_ID_3 "SWITCH_ID_NO_3_HERE"#define device_ID_4 "SWITCH_ID_NO_4_HERE" // define the GPIO connected with Relays and switches#define RelayPin1 23 //D23#define RelayPin2 22 //D22#define RelayPin3 21 //D21#define RelayPin4 19 //D19
SwitchPin3 14 //D14#define SwitchPin4 27 //D27
// comment the following line if you use a toggle switches instead of tactile buttons//#define TACTILE_BUTTON 1
typedef struct { // struct for the std::map below int relayPIN; int flipSwitchPIN;} deviceConfig_t;
// this is the main configuration// please put in your deviceId, the PIN for Relay and PIN for flipSwitch// this can be up to N devices...depending on how much pin's available on your device ;)// right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually std::map<String, deviceConfig_t> devices = { //{deviceId, {relayPIN, flipSwitchPIN}} {device_ID_1, { RelayPin1, SwitchPin1 }}, {device_ID_2, { RelayPin2, SwitchPin2 }}, {device_ID_3, { RelayPin3, SwitchPin3 }}, {device_ID_4, { RelayPin4, SwitchPin4 }} }; typedef struct { // struct for the std::map below String deviceId; bool lastFlipSwitchState; unsigned long lastFlipSwitchChange;} flipSwitchConfig_t;
std::map<int, flipSwitchConfig_t> flipSwitches; // this map is used to map flipSwitch PINs to deviceId and handling debounce and last flipSwitch state checks // it will be setup in "setupFlipSwitches" function, using informations from devices map void setupRelays() { for (auto &device : devices) { // for each device (relay, flipSwitch combination) int relayPIN = device.second.relayPIN; // get the relay pin pinMode(relayPIN, OUTPUT); // set relay pin to OUTPUT digitalWrite(relayPIN, HIGH); }} void setupFlipSwitches() { for (auto &device : devices) { // for each device (relay / flipSwitch combination) flipSwitchConfig_t flipSwitchConfig; // create a new flipSwitch configuration
flipSwitchConfig.deviceId = device.first; // set the deviceId
flipSwitchConfig.lastFlipSwitchChange = 0; // set debounce time
flipSwitchConfig.lastFlipSwitchState = true; // set
lastFlipSwitchState to false (LOW)--
int flipSwitchPIN = device.second.flipSwitchPIN; // get the flipSwitchPIN
flipSwitches[flipSwitchPIN] = flipSwitchConfig; // save the
flipSwitch config to flipSwitches map pinMode(flipSwitchPIN, INPUT_PULLUP); // set the flipSwitch pin to INPUT }} bool onPowerState(String deviceId, bool &state){ Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off"); int relayPIN = devices[deviceId].relayPIN; // get the relay pin for corresponding device digitalWrite(relayPIN, !state); // set the new relay state return true;} void handleFlipSwitches() { unsigned long actualMillis = millis(); // get actual millis for (auto &flipSwitch : flipSwitches) { // for each flipSwitch in flipSwitches map unsigned long lastFlipSwitchChange = flipSwitch.second.lastFlipSwitchChange; // get the timestamp when flipSwitch was pressed last time (used to debounce / limit events)
if (actualMillis - lastFlipSwitchChange > DEBOUNCE_TIME) {
// if time is > debounce time...
int flipSwitchPIN = flipSwitch.first;
// get the flipSwitch pin from configuration
bool lastFlipSwitchState =
flipSwitch.second.lastFlipSwitchState; // get the lastFlipSwitchState bool flipSwitchState = digitalRead(flipSwitchPIN); // read the current flipSwitch state if (flipSwitchState != lastFlipSwitchState) { // if the flipSwitchState has changed...#ifdef TACTILE_BUTTON if (flipSwitchState) { // if the tactile button is pressed #endif flipSwitch.second.lastFlipSwitchChange = actualMillis; // update lastFlipSwitchChange time String deviceId = flipSwitch.second.deviceId; // get the deviceId from config int relayPIN = devices[deviceId].relayPIN; // get the relayPIN from config bool newRelayState = !digitalRead(relayPIN); // set the new relay State digitalWrite(relayPIN, newRelayState); // set the trelay to the new state
SinricProSwitch &mySwitch = SinricPro[deviceId];
// get Switch device from SinricPro
mySwitch.sendPowerStateEvent(!newRelayState);
// send the event#ifdef TACTILE_BUTTON
}#endif
flipSwitch.second.lastFlipSwitchState = flipSwitchState;
// update lastFlipSwitchState
}
}
}} void setupWiFi(){ Serial.printf("\r\n[Wifi]: Connecting"); WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } digitalWrite(wifiLed, HIGH); Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());} void setupSinricPro(){ for (auto &device : devices) { const char *deviceId = device.first.c_str(); SinricProSwitch &mySwitch = SinricPro[deviceId]; mySwitch.onPowerState(onPowerState); }
SinricPro.begin(APP_KEY, APP_SECRET); SinricPro.restoreDeviceStates(true);} void setup(){ Serial.begin(BAUD_RATE);
pinMode(wifiLed, OUTPUT); digitalWrite(wifiLed, LOW);
setupRelays(); setupFlipSwitches(); setupWiFi(); setupSinricPro();} void loop(){ SinricPro.handle(); handleFlipSwitches();}
On Fri, Dec 31, 2021, 10:41 Boris Jäger @.***> wrote:
Ok, here is what i can see:
- It looks like, two sketches are copied togheter.
- There are no modifications in the onPowerState function that meets the logic of the (other) sketch
- onPowerState does write directly to the gpio and does not respect the toggleState_X logic
- At least 2 gpio's are dangerous to use
- gpio 5 (used as RelayPin6) outputs a PWM signal at boot
- gpio 12 (used as SwitchPin2 must be HIGH at boot, otherwise boot will fail)
- for more info's about gpio's please see here https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
- You need 6 SinricPro deviceId's but it seems only 3 are provided
- Do you have 6 deviceId's ?
Please share your original Sketch (without SinricPro) that worked before.
— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/238#issuecomment-1003271496, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDDU6IWYTL5KWDJLF55MPLUTU3PJANCNFSM5K72KLDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you authored the thread.Message ID: @.***>
I meant your sketch without SinricPro stuff. The code you have used before trying to inttegrate SinricPro. The one that was working.
Can you plzz make it correct so that I can use sinric pro for my home automation also with manual switches...
On Fri, Dec 31, 2021, 11:18 Boris Jäger @.***> wrote:
I meant your sketch without SinricPro stuff. The code you have used before trying to inttegrate SinricPro. The one that was working.
— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/238#issuecomment-1003277223, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDDU6OMWC4NTJYGA24JQUTUTU73LANCNFSM5K72KLDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you authored the thread.Message ID: @.***>
I can't help you if i don't understand the problem and the code. The more information you provide, the better I can help you.
Please answer all questions:
I highly recommend to use the beginner's multiswitch example as base for your project. This example meets your programming style and should be easier for you to understand.
1st Yes I had checked GPIOs 5 and 12 but I do not find any problem in this
2nd
I do not use any other sketch for operating appliance with Google but I had used blynk app to control the appliance and also using bluetooth . But I want to operate this with Google using Sinric pro, and also with manual switches and IR remote.
3rd
The Sinric pro provides us only 3 device's I'd so I can use the three only . For more device's I'd I have to subscribe the SINRICPRO..
The code that I had given you is working fine it works with Google home (app) and the manual switches is also works fine when I first connect the wifi to ESP32 but the manual switch is not working without connecting to the wifi first , that mean first I have to connect the wifi than the manual switches work otherwise not. Here is my code ,
// Uncomment the following line to enable serial debug output//#define ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG#endif
"SinricProSwitch.h"
"YOUR-WIFI-PASSWORD"#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" //Enter the device IDs here#define device_ID_1 "SWITCH_ID_NO_1_HERE"#define device_ID_2 "SWITCH_ID_NO_2_HERE"#define device_ID_3 "SWITCH_ID_NO_3_HERE"#define device_ID_4 "SWITCH_ID_NO_4_HERE" // define the GPIO connected with Relays and switches#define RelayPin1 23 //D23#define RelayPin2 22 //D22#define RelayPin3 21 //D21#define RelayPin4 19 //D19
SwitchPin3 14 //D14#define SwitchPin4 27 //D27
// comment the following line if you use a toggle switches instead of tactile buttons//#define TACTILE_BUTTON 1
typedef struct { // struct for the std::map below int relayPIN; int flipSwitchPIN;} deviceConfig_t;
// this is the main configuration// please put in your deviceId, the PIN for Relay and PIN for flipSwitch// this can be up to N devices...depending on how much pin's available on your device ;)// right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually std::map<String, deviceConfig_t> devices = { //{deviceId, {relayPIN, flipSwitchPIN}} {device_ID_1, { RelayPin1, SwitchPin1 }}, {device_ID_2, { RelayPin2, SwitchPin2 }}, {device_ID_3, { RelayPin3, SwitchPin3 }}, {device_ID_4, { RelayPin4, SwitchPin4 }} }; typedef struct { // struct for the std::map below String deviceId; bool lastFlipSwitchState; unsigned long lastFlipSwitchChange;} flipSwitchConfig_t;
std::map<int, flipSwitchConfig_t> flipSwitches; // this map is used to map flipSwitch PINs to deviceId and handling debounce and last flipSwitch state checks // it will be setup in "setupFlipSwitches" function, using informations from devices map void setupRelays() { for (auto &device : devices) { // for each device (relay, flipSwitch combination) int relayPIN = device.second.relayPIN; // get the relay pin pinMode(relayPIN, OUTPUT); // set relay pin to OUTPUT digitalWrite(relayPIN, HIGH); }} void setupFlipSwitches() { for (auto &device : devices) { // for each device (relay / flipSwitch combination) flipSwitchConfig_t flipSwitchConfig; // create a new flipSwitch configuration
flipSwitchConfig.deviceId = device.first; // set the deviceId
flipSwitchConfig.lastFlipSwitchChange = 0; // set debounce time
flipSwitchConfig.lastFlipSwitchState = true; // set
lastFlipSwitchState to false (LOW)--
int flipSwitchPIN = device.second.flipSwitchPIN; // get the flipSwitchPIN
flipSwitches[flipSwitchPIN] = flipSwitchConfig; // save the
flipSwitch config to flipSwitches map pinMode(flipSwitchPIN, INPUT_PULLUP); // set the flipSwitch pin to INPUT }} bool onPowerState(String deviceId, bool &state){ Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off"); int relayPIN = devices[deviceId].relayPIN; // get the relay pin for corresponding device digitalWrite(relayPIN, !state); // set the new relay state return true;} void handleFlipSwitches() { unsigned long actualMillis = millis(); // get actual millis for (auto &flipSwitch : flipSwitches) { // for each flipSwitch in flipSwitches map unsigned long lastFlipSwitchChange = flipSwitch.second.lastFlipSwitchChange; // get the timestamp when flipSwitch was pressed last time (used to debounce / limit events)
if (actualMillis - lastFlipSwitchChange > DEBOUNCE_TIME) {
// if time is > debounce time...
int flipSwitchPIN = flipSwitch.first;
// get the flipSwitch pin from configuration
bool lastFlipSwitchState =
flipSwitch.second.lastFlipSwitchState; // get the lastFlipSwitchState bool flipSwitchState = digitalRead(flipSwitchPIN); // read the current flipSwitch state if (flipSwitchState != lastFlipSwitchState) { // if the flipSwitchState has changed...#ifdef TACTILE_BUTTON if (flipSwitchState) { // if the tactile button is pressed #endif flipSwitch.second.lastFlipSwitchChange = actualMillis; // update lastFlipSwitchChange time String deviceId = flipSwitch.second.deviceId; // get the deviceId from config int relayPIN = devices[deviceId].relayPIN; // get the relayPIN from config bool newRelayState = !digitalRead(relayPIN); // set the new relay State digitalWrite(relayPIN, newRelayState); // set the trelay to the new state
SinricProSwitch &mySwitch = SinricPro[deviceId];
// get Switch device from SinricPro
mySwitch.sendPowerStateEvent(!newRelayState);
// send the event#ifdef TACTILE_BUTTON
}#endif
flipSwitch.second.lastFlipSwitchState = flipSwitchState;
// update lastFlipSwitchState
}
}
}} void setupWiFi(){ Serial.printf("\r\n[Wifi]: Connecting"); WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { Serial.printf("."); delay(250); } digitalWrite(wifiLed, HIGH); Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());} void setupSinricPro(){ for (auto &device : devices) { const char *deviceId = device.first.c_str(); SinricProSwitch &mySwitch = SinricPro[deviceId]; mySwitch.onPowerState(onPowerState); }
SinricPro.begin(APP_KEY, APP_SECRET); SinricPro.restoreDeviceStates(true);} void setup(){ Serial.begin(BAUD_RATE);
pinMode(wifiLed, OUTPUT); digitalWrite(wifiLed, LOW);
setupRelays(); setupFlipSwitches(); setupWiFi(); setupSinricPro();} void loop(){ SinricPro.handle(); handleFlipSwitches();}
On Sat, Jan 1, 2022, 15:38 Boris Jäger @.***> wrote:
I can't help you if i don't understand the problem and the code. The more information you provide, the better I can help you.
Please answer all questions:
- Did you checked the GPIOs i mentioned above (dangerous gpio 5 and 12) ?
- Do you have a working sketch without sinric pro?
- Do you have 6 SinricPro deviceId's?
— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/238#issuecomment-1003535780, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDDU6JUVYS3KO4T5LJKHDDUT3HB3ANCNFSM5K72KLDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you authored the thread.Message ID: @.***>
Sir in this code the function of manual switch is not available so that I can operate the appliance with manually..
On Sat, Jan 1, 2022, 15:56 Boris Jäger @.***> wrote:
I highly recommend to use the beginner's multiswitch example https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/examples/Switch/MultiSwitch_beginner/MultiSwitch_beginner.ino as base for your project. This example meets your programming style and should be easier for you to understand.
— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/238#issuecomment-1003538161, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDDU6KJIAPVG7M75TZ7UELUT3JDZANCNFSM5K72KLDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you authored the thread.Message ID: @.***>
The code that I had given you is working fine it works with Google home (app) and the manual switches is also works fine when I first connect the wifi to ESP32 but the manual switch is not working without connecting to the wifi first , that mean first I have to connect the wifi than the manual switches work otherwise not.
Ah, now i begin to understand.
This is because the setupWiFi() function waits until a connection is established! You have to handle the switches within this while it waits for the wifi connection. One soulution could be this:
while (WiFi.status() != WL_CONNECTED) {
handleFlipSwitches();
yield();
}
Sir , that mean I have to add this function in WIFI()
while (WiFi.status() != WL_CONNECTED) { handleFlipSwitches(); yield(); }
I also want to add #include IRremote.h so that I can use ir remote to control the appliance
So I have to also add this function to WIFI()
ir_remote_control();
Am I getting it right?
Yes!
The while loop in setupWiFi() is blocking your sketch until a wifi connection has been established.
Everything you want to handle (like buttons, ir etc..) until a wifi connection has been established must be handled in this while loop.
Ok I get it thanku so much sir for your greatness and helping me .I will asking to you such type of problem when I will face the problem thanku ...
On Sat, Jan 1, 2022, 16:21 Boris Jäger @.***> wrote:
Yes!
The while loop in setupWiFi() is blocking your sketch until a wifi connection has been established.
Everything you want to handle (like buttons, ir etc..) until a wifi connection has been established must be handled in this while loop.
— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/238#issuecomment-1003540173, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDDU6PP3ZIVOZ5J4P2SSPLUT3MCHANCNFSM5K72KLDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
You are receiving this because you authored the thread.Message ID: @.***>
If your question is finally answered, please don't forget to close this issue. Feel free to reopen this issue if the problem still exists.
For new questions / problems, please open a new issue.
This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!
Hey again! It’s been 14 days since anything happened on this issue, so our friendly robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY
. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks again for being a part of the SinricPro community!