Advertisement
JonD1988

RxPOTRev0

Jan 10th, 2022
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. /*
  3.   Rui Santos
  4.   Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
  5.  
  6.   Permission is hereby granted, free of charge, to any person obtaining a copy
  7.   of this software and associated documentation files.
  8.  
  9.   The above copyright notice and this permission notice shall be included in all
  10.   copies or substantial portions of the Software.
  11. */
  12.  
  13. #include <WiFi.h>
  14. #include <HTTPClient.h>
  15. #include <ESP32Servo.h> //To Work with Servo Motor
  16. #include <String.h> //To Convert String to Integer
  17.  
  18. const char* ssid = "ESP32-Access-Point";
  19. const char* password = "123456789";
  20.  
  21. //Your IP address or domain name with URL path
  22. const char* serverNameAngle = "http://192.168.4.1/angle";
  23.  
  24.  
  25. Servo myservo;  // create servo object to control a servo
  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. int servoPin = 18;      // GPIO pin used to connect the servo control (digital out)
  29. // Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
  30. int ADC_Max = 4096;     // This is the default ADC max value on the ESP32 (12 bit ADC width);
  31.                         // this width can be set (in low-level oode) from 9-12 bits, for a
  32.                         // a range of max values of 512-4096
  33.  
  34. int angleInt;
  35. String angle;
  36.  
  37. unsigned long previousMillis = 0;
  38. const long interval = 5000;
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.  
  43.   //Setup the Servo Motor
  44.   // Allow allocation of all timers
  45.   ESP32PWM::allocateTimer(0);
  46.   ESP32PWM::allocateTimer(1);
  47.   ESP32PWM::allocateTimer(2);
  48.   ESP32PWM::allocateTimer(3);
  49.   myservo.setPeriodHertz(50);// Standard 50hz servo
  50.   myservo.attach(servoPin, 500, 2400);   // attaches the servo on pin 18 to the servo object
  51.                                          // using SG90 servo min/max of 500us and 2400us
  52.                                          // for MG995 large servo, use 1000us and 2000us,
  53.                                          // which are the defaults, so this line could be
  54.                                          // "myservo.attach(servoPin);"
  55.  
  56.   //Connect the ESP32 client to the ESP32 server network
  57.   WiFi.begin(ssid, password);
  58.   Serial.println("Connecting");
  59.   while(WiFi.status() != WL_CONNECTED) {
  60.     delay(500);
  61.     Serial.print(".");
  62.   }
  63.   Serial.println("");
  64.   Serial.print("Connected to WiFi network with IP Address: ");
  65.   Serial.println(WiFi.localIP());
  66. }
  67.  
  68. void loop() {
  69.   unsigned long currentMillis = millis();
  70.  
  71.   if(currentMillis - previousMillis >= interval) {
  72.      // Check WiFi connection status
  73.     if(WiFi.status()== WL_CONNECTED ){
  74.       angle = httpGETRequest(serverNameAngle);
  75.       Serial.println("Angle: " + angle + "°");
  76.      
  77.       angleInt = angle.toInt(); //Converts angle back to an integer
  78.       myservo.write(angleInt);              // set the servo position according to the scaled value
  79.       delay(200);                          // wait for the servo to get there
  80.      
  81.       // save the last HTTP GET Request
  82.       previousMillis = currentMillis;
  83.     }
  84.     else {
  85.       Serial.println("WiFi Disconnected");
  86.     }
  87.   }
  88. }
  89.  
  90. String httpGETRequest(const char* serverName) {
  91.   WiFiClient client;
  92.   HTTPClient http;
  93.    
  94.   // Your Domain name with URL path or IP address with path
  95.   http.begin(client, serverName);
  96.  
  97.   // Send HTTP POST request
  98.   int httpResponseCode = http.GET();
  99.  
  100.   String payload = "--";
  101.  
  102.   if (httpResponseCode>0) {
  103.     Serial.print("HTTP Response code: ");
  104.     Serial.println(httpResponseCode);
  105.     payload = http.getString();
  106.   }
  107.   else {
  108.     Serial.print("Error code: ");
  109.     Serial.println(httpResponseCode);
  110.   }
  111.   // Free resources
  112.   http.end();
  113.  
  114.   return payload;
  115. }
  116.  
  117. //References
  118. //Reference 1 How to Send Sensor Data from ESP32 to ESP32 Article https://randomnerdtutorials.com/esp32-client-server-wi-fi/
  119. //Reference 2 How to Work with ESP32 and Servo Motors Article https://dronebotworkshop.com/esp32-servo/
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement