Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sensors.begin();#include <WiFi.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Arduino.h>
- #include "DHT.h"
- #define DHTPIN 32
- #define oneWireBus 33
- #define DHTTYPE DHT22
- #define moistPin 35
- #define LDRPin 34
- int LDRValue = 0;
- int moistVal;
- float soilTemp, t, h, moisture, light;
- DHT dht(DHTPIN, DHTTYPE);
- OneWire oneWire(oneWireBus);
- DallasTemperature sensors(&oneWire);
- String apiKey = "I0ESQ19COY10AIUJ"; // Enter your Write API key from ThingSpeak
- const char *ssid = "Mi A2 Lite-Shoaib"; // replace with your wifi ssid and wpa2 key
- const char *pass = "D01234567890";
- const char* server = "api.thingspeak.com";
- WiFiClient client;
- void setup()
- {
- Serial.begin(115200);
- delay(10);
- dht.begin();
- sensors.begin();
- Serial.println("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- }
- void loop() {
- getSensorValues();
- delay(15000);
- if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
- {
- Serial.println("Connected to server");
- String postStr = apiKey;
- postStr += "&field1=";
- postStr += String(soilTemp);
- postStr += "&field2=";
- postStr += String(t);
- postStr += "&field3=";
- postStr += String(h);
- postStr += "&field4=";
- postStr += String(moisture);
- postStr += "&field5=";
- postStr += String(light);
- postStr += "\r\n\r\n";
- client.print("POST /update HTTP/1.1\n");
- client.print("Host: api.thingspeak.com\n");
- client.print("Connection: close\n");
- client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
- client.print("Content-Type: application/x-www-form-urlencoded\n");
- client.print("Content-Length: ");
- client.print(postStr.length());
- client.print("\n\n");
- client.print(postStr);
- Serial.println(postStr);
- Serial.println("Posted");
- }
- client.stop();
- }
- void getSensorValues() {
- sensors.requestTemperatures();
- soilTemp = sensors.getTempCByIndex(0);
- h = dht.readHumidity();
- t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println(F("Failed to read from DHT sensor!")); //print to server
- return;
- }
- light = analogRead(LDRPin); // read the value from the LDR
- moisture = analogRead(moistPin);
- }
Add Comment
Please, Sign In to add comment