Advertisement
JonD1988

TxPOTRev2

Jan 28th, 2022
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This code is a modified version of the Server Sketch from Reference 1 Designed to Transmit Potentiometer Readings to a Second ESP32
  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. //Works except last two servos (5 and 6) try the pins on the transmit board or receiver board changing
  5. /*
  6.   Rui Santos
  7.   Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
  8.  
  9.   Permission is hereby granted, free of charge, to any person obtaining a copy
  10.   of this software and associated documentation files.
  11.  
  12.   The above copyright notice and this permission notice shall be included in all
  13.   copies or substantial portions of the Software.
  14. */
  15.  
  16. // Import required libraries
  17. #include "WiFi.h" //For Working with Wifi
  18. #include "ESPAsyncWebServer.h" //For Working with Wifi
  19.  
  20. // Set your access point network credentials
  21. 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
  22. const char* password = "123456789";
  23.  
  24. // Create AsyncWebServer object on port 80
  25. AsyncWebServer server(80); //For Working with Wifi
  26.  
  27. // 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
  28. // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
  29. //int potPin1 = 34;        // GPIO pin used to connect potentiometer 1 (analog in)
  30. //int potPin2 = 35;        // GPIO pin used to connect potentiometer 2 (analog in)
  31. //On 38-pin board the following pins were used - See Diagram for Board Source 1
  32. int potPin1 = 34;        // GPIO pin used to connect potentiometer 1 (analog in) - Labeled G34 on board
  33. int potPin2 = 35;        // GPIO pin used to connect potentiometer 2 (analog in) - Labeled G35 on board
  34. int potPin3 = 32;        // GPIO pin used to connect potentiometer 3 (analog in) - Labeled G32 on board
  35. int potPin4 = 33;        // GPIO pin used to connect potentiometer 4 (analog in) - Labeled G33 on board
  36. int potPin5 = 15;        // GPIO pin used to connect potentiometer 5 (analog in) - Labeled G2 on board
  37. int potPin6 = 4;        // GPIO pin used to connect potentiometer 6 (analog in) - Labeled G4 on board
  38.  
  39. int ADC_Max = 4096;     // This is the default ADC max value on the ESP32 (12 bit ADC width);
  40.                         // this width can be set (in low-level oode) from 9-12 bits, for a
  41.                         // a range of max values of 512-4096
  42.  
  43. int valOne;    // variable to read the value from the analog pin the 1st POT is connected to
  44. int valTwo;    // variable to read the value from the analog pin the 2nd POT is connected to
  45. int valThree;    // variable to read the value from the analog pin the 3rd POT is connected to
  46. int valFour;    // variable to read the value from the analog pin the 4th POT is connected to
  47. int valFive;    // variable to read the value from the analog pin the 5th POT is connected to
  48. int valSix;    // variable to read the value from the analog pin the 6th POT is connected to
  49.  
  50. //Begin Function Definitions to Read Potentiometer Values
  51. String readPOTOne() {
  52.   String angleOne;  //Initializes local variable of String type
  53.   valOne = analogRead(potPin1); //Read the value of potentiometer One (value between 0 and 1023)
  54.   valOne = map(valOne, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  55.   angleOne = String(valOne);  //Converts integer to a string
  56.   return angleOne;
  57. }
  58.  
  59. String readPOTTwo() {
  60.   String angleTwo;  //Initializes local variable of String type
  61.   valTwo = analogRead(potPin2); //Read the value of potentiometer Two (value between 0 and 1023)
  62.   valTwo = map(valTwo, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  63.   angleTwo = String(valTwo);  //Converts integer to a string
  64.   return angleTwo;
  65. }
  66.  
  67. String readPOTThree() {
  68.   String angleThree;  //Initializes local variable of String type
  69.   valThree = analogRead(potPin3); //Read the value of potentiometer Three (value between 0 and 1023)
  70.   valThree = map(valThree, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  71.   angleThree = String(valThree);  //Converts integer to a string
  72.   return angleThree;
  73. }
  74.  
  75. String readPOTFour() {
  76.   String angleFour;  //Initializes local variable of String type
  77.   valFour = analogRead(potPin4); //Read the value of potentiometer Four (value between 0 and 1023)
  78.   valFour = map(valFour, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  79.   angleFour = String(valFour);  //Converts integer to a string
  80.   return angleFour;
  81. }
  82.  
  83. String readPOTFive() {
  84.   String angleFive;  //Initializes local variable of String type
  85.   valFive = analogRead(potPin5); //Read the value of potentiometer Four (value between 0 and 1023)
  86.   valFive = map(valFive, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  87.   angleFive = String(valFive);  //Converts integer to a string
  88.   return angleFive;
  89. }
  90.  
  91. String readPOTSix() {
  92.   String angleSix;  //Initializes local variable of String type
  93.   valSix = analogRead(potPin6); //Read the value of potentiometer Six (value between 0 and 1023)
  94.   valSix = map(valSix, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
  95.   angleSix = String(valSix);  //Converts integer to a string
  96.   return angleSix;
  97. }
  98. //End Function Definitions
  99.  
  100. void setup(){
  101.   // Serial port for debugging purposes
  102.   Serial.begin(115200);
  103.   Serial.println();
  104.  
  105.   // Setting the ESP as an access point
  106.   Serial.print("Setting AP (Access Point)…");
  107.   // Remove the password parameter, if you want the AP (Access Point) to be open
  108.   WiFi.softAP(ssid, password);
  109.  
  110.   IPAddress IP = WiFi.softAPIP();
  111.   Serial.print("AP IP address: ");
  112.   Serial.println(IP);
  113.  
  114. //For angleOne - Note unique parts "/angleOne" string in first line and name of readPOTOne function in second line
  115.   server.on("/angleOne", HTTP_GET, [](AsyncWebServerRequest *request){
  116.     request->send_P(200, "text/plain", readPOTOne().c_str());
  117.   });
  118.  
  119. //For angleTwo
  120.   server.on("/angleTwo", HTTP_GET, [](AsyncWebServerRequest *request){
  121.     request->send_P(200, "text/plain", readPOTTwo().c_str());
  122.   });
  123.  
  124. //For angleThree
  125.   server.on("/angleThree", HTTP_GET, [](AsyncWebServerRequest *request){
  126.     request->send_P(200, "text/plain", readPOTThree().c_str());
  127.   });
  128.  
  129. //For angleFour
  130.   server.on("/angleFour", HTTP_GET, [](AsyncWebServerRequest *request){
  131.     request->send_P(200, "text/plain", readPOTFour().c_str());
  132.   });
  133.  
  134. //For angleFive
  135.   server.on("/angleFive", HTTP_GET, [](AsyncWebServerRequest *request){
  136.     request->send_P(200, "text/plain", readPOTFive().c_str());
  137.   });
  138.  
  139. //For angleSix
  140.   server.on("/angleSix", HTTP_GET, [](AsyncWebServerRequest *request){
  141.     request->send_P(200, "text/plain", readPOTSix().c_str());
  142.   });
  143.    
  144.   bool status;
  145.  
  146.   // Start server
  147.   server.begin();
  148. }
  149.  
  150. void loop(){
  151.  
  152. }
  153.  
  154. //References
  155. //Reference 1 How to Send Sensor Data from ESP32 to ESP32 Article https://randomnerdtutorials.com/esp32-client-server-wi-fi/
  156. //Reference 2 How to Work with ESP32 and Servo Motors Article https://dronebotworkshop.com/esp32-servo/
  157.  
  158. //Board Source 1- https://www.aliexpress.com/item/32959541446.html?spm=a2g0o.9042311.0.0.5fda4c4dj8KyU3
  159. //Diagram "Pinout for 38-Pin Version.jpg" in D:\Stuff\Projects\ESP32 Control\Sources\ESP32 Development Board Page
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement