WeMos D1 + HTU21D

Úpravu skriptu od mikroma pro WeMos D1 s čidlem HTU21D provedl Martin Čapek Martin_Capek@seznam.cz

// Simple sketch for sending data to the TMEP.cz
// by mikrom (http://www.mikrom.cz)
// http://wiki.tmep.cz/doku.php?id=zarizeni:esp8266
// modify by Martin_Capek@seznam.cz
// If you send only temperature url will be: http://ahoj.tmep.cz/?mojemereni=25.6 (domain is "ahoj" and guid is "mojemereni"
// If you send also humidity url will be: http://ahoj.tmep.cz/?mojemereni=25.6&humV=60
// but for humidity You will need HTU21D or Si7021 (not tested) sensor and code will need some modifications
//
// Not tested, but should work! :)
 
#include <ESP8266WiFi.h> // WiFi library
#include <Wire.h>
#include <Sodaq_SHT2x.h>       // HTU21D library
 
#include <ESP8266WiFi.h> // WiFi library
#include <Wire.h>
#include <Sodaq_SHT2x.h>       // HTU21D library
 
// Define settings
 
const char ssid[]     = "---ssid---"; // WiFi SSID
const char pass[]     = "---password---"; // WiFi password
const char domain[]   = "---domain---";  // domain.tmep.cz
const char guid[]     = "---guid---"; // mojemereni
const long sleep      = 60000;             // How often send data to the server. Default is 60000ms = 60 seconds
 
void setup() {
    // Start serial
    Wire.begin();
    Serial.begin(9600);
    delay(10);
    Serial.println();
 
    // Connect to the WiFi
    Serial.print("Connecting to "); Serial.println(ssid);
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println();
    Serial.println("WiFi connected");
    Serial.print("IP address: "); Serial.println(WiFi.localIP());
    Serial.println();
 
    delay(10000);
}
 
void loop() {
    // Reading temperature or humidity from HTU21D
    float h = SHT2x.GetHumidity();
    // Read temperature as Celsius (the default)
    float t = SHT2x.GetTemperature();
 
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
 
    // Connect to the HOST and send data via GET method
    WiFiClient client; // Use WiFiClient class to create TCP connections
 
    char host[50];            // Joining two chars is little bit difficult. Make new array, 50 bytes long
    strcpy(host, domain);     // Copy /domain/ in to the /host/
    strcat(host, ".tmep.cz"); // Add ".tmep.cz" at the end of the /host/. /host/ is now "/domain/.tmep.cz"
    const int httpPort = 80;
 
    Serial.print("Connecting to "); Serial.println(host);
    if (!client.connect(host, httpPort)) {
      // If you didn't get a connection to the server
      Serial.println("Connection failed");
      return;
    }
    Serial.println("Client connected");
 
    // Make an url. We need: /?guid=t&humV=h
    String url = "/?";
           url += guid;
           url += "=";
           url += t;
           url += "&humV=";
           url += h;
    Serial.print("Requesting URL: "); Serial.println(url);
 
    // Make a HTTP GETrequest.
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
 
    // Workaroud for timeout
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
 
    Serial.println();
 
    // Wait for another round
    delay(sleep);
}