Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This code is a modified version of the Server Sketch from Reference 1 Designed to Transmit Potentiometer Readings to a Second ESP32
- //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
- //Works except last two servos (5 and 6) try the pins on the transmit board or receiver board changing
- /*
- 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.
- */
- // Import required libraries
- #include "WiFi.h" //For Working with Wifi
- #include "ESPAsyncWebServer.h" //For Working with Wifi
- // Set your access point network credentials
- 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";
- // Create AsyncWebServer object on port 80
- AsyncWebServer server(80); //For Working with Wifi
- // 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
- // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
- //int potPin1 = 34; // GPIO pin used to connect potentiometer 1 (analog in)
- //int potPin2 = 35; // GPIO pin used to connect potentiometer 2 (analog in)
- //On 38-pin board the following pins were used - See Diagram for Board Source 1
- int potPin1 = 34; // GPIO pin used to connect potentiometer 1 (analog in) - Labeled G34 on board
- int potPin2 = 35; // GPIO pin used to connect potentiometer 2 (analog in) - Labeled G35 on board
- int potPin3 = 32; // GPIO pin used to connect potentiometer 3 (analog in) - Labeled G32 on board
- int potPin4 = 33; // GPIO pin used to connect potentiometer 4 (analog in) - Labeled G33 on board
- int potPin5 = 15; // GPIO pin used to connect potentiometer 5 (analog in) - Labeled G2 on board
- int potPin6 = 4; // GPIO pin used to connect potentiometer 6 (analog in) - Labeled G4 on board
- 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 valOne; // variable to read the value from the analog pin the 1st POT is connected to
- int valTwo; // variable to read the value from the analog pin the 2nd POT is connected to
- int valThree; // variable to read the value from the analog pin the 3rd POT is connected to
- int valFour; // variable to read the value from the analog pin the 4th POT is connected to
- int valFive; // variable to read the value from the analog pin the 5th POT is connected to
- int valSix; // variable to read the value from the analog pin the 6th POT is connected to
- //Begin Function Definitions to Read Potentiometer Values
- String readPOTOne() {
- String angleOne; //Initializes local variable of String type
- valOne = analogRead(potPin1); //Read the value of potentiometer One (value between 0 and 1023)
- valOne = map(valOne, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleOne = String(valOne); //Converts integer to a string
- return angleOne;
- }
- String readPOTTwo() {
- String angleTwo; //Initializes local variable of String type
- valTwo = analogRead(potPin2); //Read the value of potentiometer Two (value between 0 and 1023)
- valTwo = map(valTwo, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleTwo = String(valTwo); //Converts integer to a string
- return angleTwo;
- }
- String readPOTThree() {
- String angleThree; //Initializes local variable of String type
- valThree = analogRead(potPin3); //Read the value of potentiometer Three (value between 0 and 1023)
- valThree = map(valThree, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleThree = String(valThree); //Converts integer to a string
- return angleThree;
- }
- String readPOTFour() {
- String angleFour; //Initializes local variable of String type
- valFour = analogRead(potPin4); //Read the value of potentiometer Four (value between 0 and 1023)
- valFour = map(valFour, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleFour = String(valFour); //Converts integer to a string
- return angleFour;
- }
- String readPOTFive() {
- String angleFive; //Initializes local variable of String type
- valFive = analogRead(potPin5); //Read the value of potentiometer Four (value between 0 and 1023)
- valFive = map(valFive, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleFive = String(valFive); //Converts integer to a string
- return angleFive;
- }
- String readPOTSix() {
- String angleSix; //Initializes local variable of String type
- valSix = analogRead(potPin6); //Read the value of potentiometer Six (value between 0 and 1023)
- valSix = map(valSix, 0, ADC_Max, 0, 180); //Scale the value to use it with the servo (value between 0 and 180)
- angleSix = String(valSix); //Converts integer to a string
- return angleSix;
- }
- //End Function Definitions
- void setup(){
- // Serial port for debugging purposes
- Serial.begin(115200);
- Serial.println();
- // Setting the ESP as an access point
- Serial.print("Setting AP (Access Point)…");
- // Remove the password parameter, if you want the AP (Access Point) to be open
- WiFi.softAP(ssid, password);
- IPAddress IP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(IP);
- //For angleOne - Note unique parts "/angleOne" string in first line and name of readPOTOne function in second line
- server.on("/angleOne", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTOne().c_str());
- });
- //For angleTwo
- server.on("/angleTwo", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTTwo().c_str());
- });
- //For angleThree
- server.on("/angleThree", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTThree().c_str());
- });
- //For angleFour
- server.on("/angleFour", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTFour().c_str());
- });
- //For angleFive
- server.on("/angleFive", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTFive().c_str());
- });
- //For angleSix
- server.on("/angleSix", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readPOTSix().c_str());
- });
- bool status;
- // Start server
- server.begin();
- }
- void loop(){
- }
- //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/
- //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