Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3.  
  4. #include <dht.h>
  5.  
  6. #define dataPin 4
  7. const char* ssid = "Dimitar";
  8. const char* password = "dimitar2";
  9. const int sensor_ID = 1;
  10. dht DHT;
  11.  
  12. void setup() {
  13.  
  14. Serial.begin(115200);
  15. delay(4000); //Delay needed before calling the WiFi.begin
  16.  
  17. WiFi.begin(ssid, password);
  18.  
  19. while (WiFi.status() != WL_CONNECTED) { //Check for the connection
  20. delay(1000);
  21. Serial.println("Connecting to WiFi..");
  22. }
  23.  
  24. Serial.println("Connected to the WiFi network");
  25. }
  26.  
  27. void loop() {
  28.  
  29. if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
  30.  
  31. HTTPClient http;
  32.  
  33. http.begin("http://45.137.148.253/panel/wp-json/sensor/receive/"); //Specify destination for HTTP request
  34. http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
  35.  
  36. int readData = DHT.read22(dataPin); // Reads the data from the sensor
  37. float t = DHT.temperature; // Gets the values of the temperature
  38. float h = DHT.humidity; // Gets the values of the humidity
  39.  
  40. String data = "H:";
  41. data.concat(h);
  42. data.concat(",T:");
  43. data.concat(t);
  44. data.concat(",ID:");
  45. data.concat(sensor_ID);
  46. data.concat(";");
  47. Serial.println(h);
  48. Serial.println(t);
  49. int httpResponseCode = http.POST(data); //Send the actual POST request
  50.  
  51. if(httpResponseCode>0){
  52.  
  53. String response = http.getString(); //Get the response to the request
  54.  
  55. Serial.println(httpResponseCode); //Print return code
  56. Serial.println(response); //Print request answer
  57.  
  58. }else{
  59.  
  60. Serial.print("Error on sending POST: ");
  61. Serial.println(httpResponseCode);
  62.  
  63. }
  64.  
  65. http.end(); //Free resources
  66.  
  67. }else{
  68.  
  69. Serial.println("Error in WiFi connection");
  70.  
  71. }
  72.  
  73. delay(600000); //Send a request every 10 minutes
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement