Notifications
Clear all

Servo control using an ESP32 in the IoT Cloud

10 Posts
3 Users
4 Likes
296 Views
tinkerman
(@tinkerman)
Active Member
Joined: 2 years ago
Posts: 17
Topic starter  

Working on a project in the Arduino IoT cloud using a ESP32 to control a servo to control a latch. 

I've tried using the <Servo.h> include module but it does not seem to work.  I'm using the GPIO16 pin which has PWM and using a boolean variable for control.  I am powering the servo with an external 5v power supply. However the servo does not respond.  I used a servo tester to make sure it works.

Is there a different INCLUDE that should be used for controlling a servo in the IoT cloud with and ESP32?

 

The code I'm using is:

/*
Since EspSERV is READ_WRITE variable, onEspSERVChange() is
executed every time a new value is received from IoT Cloud.
*/
void onEspSERVChange() {
if (espSERV) {
SWserv.write(0);
} else {
SWserv.write(180);
}

}

 


   
Quote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3404
 

I have used the cloud and got it to work, although not with servos. If you could do 2 things, show us all the code, I know there is a lot missing and see the help for how to display it here.

IIRC, it's in the IDE Edit/Copy as HTML then in your reply here it's {;} and now paste. BEFORE that please do an IDE Tools/Auto Format

 

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
tinkerman
(@tinkerman)
Active Member
Joined: 2 years ago
Posts: 17
Topic starter  

I hope I did this right.  FYI, the included LED and potentiometer code work fine.  The Servo is attached to GPIO16 and using an external 5v power supply.

/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/81d9e0e6-ef64-4e18-8c6c-b277d72546ed

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

int esp32POT;
bool esp32LED;
bool esp32SERV;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <ESP32Servo.h>

int pinLED = 2;
int pinPOT = 34;

int servoPin = 16;

Servo SWservo;

void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
SWservo.setPeriodHertz(50);
SWservo.attach(servoPin,500,2400);

pinMode(pinLED, OUTPUT);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();

esp32POT = analogRead(pinPOT);
delay(500);

}

/*
Since Esp32LED is READ_WRITE variable, onEsp32LEDChange() is
executed every time a new value is received from IoT Cloud.
*/
void onEsp32LEDChange() {
// Add your code here to act upon Esp32LED change

if (esp32LED) {
digitalWrite(pinLED, HIGH);
} else {
digitalWrite(pinLED, LOW);
}
}

/*
Since Esp32SERV is READ_WRITE variable, onEsp32SERVChange() is
executed every time a new value is received from IoT Cloud.
*/
void onEsp32SERVChange() {
if (esp32SERV) {
SWservo.write(0);
} else {
SWservo.write(180);
}
}


   
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3404
 

@tinkerman Is there a question, or are you doing a show and tell?

FYI, you forgot this part of my prev post to you.

IIRC, it's in the IDE Edit/Copy as HTML then in your reply here it's {;} and now paste. BEFORE that please do an IDE Tools/Auto Format

 

 

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2112
 

@tinkerman 

Just a reminder that you need a common ground from the external servo power and your ESP's power.

I was kidnapped by mimes.
They did unspeakable things to me.


   
ReplyQuote
tinkerman
(@tinkerman)
Active Member
Joined: 2 years ago
Posts: 17
Topic starter  

@zander Hi, sorry, I was having issues posting the previous response.  I didn't see anywhere in the Arduino IoT cloud where I can save the code as an HTML.

My original question was asking if using the #include <Servo.h> was correct when using an ESP32 board.  I found 2 different examples, one using the <Servo.h> include and the other using an <ESP32Servo.h> include.  Neither seem to work. 

For the moment, I am going to build the same thing using an Arduino Nano and not use the Arduino Iot Cloud, but just the IDE on my PC. 

Appreciate any help, or suggestions. 

 


   
ReplyQuote
tinkerman
(@tinkerman)
Active Member
Joined: 2 years ago
Posts: 17
Topic starter  

@will Yes, I did that.  At this point I am going to use a Nano board and build the same example and make sure it all works.  Once I am satisfied that the code works on my PC, then I'll try again using the ESP32 in the Arduino IoT cloud space.

Thank you.


   
ReplyQuote
Will
 Will
(@will)
Noble Member
Joined: 1 year ago
Posts: 2112
 

@tinkerman 

That sounds like a good idea.

Another long shot to try (if you haven't already done so) is to go into Arduino > Preferences and set Compiler warnings to "All".

This will cause the compiler to list all of the warnings resulting from the compilation as well as all of the fatal errors. It's possible that something is going wrong during compilation but you're not seeing the error because it's not severe enough to stop compiling because of it.

I was kidnapped by mimes.
They did unspeakable things to me.


   
tinkerman and Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3404
 

@tinkerman One way of getting the sketch into forum condition is start in the arduino cloud or more precisely the IoT cloud, select your Thing, then select Sketch then select Full Editor now select the little down arrow beside the sketch and download the sketch. Now you can open it with the IDE. Just remember not to modify anything that will mess up the IoT cloud. You can create a real mess up to and including losing all your on event code. That is a good reason to have on event code be a single line that invoked 'normal' code.

The other approach is BETA, use IDE 2 and turn on the cloud option. I don't have it in front of me at the moment so can't give you step by step but in both IDE's make sure your prefs are maxed out and remember IDE 2 is BETA.

I gave up on the cloud for a few reasons, one of which is not a very rich environment, the second was unreliable connections. That reminds me I should reduce my cost now that I am not using it.

I am not a servo guy and the guy I know as a servo guy doesn't have esp32 loaded in his IDE so.

Whenever you say 

Neither seem to work. 

you may as well say iuhg oiuy rer yug oij oiuh

Show error messages, show the minimum code that replicates the error with a statement of what was expected and what happened.

Good luck.

 

 

 

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
tinkerman reacted
ReplyQuote
Ron
 Ron
(@zander)
Famed Member
Joined: 2 years ago
Posts: 3404
 

@tinkerman OR just use the Nano in the cloud, it is supported I think.

"Don't tell people how to do things. Tell them what to do and let them surprise you with their results.” - G.S. Patton, Gen. USA
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon


   
tinkerman reacted
ReplyQuote