Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This code sends data to thingspeak.com
- Hardware: esp8266, DHT11
- DHT11 data pin connected to GPIO2 on the esp8266
- Compiled on Arduino IDE by Arduino.cc
- Alexshu.com
- DHT Library: https://github.com/adafruit/DHT-sensor-library
- */
- #include "DHT.h"
- #include <ESP8266WiFi.h>
- const char* ssid = "SSID"; // CHANGE THIS
- const char* password = "PASSWORD"; // CHANGE THIS
- const char* host = "api.thingspeak.com";
- const char* privateKey = "KEY"; // CHANGE THIS
- DHT dht(2, DHT11, 20);
- void setup() {
- Serial.begin(115200);
- dht.begin();
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- 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() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- 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;
- }
- // We now create a URI for the request
- String url = "/update";
- url += "?key=";
- url += privateKey;
- url += "&field1=";
- url += t;
- url += "&field2=";
- url += h;
- Serial.print("Requesting URL: ");
- Serial.println(url);
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(200);
- // Read all the lines of the reply from server and print them to Serial
- while(client.available()){
- String line = client.readStringUntil('\r');
- Serial.print(line);
- }
- Serial.println();
- Serial.println("closing connection");
- delay(60000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement