Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This code is a modified version of the Client Sketch from Reference 1 Designed to Receive Potentiometer Readings on a Second ESP32 and Turn the Servo Motor to the Corresponding Angle
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- */
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <ESP32Servo.h> //To Work with Servo Motor
- #include <String.h> //To Convert String to Integer
- const char* ssid = "ESP32-Access-Point";
- const char* password = "123456789";
- //Your IP address or domain name with URL path
- const char* serverNameAngle = "http://192.168.4.1/angle";
- Servo myservo; // create servo object to control a servo
- // Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33
- int servoPin = 18; // GPIO pin used to connect the servo control (digital out)
- // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
- int ADC_Max = 4096; // This is the default ADC max value on the ESP32 (12 bit ADC width);
- // this width can be set (in low-level oode) from 9-12 bits, for a
- // a range of max values of 512-4096
- int angleInt;
- String angle;
- unsigned long previousMillis = 0;
- const long interval = 5000;
- void setup() {
- Serial.begin(115200);
- //Setup the Servo Motor
- // Allow allocation of all timers
- ESP32PWM::allocateTimer(0);
- ESP32PWM::allocateTimer(1);
- ESP32PWM::allocateTimer(2);
- ESP32PWM::allocateTimer(3);
- myservo.setPeriodHertz(50);// Standard 50hz servo
- myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
- // using SG90 servo min/max of 500us and 2400us
- // for MG995 large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservo.attach(servoPin);"
- //Connect the ESP32 client to the ESP32 server network
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while(WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis >= interval) {
- // Check WiFi connection status
- if(WiFi.status()== WL_CONNECTED ){
- angle = httpGETRequest(serverNameAngle);
- Serial.println("Angle: " + angle + "°");
- angleInt = angle.toInt(); //Converts angle back to an integer
- myservo.write(angleInt); // set the servo position according to the scaled value
- delay(200); // wait for the servo to get there
- // save the last HTTP GET Request
- previousMillis = currentMillis;
- }
- else {
- Serial.println("WiFi Disconnected");
- }
- }
- }
- String httpGETRequest(const char* serverName) {
- WiFiClient client;
- HTTPClient http;
- // Your Domain name with URL path or IP address with path
- http.begin(client, serverName);
- // Send HTTP POST request
- int httpResponseCode = http.GET();
- String payload = "--";
- if (httpResponseCode>0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- payload = http.getString();
- }
- else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- // Free resources
- http.end();
- return payload;
- }
- //References
- //Reference 1 How to Send Sensor Data from ESP32 to ESP32 Article https://randomnerdtutorials.com/esp32-client-server-wi-fi/
- //Reference 2 How to Work with ESP32 and Servo Motors Article https://dronebotworkshop.com/esp32-servo/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement