sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
Other
230 stars 124 forks source link

Using a Servo to Turn A Physical Switch #67

Closed Dragontamer333 closed 4 years ago

Dragontamer333 commented 4 years ago

So I used the original Sinric to make a servo turn a physical switch for being on and off. How would I do that with Sinric Pro and its multiswitch code. I like the relay part, but I want to replace that relay with a servo that will turn to a specific position with off and to another position with on.

sivar2311 commented 4 years ago

Hi Dragontamer,

the answer to this question is simple and complex at the same time ;) You can't take the example code 1:1. It must be adapted very extensively to your needs. Do you have a working sketch for controling just one servo using SinricPro ?

Dragontamer333 commented 4 years ago

No, I do not. I do have the code for non-Pro Sinric. While I'm looking at the Switch code for SinricPro I'm seeing an LED is involved. Can that LED be considered as the light for controlling the switch. If I were to include some servo code instead of:

digitalWrite(LED_PIN, myPowerState?LOW:HIGH);

Can that control the servo? It would be something like:

if(myPowerState == false){ servo.write(180); } else if(myPowerState == true){ servo.write(0); }

With 180 meaning the servo angle and that means the switch is on while 0 is off. I know I have to modify the code in order to make it work with what I want it to do, but I don't understand all of what the code is saying to modify it and add what i want.

sivar2311 commented 4 years ago

Sure you can remove the code for the LED. This ist just an example ;) The code to control your servo looks fine and should work. It didn't work?

Dragontamer333 commented 4 years ago

So here is the code that I added.

void handleButtonPress() { unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis if (digitalRead(BUTTON_PIN) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced? if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa myPowerState = false; } else { myPowerState = true; } digitalWrite(LED_PIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)

if (myPowerState == false){
  myservo.write(0);
}
else if (myPowerState == true){
  myservo.write(180);
}

// get Switch device back
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
// send powerstate event
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
Serial.printf("Device %s turned %s (manually via flashbutton)\r\n", mySwitch.getDeviceId(), myPowerState?"on":"off");

lastBtnPress = actualMillis;  // update last button press variable

} }

I included the libraries and such to the code and set up the servo correctly and it does move as the arduino starts up, and it does say it is connected to the Sinric Pro. But when the I ask Alexa to turn on the switch I have it set for the servo doesn't move. Seeing if i screwed with the code too much I attached an LED to the same pin as the LED in the code. When Alexa says its off the LED is on and when it is on the LED is off. That makes sense since if we were to attach a relay to the same pin as the LED then it would be triggered and off for the off command. So my issue right now is that the code for my servo doesn't work, and I don't know where the code is that triggers the LED. If I can find that spot and include my servo code it should work then, right? Anyone have any ideas?

sivar2311 commented 4 years ago

Yes, you made a mistake. Your code is inside the wrong function! handleButtonPress is the function for manually pressing the button! You dont need this function in your sketch, because you dont have such a button. Your servo code must be placed inside the onPowerState function!

Dragontamer333 commented 4 years ago

So I moved the code and it looks like this, but doesn't change at all.

bool onPowerState(const String &deviceId, bool &state) { Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off"); myPowerState = state; digitalWrite(LED_PIN, myPowerState?LOW:HIGH); return true; // request handled properly

if (myPowerState == false){ myservo.write(0); } else if (myPowerState == true){ myservo.write(180); } }

sivar2311 commented 4 years ago

In this case, your code never will be executed, because your function is returning before your code! Put the "return true" at the end of the function.

Dragontamer333 commented 4 years ago

Yep I was looking at the code and just changed that and it worked thank you. If you don't mind me asking I want to include the temperature sensor code from the other example so I can get temperature and humidity of the room. I copied over the necessary code and it does read and send the data to Alexa, but when I turn the switch on and off the serial monitor says the sensor is turned on and off.

Dragontamer333 commented 4 years ago

And looking at the Multiswitch code it seems like I need to include my servo portion into the onPowerState on the other versions, but how would I differentiate between the relay pin and the servo pin. Would it be something like if its the Device ID then do this code, but if it is every other ID then do the relay type code.

sivar2311 commented 4 years ago

Sorry, i don't understand. Is this a new sketch? Or is this the same sketch?

Dragontamer333 commented 4 years ago

Same sketch, but I want to include a temperature sensor withing this same code. It worked with sending data to Alexa, but when turning on or off the switch we were talking about earlier the serial monitor for arduino says the temperature sensor is turning on and off.

Dragontamer333 commented 4 years ago

For the multi switch comment I made I'm trying to understand how to make it so one arduino will have 2 smart switches. One controlled by a relay, like the orginal design for the code, and then the other controlled by the servo code that was just made.

sivar2311 commented 4 years ago

You need to add a new sensor device. There are two options: 1) using same onPowerState function

sivar2311 commented 4 years ago

Now you're mixing up many things...starting with servo, adding sensors, and now multi switches. I think best option is to start very simple, maybe open a new thread to keep your questions seperated. Otherwise it very hard to follow you. Please, do one step after the other. I don't know your programming skills.

Dragontamer333 commented 4 years ago

Ok thank you. So lets focus on just the multi switch portion and I'll set up another thread for the temperature sensor portion. Sorry for the confusion.

sivar2311 commented 4 years ago

Please have a look at MultiSwitch_beginner.ino. The example should be self-explanatory.

Dragontamer333 commented 4 years ago

Ok, thank you so much. seeing the beginner code I understand how it works and know now how to implement my code. I'm sorry for being so confusing.