Advertisement
JonD1988

RxPOTRev2

Jan 28th, 2022
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.63 KB | None | 0 0
  1. //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
  2. //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)
  3. //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
  4. /*
  5.   Rui Santos
  6.   Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
  7.  
  8.   Permission is hereby granted, free of charge, to any person obtaining a copy
  9.   of this software and associated documentation files.
  10.  
  11.   The above copyright notice and this permission notice shall be included in all
  12.   copies or substantial portions of the Software.
  13. */
  14.  
  15. #include <WiFi.h> //Used for WiFi
  16. #include <HTTPClient.h> //Used for WiFi
  17. #include <ESP32Servo.h> //To Work with Servo Motor
  18. #include <String.h> //To Convert String to Integer
  19.  
  20. 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
  21. const char* password = "123456789";
  22.  
  23. //Your IP address or domain name with URL path
  24. const char* serverNameAngleOne = "http://192.168.4.1/angleOne"; //1st POT Angle
  25. const char* serverNameAngleTwo = "http://192.168.4.1/angleTwo"; //2nd POT Angle
  26. const char* serverNameAngleThree = "http://192.168.4.1/angleThree"; //3rd POT Angle
  27. const char* serverNameAngleFour = "http://192.168.4.1/angleFour"; //4th POT Angle
  28. const char* serverNameAngleFive = "http://192.168.4.1/angleFive"; //5th POT Angle
  29. const char* serverNameAngleSix = "http://192.168.4.1/angleSix"; //6th POT Angle
  30.  
  31. Servo myservoOne;  //Create servo object to control a servo - 1st Servo Motor
  32. Servo myservoTwo;  //Create servo object to control a servo - 2nd Servo Motor
  33. Servo myservoThree;  //Create servo object to control a servo - 3rd Servo Motor
  34. Servo myservoFour;  //Create servo object to control a servo - 4th Servo Motor
  35. Servo myservoFive;  //Create servo object to control a servo - 5th Servo Motor
  36. Servo myservoSix;  //Create servo object to control a servo - 6th Servo Motor
  37.  
  38. // 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
  39. //On 38-pin board the following pins were used - See Diagram for Board Source 1
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46.  
  47. // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
  48. int ADC_Max = 4096;     // This is the default ADC max value on the ESP32 (12 bit ADC width);
  49.                         // this width can be set (in low-level oode) from 9-12 bits, for a
  50.                         // a range of max values of 512-4096
  51.  
  52. int angleIntOne; //Variable to Store Angle One Converted from String Back to an Integer
  53. String angleOne; //Variable to Store Received Angle One Which Is a String Type Variable
  54.  
  55. int angleIntTwo; //Variable to Store Angle Two Converted from String Back to an Integer
  56. String angleTwo; //Variable to Store Received Angle Two Which Is a String Type Variable
  57.  
  58. int angleIntThree; //Variable to Store Angle Three Converted from String Back to an Integer
  59. String angleThree; //Variable to Store Received Angle Three Which Is a String Type Variable
  60.  
  61. int angleIntFour; //Variable to Store Angle Four Converted from String Back to an Integer
  62. String angleFour; //Variable to Store Received Angle Four Which Is a String Type Variable
  63.  
  64. int angleIntFive; //Variable to Store Angle Five Converted from String Back to an Integer
  65. String angleFive; //Variable to Store Received Angle Five Which Is a String Type Variable
  66.  
  67. int angleIntSix; //Variable to Store Angle Six Converted from String Back to an Integer
  68. String angleSix; //Variable to Store Received Angle Six Which Is a String Type Variable
  69.  
  70. unsigned long previousMillis = 0;
  71. const long interval = 100; //Reference 1 said this value was arbitrary - I changed it from 5000 to 100 ms
  72.  
  73. void setup() {
  74.   Serial.begin(115200); //Start the serial monitor for debugging purposes
  75.  
  76.   //Setup the Servo Motor
  77.   // Allow allocation of all timers
  78.   ESP32PWM::allocateTimer(0);
  79.   ESP32PWM::allocateTimer(1);
  80.   ESP32PWM::allocateTimer(2);
  81.   ESP32PWM::allocateTimer(3);
  82.   myservoOne.setPeriodHertz(50);  //Standard 50hz servo
  83.   myservoOne.attach(servoPinOne, 1000, 2000);   // attaches the servo on servoPinOne to the servo object
  84.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  85.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  86.                                          // which are the defaults, so this line could be
  87.                                          // "myservoOne.attach(servoPinOne);"
  88.  
  89.   myservoTwo.setPeriodHertz(50);  //Standard 50hz servo
  90.   myservoTwo.attach(servoPinTwo, 500, 2400);   //attaches the servo on servoPinTwo to the servo object
  91.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  92.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  93.                                          // which are the defaults, so this line could be
  94.                                          // "myservoTwo.attach(servoPinTwo);"
  95.  
  96.   myservoThree.setPeriodHertz(50);  //Standard 50hz servo
  97.   myservoThree.attach(servoPinThree, 500, 2400);   //attaches the servo on servoPinThree to the servo object
  98.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  99.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  100.                                          // which are the defaults, so this line could be
  101.                                          // "myservoThree.attach(servoPinThree);"
  102.  
  103.   myservoFour.setPeriodHertz(50);  //Standard 50hz servo
  104.   myservoFour.attach(servoPinFour, 500, 2400);   //attaches the servo on servoPinFour to the servo object
  105.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  106.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  107.                                          // which are the defaults, so this line could be
  108.                                          // "myservoFour.attach(servoPinFour);"
  109.  
  110.   myservoFive.setPeriodHertz(50);  //Standard 50hz servo
  111.   myservoFive.attach(servoPinFive, 500, 2400);   //attaches the servo on servoPinFive to the servo object
  112.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  113.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  114.                                          // which are the defaults, so this line could be
  115.                                          // "myservoFive.attach(servoPinFive);"
  116.  
  117.   myservoSix.setPeriodHertz(50);  //Standard 50hz servo
  118.   myservoSix.attach(servoPinSix, 500, 2400);   //attaches the servo on servoPinSix to the servo object
  119.                                          // using SG90 or MG90S small servo min/max of 500us and 2400us
  120.                                          // for MG995 or MG996R large servo, use 1000us and 2000us,
  121.                                          // which are the defaults, so this line could be
  122.                                          // "myservoSix.attach(servoPinSix);"
  123.  
  124.   //Connect the ESP32 client to the ESP32 server network
  125.   WiFi.begin(ssid, password);
  126.   Serial.println("Connecting");
  127.   while(WiFi.status() != WL_CONNECTED) {
  128.     delay(500);
  129.     Serial.print(".");
  130.   }
  131.   Serial.println("");
  132.   Serial.print("Connected to WiFi network with IP Address: ");
  133.   Serial.println(WiFi.localIP());
  134. }
  135.  
  136. void loop() {
  137.   unsigned long currentMillis = millis();
  138.  
  139.   if(currentMillis - previousMillis >= interval) {
  140.      // Check WiFi connection status
  141.     if(WiFi.status()== WL_CONNECTED ){
  142.       angleOne = httpGETRequest(serverNameAngleOne); //Gets the servo angle from the web server of the transmitting ESP32 and stores to a variable
  143.       Serial.println("AngleOne: " + angleOne + "°"); //Prints results to serial monitor for debugging
  144.       angleTwo = httpGETRequest(serverNameAngleTwo);
  145.       Serial.println("AngleTwo: " + angleTwo + "°");
  146.       angleThree = httpGETRequest(serverNameAngleThree);
  147.       Serial.println("AngleThree: " + angleThree + "°");
  148.       angleFour = httpGETRequest(serverNameAngleFour);
  149.       Serial.println("AngleFour: " + angleFour + "°");
  150.       angleFive = httpGETRequest(serverNameAngleFive);
  151.       Serial.println("AngleFive: " + angleFive + "°");
  152.       angleSix = httpGETRequest(serverNameAngleSix);
  153.       Serial.println("AngleSix: " + angleSix + "°");
  154.      
  155.       angleIntOne = angleOne.toInt(); //Converts angleOne back to an integer
  156.       angleIntTwo = angleTwo.toInt(); //Converts angleTwo back to an integer
  157.       angleIntThree = angleThree.toInt(); //Converts angleThree back to an integer
  158.       angleIntFour = angleFour.toInt(); //Converts angleFour back to an integer
  159.       angleIntFive = angleFive.toInt(); //Converts angleFive back to an integer
  160.       angleIntSix = angleSix.toInt(); //Converts angleSix back to an integer
  161.      
  162.       myservoOne.write(angleIntOne);       //set the servo position according to the scaled value
  163.       delay(200);                          //wait for the servo to get there
  164.  
  165.       myservoTwo.write(angleIntTwo);       //set the servo position according to the scaled value
  166.       delay(200);                          //wait for the servo to get there
  167.      
  168.       myservoThree.write(angleIntThree);   //set the servo position according to the scaled value
  169.       delay(200);                          //wait for the servo to get there
  170.  
  171.       myservoFour.write(angleIntFour);     //set the servo position according to the scaled value
  172.       delay(200);                          //wait for the servo to get there
  173.  
  174.       myservoFive.write(angleIntFive);     //set the servo position according to the scaled value
  175.       delay(200);                          //wait for the servo to get there
  176.  
  177.       myservoSix.write(angleIntSix);       //set the servo position according to the scaled value
  178.       delay(200);                          //wait for the servo to get there
  179.      
  180.       //save the last HTTP GET Request
  181.       previousMillis = currentMillis;
  182.     }
  183.     else {
  184.       Serial.println("WiFi Disconnected");
  185.     }
  186.   }
  187. }
  188.  
  189. //WiFi Related Function Definitions
  190. String httpGETRequest(const char* serverName) {
  191.   WiFiClient client;
  192.   HTTPClient http;
  193.    
  194.   // Your Domain name with URL path or IP address with path
  195.   http.begin(client, serverName);
  196.  
  197.   // Send HTTP POST request
  198.   int httpResponseCode = http.GET();
  199.  
  200.   String payload = "--";
  201.  
  202.   if (httpResponseCode>0) {
  203.     Serial.print("HTTP Response code: ");
  204.     Serial.println(httpResponseCode);
  205.     payload = http.getString();
  206.   }
  207.   else {
  208.     Serial.print("Error code: ");
  209.     Serial.println(httpResponseCode);
  210.   }
  211.   // Free resources
  212.   http.end();
  213.  
  214.   return payload;
  215. }
  216.  
  217. //References
  218. //Reference 1 How to Send Sensor Data from ESP32 to ESP32 Article https://randomnerdtutorials.com/esp32-client-server-wi-fi/
  219. //Reference 2 How to Work with ESP32 and Servo Motors Article https://dronebotworkshop.com/esp32-servo/
  220. //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
  221.  
  222. //Board Source 1- https://www.aliexpress.com/item/32959541446.html?spm=a2g0o.9042311.0.0.5fda4c4dj8KyU3
  223. //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