Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include "DHT.h"
- #include <HTTPClient.h>
- #define DHTPIN 32
- const char* ssid = "PTCL-BB1";
- const char* password = "shoaibmectec";
- const char* host = "http://jsonplaceholder.typicode.com/posts"; //edit the host adress, ip address etc.
- String url = "/post/"; int adcvalue = 0;
- float t;
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- void setup()
- {
- dht.begin();
- Serial.begin(115200);
- delay(10); // We start by connecting to a WiFi network
- Serial.println();
- Serial.println(); Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- int value = 0;
- void loop()
- {
- getSensorValues(); //susbstitute adc value
- Serial.print("connecting to ");
- Serial.println(host); // Use WiFiClient class to create TCP connections
- WiFiClient client;
- const int httpPort = 80;
- if (!client.connect(host, httpPort))
- {
- Serial.println("connection failed");
- return;
- }
- Serial.print("Requesting URL: ");
- Serial.println(url); //Post Data
- String postData = "temp=" + String(t);
- String address = host + url;
- HTTPClient http;
- http.begin(address);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- auto httpCode = http.POST(postData);
- Serial.println(httpCode); //Print HTTP return code
- String payload = http.getString();
- Serial.println(payload); //Print request response payload
- http.end(); //Close connection Serial.println();
- Serial.println("closing connection");
- delay(5000);
- }
- void getSensorValues() {
- float h = dht.readHumidity();
- t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println(F("Failed to read from DHT sensor!")); //print to server
- return;
- }
- }
Add Comment
Please, Sign In to add comment