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
- //The difference between Rev 0 and Rev 1 is that Rev 1 adds a second servo motor on GPIO19 (D19 on the 30-Pin Board) and a second POT on GPIO35 (D35 on 30-Pin Board)
- //Rev 2 is testing this code on two 38-pin ESPs sourced from the link referenced under Board Source 1 at the bottom of this file and to use 6 servo motors instead of just two
- /*
- 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> //Used for WiFi
- #include <HTTPClient.h> //Used for WiFi
- #include <ESP32Servo.h> //To Work with Servo Motor
- #include <String.h> //To Convert String to Integer
- const char* ssid = "ESP32-Access-Point"; //Changed these credentials for the Rev 2 sketch so it doesn't interfere with my other similar project for 2 servo control
- const char* password = "123456789";
- //Your IP address or domain name with URL path
- const char* serverNameAngleOne = "http://192.168.4.1/angleOne"; //1st POT Angle
- const char* serverNameAngleTwo = "http://192.168.4.1/angleTwo"; //2nd POT Angle
- const char* serverNameAngleThree = "http://192.168.4.1/angleThree"; //3rd POT Angle
- const char* serverNameAngleFour = "http://192.168.4.1/angleFour"; //4th POT Angle
- const char* serverNameAngleFive = "http://192.168.4.1/angleFive"; //5th POT Angle
- const char* serverNameAngleSix = "http://192.168.4.1/angleSix"; //6th POT Angle
- Servo myservoOne; //Create servo object to control a servo - 1st Servo Motor
- Servo myservoTwo; //Create servo object to control a servo - 2nd Servo Motor
- Servo myservoThree; //Create servo object to control a servo - 3rd Servo Motor
- Servo myservoFour; //Create servo object to control a servo - 4th Servo Motor
- Servo myservoFive; //Create servo object to control a servo - 5th Servo Motor
- Servo myservoSix; //Create servo object to control a servo - 6th Servo Motor
- // 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
- //On 38-pin board the following pins were used - See Diagram for Board Source 1
- int servoPinOne = 16; //GPIO pin used to connect the servo control (digital out) for Servo One - Labeled G16 on board and also RX2 on diagram
- int servoPinTwo = 17; //GPIO pin used to connect the servo control (digital out) for Servo Two - Labeled G17 on board and also TX2 on diagram
- int servoPinThree = 18; //GPIO pin used to connect the servo control (digital out) for Servo Three - Labeled G18 on board and also VSPI SCK on diagram
- int servoPinFour = 19; //GPIO pin used to connect the servo control (digital out) for Servo Four - Labeled G19 on board and also VSPI MISO on diagram
- int servoPinFive = 21; //GPIO pin used to connect the servo control (digital out) for Servo Five - Labeled G21 on board and also I2C SDA on diagram
- int servoPinSix = 22; //GPIO pin used to connect the servo control (digital out) for Servo Five - Labeled G22 on board and also I2C SCL on diagram
- // 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 angleIntOne; //Variable to Store Angle One Converted from String Back to an Integer
- String angleOne; //Variable to Store Received Angle One Which Is a String Type Variable
- int angleIntTwo; //Variable to Store Angle Two Converted from String Back to an Integer
- String angleTwo; //Variable to Store Received Angle Two Which Is a String Type Variable
- int angleIntThree; //Variable to Store Angle Three Converted from String Back to an Integer
- String angleThree; //Variable to Store Received Angle Three Which Is a String Type Variable
- int angleIntFour; //Variable to Store Angle Four Converted from String Back to an Integer
- String angleFour; //Variable to Store Received Angle Four Which Is a String Type Variable
- int angleIntFive; //Variable to Store Angle Five Converted from String Back to an Integer
- String angleFive; //Variable to Store Received Angle Five Which Is a String Type Variable
- int angleIntSix; //Variable to Store Angle Six Converted from String Back to an Integer
- String angleSix; //Variable to Store Received Angle Six Which Is a String Type Variable
- unsigned long previousMillis = 0;
- const long interval = 100; //Reference 1 said this value was arbitrary - I changed it from 5000 to 100 ms
- void setup() {
- Serial.begin(115200); //Start the serial monitor for debugging purposes
- //Setup the Servo Motor
- // Allow allocation of all timers
- ESP32PWM::allocateTimer(0);
- ESP32PWM::allocateTimer(1);
- ESP32PWM::allocateTimer(2);
- ESP32PWM::allocateTimer(3);
- myservoOne.setPeriodHertz(50); //Standard 50hz servo
- myservoOne.attach(servoPinOne, 1000, 2000); // attaches the servo on servoPinOne to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoOne.attach(servoPinOne);"
- myservoTwo.setPeriodHertz(50); //Standard 50hz servo
- myservoTwo.attach(servoPinTwo, 500, 2400); //attaches the servo on servoPinTwo to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoTwo.attach(servoPinTwo);"
- myservoThree.setPeriodHertz(50); //Standard 50hz servo
- myservoThree.attach(servoPinThree, 500, 2400); //attaches the servo on servoPinThree to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoThree.attach(servoPinThree);"
- myservoFour.setPeriodHertz(50); //Standard 50hz servo
- myservoFour.attach(servoPinFour, 500, 2400); //attaches the servo on servoPinFour to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoFour.attach(servoPinFour);"
- myservoFive.setPeriodHertz(50); //Standard 50hz servo
- myservoFive.attach(servoPinFive, 500, 2400); //attaches the servo on servoPinFive to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoFive.attach(servoPinFive);"
- myservoSix.setPeriodHertz(50); //Standard 50hz servo
- myservoSix.attach(servoPinSix, 500, 2400); //attaches the servo on servoPinSix to the servo object
- // using SG90 or MG90S small servo min/max of 500us and 2400us
- // for MG995 or MG996R large servo, use 1000us and 2000us,
- // which are the defaults, so this line could be
- // "myservoSix.attach(servoPinSix);"
- //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 ){
- angleOne = httpGETRequest(serverNameAngleOne); //Gets the servo angle from the web server of the transmitting ESP32 and stores to a variable
- Serial.println("AngleOne: " + angleOne + "°"); //Prints results to serial monitor for debugging
- angleTwo = httpGETRequest(serverNameAngleTwo);
- Serial.println("AngleTwo: " + angleTwo + "°");
- angleThree = httpGETRequest(serverNameAngleThree);
- Serial.println("AngleThree: " + angleThree + "°");
- angleFour = httpGETRequest(serverNameAngleFour);
- Serial.println("AngleFour: " + angleFour + "°");
- angleFive = httpGETRequest(serverNameAngleFive);
- Serial.println("AngleFive: " + angleFive + "°");
- angleSix = httpGETRequest(serverNameAngleSix);
- Serial.println("AngleSix: " + angleSix + "°");
- angleIntOne = angleOne.toInt(); //Converts angleOne back to an integer
- angleIntTwo = angleTwo.toInt(); //Converts angleTwo back to an integer
- angleIntThree = angleThree.toInt(); //Converts angleThree back to an integer
- angleIntFour = angleFour.toInt(); //Converts angleFour back to an integer
- angleIntFive = angleFive.toInt(); //Converts angleFive back to an integer
- angleIntSix = angleSix.toInt(); //Converts angleSix back to an integer
- myservoOne.write(angleIntOne); //set the servo position according to the scaled value
- delay(200); //wait for the servo to get there
- myservoTwo.write(angleIntTwo); //set the servo position according to the scaled value
- delay(200); //wait for the servo to get there
- myservoThree.write(angleIntThree); //set the servo position according to the scaled value
- delay(200); //wait for the servo to get there
- myservoFour.write(angleIntFour); //set the servo position according to the scaled value
- delay(200); //wait for the servo to get there
- myservoFive.write(angleIntFive); //set the servo position according to the scaled value
- delay(200); //wait for the servo to get there
- myservoSix.write(angleIntSix); //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");
- }
- }
- }
- //WiFi Related Function Definitions
- 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/
- //Reference 3 ESP32 Pinout Reference: Which GPIO pins should you use? https://randomnerdtutorials.com/esp32-pinout-reference-gpios/ - I found this diagram helpful to double check Board Source 1's diagram
- //Board Source 1- https://www.aliexpress.com/item/32959541446.html?spm=a2g0o.9042311.0.0.5fda4c4dj8KyU3
- //Diagram "Pinout for 38-Pin Version.jpg" in D:\Stuff\Projects\ESP32 Control\Sources\ESP32 Development Board Page
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement