manhoosbilli1

esp-32 showing post method

Nov 22nd, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include "DHT.h"
  3. #include <HTTPClient.h>
  4. #define DHTPIN 32
  5. const char* ssid = "PTCL-BB1";
  6. const char* password = "shoaibmectec";
  7. const char* host = "http://jsonplaceholder.typicode.com/posts"; //edit the host adress, ip address etc.
  8. String url = "/post/"; int adcvalue = 0;
  9. float t;
  10. #define DHTTYPE DHT22
  11. DHT dht(DHTPIN, DHTTYPE);
  12. void setup()
  13. {
  14.  
  15.   dht.begin();
  16.   Serial.begin(115200);
  17.   delay(10); // We start by connecting to a WiFi network
  18.   Serial.println();
  19.   Serial.println(); Serial.print("Connecting to ");
  20.   Serial.println(ssid);
  21.   WiFi.mode(WIFI_STA);
  22.   WiFi.begin(ssid, password);
  23.   while (WiFi.status() != WL_CONNECTED)
  24.   {
  25.     delay(500);
  26.     Serial.print(".");
  27.   }
  28.   Serial.println("");
  29.   Serial.println("WiFi connected");
  30.   Serial.println("IP address: ");
  31.   Serial.println(WiFi.localIP());
  32. }
  33. int value = 0;
  34. void loop()
  35. {
  36.   getSensorValues();   //susbstitute adc value
  37.   Serial.print("connecting to ");
  38.   Serial.println(host); // Use WiFiClient class to create TCP connections
  39.   WiFiClient client;
  40.   const int httpPort = 80;
  41.   if (!client.connect(host, httpPort))
  42.   {
  43.     Serial.println("connection failed");
  44.     return;
  45.   }
  46.   Serial.print("Requesting URL: ");
  47.   Serial.println(url); //Post Data
  48.   String postData = "temp=" + String(t);
  49.   String address = host + url;
  50.   HTTPClient http;
  51.   http.begin(address);
  52.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  53.   auto httpCode = http.POST(postData);
  54.   Serial.println(httpCode); //Print HTTP return code
  55.   String payload = http.getString();
  56.   Serial.println(payload); //Print request response payload
  57.   http.end(); //Close connection Serial.println();
  58.   Serial.println("closing connection");
  59.   delay(5000);
  60.  
  61. }
  62. void getSensorValues() {
  63.   float h = dht.readHumidity();
  64.   t = dht.readTemperature();
  65.   if (isnan(h) || isnan(t)) {
  66.     Serial.println(F("Failed to read from DHT sensor!")); //print to server
  67.     return;
  68.   }
  69. }
Add Comment
Please, Sign In to add comment