Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <dht.h>
- #define dataPin 4
- const char* ssid = "Dimitar";
- const char* password = "dimitar2";
- const int sensor_ID = 1;
- dht DHT;
- void setup() {
- Serial.begin(115200);
- delay(4000); //Delay needed before calling the WiFi.begin
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) { //Check for the connection
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- Serial.println("Connected to the WiFi network");
- }
- void loop() {
- if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
- HTTPClient http;
- http.begin("http://45.137.148.253/panel/wp-json/sensor/receive/"); //Specify destination for HTTP request
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
- int readData = DHT.read22(dataPin); // Reads the data from the sensor
- float t = DHT.temperature; // Gets the values of the temperature
- float h = DHT.humidity; // Gets the values of the humidity
- String data = "H:";
- data.concat(h);
- data.concat(",T:");
- data.concat(t);
- data.concat(",ID:");
- data.concat(sensor_ID);
- data.concat(";");
- Serial.println(h);
- Serial.println(t);
- int httpResponseCode = http.POST(data); //Send the actual POST request
- if(httpResponseCode>0){
- String response = http.getString(); //Get the response to the request
- Serial.println(httpResponseCode); //Print return code
- Serial.println(response); //Print request answer
- }else{
- Serial.print("Error on sending POST: ");
- Serial.println(httpResponseCode);
- }
- http.end(); //Free resources
- }else{
- Serial.println("Error in WiFi connection");
- }
- delay(600000); //Send a request every 10 minutes
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement