HarelKarni

esp32 http client

Jun 21st, 2021
403
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
  4.  
  5.   Permission is hereby granted, free of charge, to any person obtaining a copy
  6.   of this software and associated documentation files.
  7.  
  8.   The above copyright notice and this permission notice shall be included in all
  9.   copies or substantial portions of the Software.
  10. */
  11.  
  12. #include <Arduino.h>
  13.  
  14. #include <WiFi.h>
  15. #include <HTTPClient.h>
  16. #include <Servo.h>
  17. #include <Wire.h>
  18. #include <Adafruit_Sensor.h>
  19. #include <Adafruit_HMC5883_U.h>
  20.  
  21. #define LEFT            0
  22. #define RIGHT           1
  23. #define SHOOT           2
  24. #define UP              3
  25. #define DOWN            4
  26. #define NUM_OF_BUTTONS  5
  27.  
  28. #define V_SERVO_PIN 26
  29. #define H_SERVO_PIN 16
  30. #define SHOOT_LED 19
  31. #define RED_LED 18
  32. #define GREEN_LED 5
  33. #define BLUE_LED 17
  34.  
  35.  
  36. /* Assign a unique ID to this sensor at the same time */
  37. Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(100);
  38. float lastZ = 0;
  39. int loopsToLight = 0;
  40.  
  41. // svp, svn
  42. String httpGETRequest(const char* serverName);
  43. void displaySensorDetails();
  44.  
  45.  
  46. Servo h_servo;
  47. Servo v_servo;
  48.  
  49. int h_pos = 90;
  50. int v_pos = 90;
  51.  
  52. const char* ssid = "ESP8266-Access-Point";
  53. const char* password = "123456789";
  54.  
  55. //Your IP address or domain name with URL path
  56. const char* serverNameState = "http://192.168.4.1/state";
  57. String state;
  58.  
  59. bool up, down, right, left, shoot;
  60.  
  61. unsigned long previousMillis = 0;
  62. const long interval = 50;
  63.  
  64. int keep_boundry(int angle)
  65. {
  66.   if (angle > 180)
  67.     return 180;
  68.   if (angle < 0)
  69.     return 0;
  70.  
  71.   return angle;
  72. }
  73.  
  74. void direction_control(bool up, bool down, bool right, bool left)
  75. {
  76.   const int spin_speed = 10;
  77.   bool horizontal = right^left;
  78.   bool vertical = up^down;
  79.  
  80.   if (horizontal)
  81.   {
  82.     if (right)
  83.       h_pos -= spin_speed;
  84.     if (left)
  85.       h_pos += spin_speed;
  86.   }
  87.   if (vertical)
  88.   {
  89.     if (up)
  90.       v_pos += spin_speed;
  91.     if (down)
  92.       v_pos -= spin_speed;
  93.   }
  94.  
  95.   h_pos = keep_boundry(h_pos);
  96.   v_pos = keep_boundry(v_pos);
  97.  
  98.   v_servo.write(v_pos);
  99.   h_servo.write(h_pos);
  100.  
  101.   // Serial.println(String(h_pos) + " ; " + String(v_pos) + "\n");
  102. }
  103.  
  104. void setup()
  105. {
  106.   Serial.begin(115200);
  107.   Serial.println();
  108.  
  109.   pinMode(V_SERVO_PIN, OUTPUT);
  110.   pinMode(H_SERVO_PIN, OUTPUT);
  111.   pinMode(SHOOT_LED, OUTPUT);
  112.   pinMode(GREEN_LED, OUTPUT);
  113.   pinMode(RED_LED, OUTPUT);
  114.   pinMode(BLUE_LED, OUTPUT);
  115.  
  116.   digitalWrite(BLUE_LED, HIGH); // blue means not connected to wifi yet
  117.  
  118.   h_servo.attach(H_SERVO_PIN);
  119.   v_servo.attach(V_SERVO_PIN);
  120.  
  121.   WiFi.begin(ssid, password);
  122.   Serial.println("Connectingg");
  123.   while(WiFi.status() != WL_CONNECTED)
  124.   {
  125.     delay(500);
  126.     Serial.print(".");
  127.   }
  128.  
  129.   digitalWrite(BLUE_LED, LOW); // connected so stop show blue
  130.  
  131.   Serial.println("");
  132.   Serial.print("Connected to WiFi network with IP Address: ");
  133.   Serial.println(WiFi.localIP());
  134.  
  135.   /* Initialise the sensor */
  136.   if(!mag.begin())
  137.   {
  138.     /* There was a problem detecting the HMC5883 ... check your connections */
  139.     Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
  140.     while(true);
  141.   }
  142.  
  143.   /* Display some basic information on this sensor */
  144.   displaySensorDetails();
  145. }
  146.  
  147.  
  148. void loop()
  149. {
  150.   unsigned long currentMillis = millis();
  151.  
  152.   if(currentMillis - previousMillis >= interval)
  153.   {
  154.      // Check WiFi connection status
  155.     if(WiFi.status() == WL_CONNECTED )
  156.     {
  157.       state = httpGETRequest(serverNameState);
  158.      
  159.       // save the last HTTP GET Request
  160.       previousMillis = currentMillis;
  161.  
  162.       if(state.length() != NUM_OF_BUTTONS)
  163.         return;
  164.  
  165.       up = state.charAt(UP) == '1';
  166.       down = state.charAt(DOWN) == '1';
  167.       right = state.charAt(RIGHT) == '1';
  168.       left = state.charAt(LEFT) == '1';
  169.       shoot = state.charAt(SHOOT) == '1';
  170.  
  171.       digitalWrite(SHOOT_LED, shoot);
  172.       direction_control(up, down, right, left);
  173.      
  174.       // Serial.println("State: " + state);
  175.     }
  176.     else
  177.     {
  178.       Serial.println("WiFi Disconnected");
  179.     }
  180.   }
  181.  
  182.  
  183.   /* Get a new sensor event */
  184.   sensors_event_t event;
  185.   mag.getEvent(&event);
  186.  
  187.   /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  188.   // Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  189.   // Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  190.   Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  191.  
  192.   if (abs(event.magnetic.z - lastZ) > 2.5 || loopsToLight > 0)
  193.   {
  194.     digitalWrite(RED_LED, HIGH);
  195.     digitalWrite(GREEN_LED, LOW);
  196.     Serial.println("Z TOO BIOG IM DEAD NOW PWTFFFFFFFFFFFFffff");
  197.     if(loopsToLight <= 0) loopsToLight = 100;
  198.     else loopsToLight--;
  199.   }
  200.   else
  201.   {
  202.     digitalWrite(RED_LED, LOW);
  203.     digitalWrite(GREEN_LED, HIGH);
  204.   }
  205.  
  206.   lastZ = event.magnetic.z;
  207. }
  208.  
  209. String httpGETRequest(const char* serverName)
  210. {
  211.   HTTPClient http;
  212.    
  213.   // Your IP address with path or Domain name with URL path
  214.   http.begin(serverName);
  215.  
  216.   // Send HTTP POST request
  217.   int httpResponseCode = http.GET();
  218.  
  219.   String payload = "--";
  220.  
  221.   if (httpResponseCode == 200)
  222.   {
  223.     payload = http.getString();
  224.   }
  225.   else
  226.   {
  227.     Serial.print("Error code: ");
  228.     Serial.println(httpResponseCode);
  229.   }
  230.  
  231.   // Free resources
  232.   http.end();
  233.  
  234.   return payload;
  235. }
  236.  
  237.  
  238.  
  239. void displaySensorDetails()
  240. {
  241.   sensor_t sensor;
  242.   mag.getSensor(&sensor);
  243.   Serial.println("------------------------------------");
  244.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  245.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  246.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  247.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  248.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  249.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  250.   Serial.println("------------------------------------");
  251.   Serial.println("");
  252. }
  253.  
RAW Paste Data