Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tutorial found at here https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/
- #ifdef ESP32
- #include <WiFi.h>
- #include <HTTPClient.h>
- #else
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <WiFiClient.h>
- #endif
- #include <Wire.h>
- // Replace with your network credentials
- const char* ssid = "RST";
- const char* password = "<B75MA-P33>";
- // REPLACE with your Domain name and URL path or IP address with path
- const char* serverName = "http://192.168.0.5:8000/api/Event/Sensor";
- String sLocation = "Office";
- const int okBut = 34; //UP
- const int metalBut = 35; // the number of the pushbutton pin
- const int ledPin = 2; // the number of the LED pin
- int buttonState = 0;
- int buttonStateX = 0;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while(WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- Serial.println(WiFi.macAddress());
- String lcdBuffer;
- pinMode(ledPin, OUTPUT);
- pinMode(okBut, INPUT);
- pinMode(metalBut, INPUT);
- }
- void loop() {
- buttonState = digitalRead(okBut);
- buttonStateX = digitalRead(metalBut);
- // if (buttonState == HIGH) {
- // //Check WiFi connection status
- // if(WiFi.status()== WL_CONNECTED){
- // HTTPClient http;
- //
- // // Your Domain name with URL path or IP address with path
- // http.begin(serverName);
- //
- // // Specify content-type header
- // http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- //
- // // Prepare your HTTP POST request data
- // //String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
- // // + "&location=" + sensorLocation + "&value1=" + String(bme.readTemperature())
- // // + "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";
- //
- //
- // // You can comment the httpRequestData variable above
- // // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
- // //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
- //
- // 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";
- //
- // //String httpRequestData = "detect_id=" + "1" + "&device_ip=" + esp32_ip + "&device_mac="+ esp32_mac + "&loc=" + sLocation + "";
- //
- //
- // // If you need an HTTP request with a content type: text/plain
- // //http.addHeader("Content-Type", "text/plain");
- // //int httpResponseCode = http.POST("Hello, World!");
- //
- // // If you need an HTTP request with a content type: application/json, use the following:
- // //http.addHeader("Content-Type", "application/json");
- // //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
- //
- // // Send HTTP POST request
- // int httpResponseCode = http.POST(httpRequestData);
- //
- // Serial.print("httpRequestData: ");
- // Serial.println(httpRequestData);
- //
- // if (httpResponseCode>0) {
- // Serial.print("HTTP Response code: ");
- // Serial.println(httpResponseCode);
- // }
- // else {
- // Serial.print("Error code: ");
- // Serial.println(httpResponseCode);
- // }
- // // Free resources
- // http.end();
- // }
- // else {
- // Serial.println("WiFi Disconnected");
- // }
- // //Send an HTTP POST request every 30 seconds
- // delay(500);
- // }
- String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." +
- WiFi.localIP()[3];
- String esp32_mac = String(WiFi.macAddress());
- if (buttonStateX == HIGH) {
- //Check WiFi connection status
- if(WiFi.status()== WL_CONNECTED){
- HTTPClient http;
- http.begin(serverName);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- //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";
- String httpRequestData = String("detect_id=") + 1 + String("&device_ip=") + LocalIP + String("&device_mac=")+ esp32_mac + String("&loc=") + sLocation + String("");
- //String httpRequestData = "detect_id=" + "1" + "&device_ip=" + LocalIP + "&device_mac="+ "" + "&loc=" + sLocation + "";
- int httpResponseCode = http.POST(httpRequestData);
- Serial.print("httpRequestData: ");
- Serial.println(httpRequestData);
- if (httpResponseCode>0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- }
- else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- http.end();
- }
- else {
- Serial.println("WiFi Disconnected");
- }
- delay(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement