├── LICENSE └── esp8266-dht-thingspeak-logger.ino /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 by Daniel Eichhorn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /esp8266-dht-thingspeak-logger.ino: -------------------------------------------------------------------------------- 1 | /**The MIT License (MIT) 2 | 3 | Copyright (c) 2015 by Daniel Eichhorn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | See more at http://blog.squix.ch 24 | */ 25 | 26 | #include 27 | #include "DHT.h" 28 | 29 | 30 | /*************************** 31 | * Begin Settings 32 | **************************/ 33 | 34 | 35 | const char* ssid = "yourssid"; 36 | const char* password = "yourpassw0rd"; 37 | 38 | const char* host = "api.thingspeak.com"; 39 | 40 | const char* THINGSPEAK_API_KEY = "XXX"; 41 | 42 | // DHT Settings 43 | #define DHTPIN D6 // what digital pin we're connected to. If you are not using NodeMCU change D6 to real pin 44 | 45 | 46 | // Uncomment whatever type you're using! 47 | //#define DHTTYPE DHT11 // DHT 11 48 | #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 49 | //#define DHTTYPE DHT21 // DHT 21 (AM2301) 50 | 51 | const boolean IS_METRIC = true; 52 | 53 | // Update every 600 seconds = 10 minutes. Min with Thingspeak is ~20 seconds 54 | const int UPDATE_INTERVAL_SECONDS = 600; 55 | 56 | /*************************** 57 | * End Settings 58 | **************************/ 59 | 60 | // Initialize the temperature/ humidity sensor 61 | DHT dht(DHTPIN, DHTTYPE); 62 | 63 | void setup() { 64 | Serial.begin(115200); 65 | delay(10); 66 | 67 | // We start by connecting to a WiFi network 68 | 69 | Serial.println(); 70 | Serial.println(); 71 | Serial.print("Connecting to "); 72 | Serial.println(ssid); 73 | 74 | WiFi.begin(ssid, password); 75 | 76 | while (WiFi.status() != WL_CONNECTED) { 77 | delay(500); 78 | Serial.print("."); 79 | } 80 | 81 | Serial.println(""); 82 | Serial.println("WiFi connected"); 83 | Serial.println("IP address: "); 84 | Serial.println(WiFi.localIP()); 85 | } 86 | 87 | void loop() { 88 | Serial.print("connecting to "); 89 | Serial.println(host); 90 | 91 | // Use WiFiClient class to create TCP connections 92 | WiFiClient client; 93 | const int httpPort = 80; 94 | if (!client.connect(host, httpPort)) { 95 | Serial.println("connection failed"); 96 | return; 97 | } 98 | 99 | // read values from the sensor 100 | float humidity = dht.readHumidity(); 101 | float temperature = dht.readTemperature(!IS_METRIC); 102 | 103 | // We now create a URI for the request 104 | String url = "/update?api_key="; 105 | url += THINGSPEAK_API_KEY; 106 | url += "&field1="; 107 | url += String(temperature); 108 | url += "&field2="; 109 | url += String(humidity); 110 | 111 | Serial.print("Requesting URL: "); 112 | Serial.println(url); 113 | 114 | // This will send the request to the server 115 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 116 | "Host: " + host + "\r\n" + 117 | "Connection: close\r\n\r\n"); 118 | delay(10); 119 | while(!client.available()){ 120 | delay(100); 121 | Serial.print("."); 122 | } 123 | // Read all the lines of the reply from server and print them to Serial 124 | while(client.available()){ 125 | String line = client.readStringUntil('\r'); 126 | Serial.print(line); 127 | } 128 | 129 | Serial.println(); 130 | Serial.println("closing connection"); 131 | 132 | 133 | // Go back to sleep. If your sensor is battery powered you might 134 | // want to use deep sleep here 135 | delay(1000 * UPDATE_INTERVAL_SECONDS); 136 | } 137 | 138 | --------------------------------------------------------------------------------