· 6 years ago · Oct 25, 2019, 10:56 AM
1/*
2 Rui Santos
3 Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
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
13
14 #include <WiFi.h>
15 #include <HTTPClient.h>
16/* #else
17 #include <ESP8266WiFi.h>
18 #include <ESP8266HTTPClient.h>
19 #include <WiFiClient.h>
20#endif
21
22#include <Wire.h> */
23
24
25// Replace with your network credentials
26const char* ssid = "testwifi";
27const char* password = "cmipl1234";
28const char* serverName = "http://katalystpower.com/post-esp-data.php";
29
30// Keep this API Key value to be compatible with the PHP code provided in the project page.
31// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
32String apiKeyValue = "tPmAT5Ab3j7F9";
33
34String sensorName = "razmaan";
35String sensorLocation = "fuckyou";
36
37
38int analog_value = 0 ;
39
40
41void setup() {
42 pinMode(2,OUTPUT);
43 Serial.begin(115200);
44
45 WiFi.begin(ssid, password);
46 Serial.println("Connecting");
47 while(WiFi.status() != WL_CONNECTED) {
48 delay(500);
49 Serial.print(".");
50 }
51
52 Serial.println("");
53 Serial.print("Connected to WiFi network with IP Address: ");
54 Serial.println(WiFi.localIP());
55
56 // (you can also pass in a Wire library object like &Wire2)
57 bool status = 1 ;
58 if (!status) {
59 Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
60 while (1);
61 }
62}
63
64void loop() {
65 //Check WiFi connection status
66 analog_value = analogRead(15);
67 delay(250);
68 if(WiFi.status()== WL_CONNECTED){
69
70
71 digitalWrite(2,HIGH);
72
73
74 HTTPClient http;
75
76 // Your Domain name with URL path or IP address with path
77 http.begin(serverName);
78
79 // Specify content-type header
80 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
81
82 // Prepare your HTTP POST request data
83 String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
84 + "&location=" + sensorLocation + "&value1=" + String(analog_value)
85 + "&value2=" + String(random(100, 300)) + "&value3=" + String(random(150, 360)) + "";
86 Serial.print("httpRequestData: ");
87 Serial.println(httpRequestData);
88 Serial.println(analog_value);
89
90 // You can comment the httpRequestData variable above
91 // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
92 //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
93
94 // Send HTTP POST request
95 int httpResponseCode = http.POST(httpRequestData);
96
97 // If you need an HTTP request with a content type: text/plain
98 //http.addHeader("Content-Type", "text/plain");
99 //int httpResponseCode = http.POST("Hello, World!");
100
101 // If you need an HTTP request with a content type: application/json, use the following:
102 //http.addHeader("Content-Type", "application/json");
103 //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
104
105 if (httpResponseCode>0) {
106 Serial.print("HTTP Response code: ");
107 Serial.println(httpResponseCode);
108 }
109 else {
110 Serial.print("Error code: ");
111 Serial.println(httpResponseCode);
112 }
113 // Free resources
114 http.end();
115 }
116 else {
117 Serial.println("WiFi Disconnected");
118 digitalWrite(2,LOW);
119 WiFi.begin(ssid, password);
120 }
121 //Send an HTTP POST request every 30 seconds
122 delay(5000);
123}