Advertisement
rstx2

new esp32 post

Mar 4th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. // tutorial found at here https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/
  2.  
  3. #ifdef ESP32
  4. #include <WiFi.h>
  5. #include <HTTPClient.h>
  6. #else
  7. #include <ESP8266WiFi.h>
  8. #include <ESP8266HTTPClient.h>
  9. #include <WiFiClient.h>
  10. #endif
  11.  
  12. #include <Wire.h>
  13.  
  14.  
  15. // Replace with your network credentials
  16. const char* ssid = "RST";
  17. const char* password = "<B75MA-P33>";
  18.  
  19. // REPLACE with your Domain name and URL path or IP address with path
  20. const char* serverName = "http://192.168.0.5:8000/api/Event/Sensor";
  21.  
  22. String sLocation = "Office";
  23. const int okBut = 34; //UP
  24. const int metalBut = 35; // the number of the pushbutton pin
  25. const int ledPin = 2; // the number of the LED pin
  26.  
  27.  
  28.  
  29. int buttonState = 0;
  30. int buttonStateX = 0;
  31.  
  32.  
  33. void setup() {
  34. Serial.begin(115200);
  35.  
  36. WiFi.begin(ssid, password);
  37. Serial.println("Connecting");
  38. while(WiFi.status() != WL_CONNECTED) {
  39. delay(500);
  40. Serial.print(".");
  41. }
  42. Serial.println("");
  43. Serial.print("Connected to WiFi network with IP Address: ");
  44. Serial.println(WiFi.localIP());
  45. Serial.println(WiFi.macAddress());
  46. String lcdBuffer;
  47. pinMode(ledPin, OUTPUT);
  48. pinMode(okBut, INPUT);
  49. pinMode(metalBut, INPUT);
  50.  
  51.  
  52. }
  53.  
  54. void loop() {
  55.  
  56. buttonState = digitalRead(okBut);
  57. buttonStateX = digitalRead(metalBut);
  58. // if (buttonState == HIGH) {
  59. // //Check WiFi connection status
  60. // if(WiFi.status()== WL_CONNECTED){
  61. // HTTPClient http;
  62. //
  63. // // Your Domain name with URL path or IP address with path
  64. // http.begin(serverName);
  65. //
  66. // // Specify content-type header
  67. // http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  68. //
  69. // // Prepare your HTTP POST request data
  70. // //String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
  71. // // + "&location=" + sensorLocation + "&value1=" + String(bme.readTemperature())
  72. // // + "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";
  73. //
  74. //
  75. // // You can comment the httpRequestData variable above
  76. // // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
  77. // //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
  78. //
  79. // String httpRequestData = "camera_ip=10.11.0.67&camera_mac=88:A9:A7:C0:00:30&group_id=0&group_name=ESP&name=ESP32&time_detect=1582740566020";
  80. //
  81. // //String httpRequestData = "detect_id=" + "1" + "&device_ip=" + esp32_ip + "&device_mac="+ esp32_mac + "&loc=" + sLocation + "";
  82. //
  83. //
  84. // // If you need an HTTP request with a content type: text/plain
  85. // //http.addHeader("Content-Type", "text/plain");
  86. // //int httpResponseCode = http.POST("Hello, World!");
  87. //
  88. // // If you need an HTTP request with a content type: application/json, use the following:
  89. // //http.addHeader("Content-Type", "application/json");
  90. // //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
  91. //
  92. // // Send HTTP POST request
  93. // int httpResponseCode = http.POST(httpRequestData);
  94. //
  95. // Serial.print("httpRequestData: ");
  96. // Serial.println(httpRequestData);
  97. //
  98. // if (httpResponseCode>0) {
  99. // Serial.print("HTTP Response code: ");
  100. // Serial.println(httpResponseCode);
  101. // }
  102. // else {
  103. // Serial.print("Error code: ");
  104. // Serial.println(httpResponseCode);
  105. // }
  106. // // Free resources
  107. // http.end();
  108. // }
  109. // else {
  110. // Serial.println("WiFi Disconnected");
  111. // }
  112. // //Send an HTTP POST request every 30 seconds
  113. // delay(500);
  114. // }
  115.  
  116. String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." +
  117. WiFi.localIP()[3];
  118.  
  119. String esp32_mac = String(WiFi.macAddress());
  120.  
  121. if (buttonStateX == HIGH) {
  122. //Check WiFi connection status
  123. if(WiFi.status()== WL_CONNECTED){
  124. HTTPClient http;
  125. http.begin(serverName);
  126. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  127. //String httpRequestData = "camera_ip=10.11.0.67&camera_mac=88:A9:A7:C0:00:30&group_id=0&group_name=ESP&name=ESP32&time_detect=1582740566020";
  128.  
  129. String httpRequestData = String("detect_id=") + 1 + String("&device_ip=") + LocalIP + String("&device_mac=")+ esp32_mac + String("&loc=") + sLocation + String("");
  130.  
  131. //String httpRequestData = "detect_id=" + "1" + "&device_ip=" + LocalIP + "&device_mac="+ "" + "&loc=" + sLocation + "";
  132.  
  133. int httpResponseCode = http.POST(httpRequestData);
  134. Serial.print("httpRequestData: ");
  135. Serial.println(httpRequestData);
  136.  
  137. if (httpResponseCode>0) {
  138. Serial.print("HTTP Response code: ");
  139. Serial.println(httpResponseCode);
  140. }
  141. else {
  142. Serial.print("Error code: ");
  143. Serial.println(httpResponseCode);
  144. }
  145. http.end();
  146. }
  147. else {
  148. Serial.println("WiFi Disconnected");
  149. }
  150. delay(500);
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement