| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*
- * esp8266 CLOUD version chip
- * (with light sensor and LED light at the two corners)
- *
- *
- * gpio2 - small power/status led
- * a0 - analog read
- *
- */
- #include <ESP8266WiFi.h>
- #include <DHT.h>
- #define DHTTYPE DHT11 // DHT 11
- // DHT Sensor
- const int DHTPin = 2;
- // Initialize DHT sensor.
- DHT dht(DHTPin, DHTTYPE);
- const char* ssid = "hazard18";
- const char* password = "pu1f0xylu1";
- // Host
- const char* host = "dweet.io";
- // Temporary variables
- static char celsiusTemp[7];
- static char fahrenheitTemp[7];
- static char humidityTemp[7];
- int c = 0;
- void setup() {
- //pinMode(2, OUTPUT);
-
- Serial.begin(115200);
- delay(100);
- Serial.print("Starting... ");
- dht.begin();
- // 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());
- }
- float h,t,f;
- void loop() {
- 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;
- }
-
- h = dht.readHumidity();
- t = dht.readTemperature();
- f = dht.readTemperature(true);
- if (isnan(h) || isnan(t) || isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- strcpy(celsiusTemp,"Failed");
- strcpy(fahrenheitTemp, "Failed");
- strcpy(humidityTemp, "Failed");
- //digitalWrite(2, HIGH);
- client.print(String("GET /dweet/for/esp1881?tempC=") + String(celsiusTemp) + String(c++) +
- String("&tempF=") + String(fahrenheitTemp) +
- String("&humidity=") + String(humidityTemp) +
- " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
-
- }
- else{
- // Computes temperature values in Celsius + Fahrenheit and Humidity
-
-
- float hic = dht.computeHeatIndex(t, h, false);
- float hif = dht.computeHeatIndex(f, h);
- sprintf(celsiusTemp,"%f", t);
- sprintf(fahrenheitTemp, "%f", f);
- sprintf(humidityTemp, "%f", h);
-
-
- Serial.println(celsiusTemp);
- Serial.println(fahrenheitTemp);
- Serial.println(humidityTemp);
- // Serial.println(String("GET /dweet/for/esp1881?tempC=") + String(celsiusTemp) +
- // String("&tempF=") + String(fahrenheitTemp) +
- // String("&humidity=") + String(humidityTemp) +
- // " HTTP/1.1\r\n" +
- // "Host: " + host + "\r\n" +
- // "Connection: close\r\n\r\n");
-
- client.print(String("GET /dweet/for/esp1881?tempC=") + String(hic, 2) +
- String("&tempF=") + String(hif, 2) +
- String("&humidity=") + String(h, 2) +
- " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- }
- delay(100);
- //digitalWrite(2, LOW);
-
- // 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);
- }
-
- //}
- delay(500);
-
- }
|