Uživatelské nástroje

Nástroje pro tento web


zarizeni:esp8266

Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

Obě strany předchozí revizePředchozí verze
Následující verze
Předchozí verze
zarizeni:esp8266 [2016/05/30 10:07] multitrickerzarizeni:esp8266 [2023/02/21 13:26] (aktuální) multitricker
Řádek 3: Řádek 3:
 Příklad jak naprogramovat ESP8266 v Arduino IDE od ''mikroma''. Příklad jak naprogramovat ESP8266 v Arduino IDE od ''mikroma''.
  
-**Web autora příkladu:** http://mikrom.cz/\\ +**Web autora příkladů:** https://mikrom.cz/\\ 
-**Kontakt na autora:** mikrom@mikrom.cz+**Kontakt na autora:** [[mikrom@mikrom.cz]]
  
 Nezapomeňte na začátku kódu nastavit své správné proměnné (SSID vlastní wifi, heslo k wifi síti, správnou adresu serveru a GUID vyplněné v administraci na tmep.cz). Nezapomeňte na začátku kódu nastavit své správné proměnné (SSID vlastní wifi, heslo k wifi síti, správnou adresu serveru a GUID vyplněné v administraci na tmep.cz).
  
-<file c tmep.ino>+===== Teplota ===== 
 + 
 +{{:zarizeni:esp:tmep_send_temperature.fzz|Schéma Fritzing}} 
 + 
 +{{:zarizeni:esp:tmep_send_temperature.png?500|}} 
 + 
 +<file c tmep_send_temperature.ino> 
 +/* 
 +  Simple sketch for sending data to the TMEP.cz 
 +  by mikrom (http://www.mikrom.cz) 
 +  http://wiki.tmep.cz/doku.php?id=zarizeni:esp8266 
 + 
 +  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 DHT11 or DHT22 sensor and code will need some modifications, see the other sketch. 
 + 
 +  Blinking with ESP internal LED according status: 
 +  LED constantly blinking = connecting to WiFi 
 +  LED on for 5 seconds = boot, setup, WiFi connect OK 
 +  LED blink 2x per second = temp error (wrong pin, bad sensor, read -127.00°C) 
 +  LED blink 3x per second = connect to host failed 
 +  LED blink 4x per second = client connection timeout 
 +  LED blink 1x per [sleep] = send data ok 
 +*/ 
 + 
 +#include <ESP8266WiFi.h>       // WiFi library 
 +#include <OneWire.h>           // OneWire communication library for DS18B20 
 +#include <DallasTemperature.h> // DS18B20 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 byte sleep      = 1; // How often send data to the server. In minutes 
 +const byte oneWireBus = 5; // Pin where is DS18B20 connected 
 + 
 +// Create Temperature object "sensors" 
 +OneWire oneWire(oneWireBus);         // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
 +DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature. 
 + 
 +void setup() { 
 +  pinMode(2, OUTPUT); // GPIO2, LED on ESP8266 
 +   
 +  // Start serial 
 +  Serial.begin(115200); 
 +  delay(10); 
 +  Serial.println(); 
 + 
 +  // Connect to the WiFi 
 +  Serial.print(F("Connecting to ")); Serial.println(ssid); 
 +  WiFi.begin(ssid, pass); 
 +  while (WiFi.status() != WL_CONNECTED) { 
 +    digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); // Blinking with LED during connecting to WiFi 
 +    delay(500); 
 +    //digitalWrite(2, HIGH); // Blinking with LED during connecting to WiFi 
 +    Serial.print(F(".")); 
 +  } 
 +  Serial.println(); 
 +  Serial.println(F("WiFi connected")); 
 +  Serial.print(F("IP address: ")); Serial.println(WiFi.localIP()); 
 +  Serial.println(); 
 + 
 +  sensors.begin(); // Initialize the DallasTemperature DS18B20 class (not strictly necessary with the client class, but good practice). 
 + 
 +  // LED on for 5 seconds after setup is done 
 +  digitalWrite(2, LOW); 
 +  delay(5000); 
 +  digitalWrite(2, HIGH); 
 +
 + 
 +void loop() { 
 +  sensors.requestTemperatures();        // Send the command to get temperatures. request to all devices on the bus 
 +  float t = sensors.getTempCByIndex(0); // Read temperature in "t" variable 
 +  if (t == -127.00) {                   // If you have connected it wrong, Dallas read this temperature! :) 
 +    Serial.println(F("Temp error!")); 
 +    // Blink 2 time when temp error 
 +    digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); 
 +    delay(1000); 
 +    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" 
 +   
 +  Serial.print(F("Connecting to ")); Serial.println(host); 
 +  if (!client.connect(host, 80)) { 
 +    // If you didn't get a connection to the server 
 +    Serial.println(F("Connection failed")); 
 +    // Blink 3 times when host connection error 
 +    digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); 
 +    delay(1000); 
 +    return; 
 +  } 
 +  Serial.println(F("Client connected")); 
 + 
 +  // Make an url. We need: /?guid=t 
 +  String url = "/?"; 
 +         url += guid; 
 +         url += "="; 
 +         url += t; 
 +  Serial.print(F("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"); 
 + 
 +  // Blik 1 time when send OK 
 +  digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); 
 +   
 +  // Workaroud for timeout 
 +  unsigned long timeout = millis(); 
 +  while (client.available() == 0) { 
 +    if (millis() - timeout > 5000) { 
 +      Serial.println(F(">>> Client Timeout !")); 
 +      client.stop(); 
 +      // Blink 4 times when client timeout 
 +      digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); delay(100); digitalWrite(2, LOW); delay(100); digitalWrite(2, HIGH); 
 +      delay(1000); 
 +      return; 
 +    } 
 +  } 
 + 
 +  Serial.println(); 
 +   
 +  // Wait for another round 
 +  delay(sleep*60000); 
 +
 +</file> 
 + 
 +===== Teplota a vlhkost ===== 
 + 
 +[[https://learn.adafruit.com/dht/overview 
 +|Informace o senzorech.]] 
 + 
 +{{:zarizeni:esp:tmep_send_temperature_and_humidity.fzz|Schéma Fritzing}} 
 + 
 +{{:zarizeni:esp:tmep_send_temperature_and_humidity.png?500|}} 
 + 
 +<file c tmep_send_temperature_and_humidity.ino>
 // Simple sketch for sending data to the TMEP.cz // Simple sketch for sending data to the TMEP.cz
 // by mikrom (http://www.mikrom.cz) // by mikrom (http://www.mikrom.cz)
 // http://wiki.tmep.cz/doku.php?id=zarizeni:esp8266 // http://wiki.tmep.cz/doku.php?id=zarizeni:esp8266
 +//
 // 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 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 // If you send also humidity url will be: http://ahoj.tmep.cz/?mojemereni=25.6&humV=60
-// Another nice example with bulding HTTP GET request is here: https://github.com/SensorsIot/ESP8266-Longterm-Sensor-Hourly/blob/master/Sparkfun.ino+// but for humidity You will need DHT11 or DHT22 sensor and code will need some modifications 
 +// 
 +// Not tested, but should work! :)
  
-// Include WiFi stuff +#include <ESP8266WiFi.h> // WiFi library 
-#include <ESP8266WiFi.h> +#include "DHT.h        // DHT sensor library
- +
-// Include Temperature stuff for DS18B20 +
-#include <OneWire.h+
-#include <DallasTemperature.h>+
  
 // Define settings // Define settings
-#define WLAN_SSID    "---wifi ssid---"      // WiFi SSID +const char* ssid    "---wifi ssid---"// WiFi SSID 
-#define WLAN_PASS    "---wifi password---"  // WiFi password +const char* pass    "---wifi pass---"// WiFi password 
-#define HOST         "---domain---.tmep.cz" // domena.tmep.cz +const char* domain "---domain---";    // domain.tmep.cz 
-#define GUID         "---guid---"           // mojemereni +const char* guid    = "---guid---";      // mojemereni 
-#define DELAY        60000                  // How often send data to the server. Default is 60000ms minute +const long sleep    = 600000;            // How often send data to the server. Default is 600000ms 10 minute 
-#define ONE_WIRE_BUS                      // Pin where is DS18B20 connected+const byte dhtPin   5;                 // Pin where is DHT connected 
 +const byte dhtType  = 22;                // DHT type, for DHT11 = 11, for DHT22 = 22
  
-// Use WiFiClient class to create TCP connections +DHT dht(dhtPin, dhtType); // Initialize DHT sensor.
-WiFiClient client; +
- +
-// Create Temperature object "sensors" +
-OneWire oneWire(ONE_WIRE_BUS);       // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) +
-DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.+
  
 void setup() { void setup() {
Řádek 45: Řádek 184:
  
   // Connect to the WiFi   // Connect to the WiFi
-  Serial.print("Connecting to "); Serial.println(WLAN_SSID); +  Serial.print("Connecting to "); Serial.println(ssid); 
-  WiFi.begin(WLAN_SSIDWLAN_PASS);+  WiFi.begin(ssidpass);
   while (WiFi.status() != WL_CONNECTED) {   while (WiFi.status() != WL_CONNECTED) {
     delay(500);     delay(500);
     Serial.print(".");     Serial.print(".");
   }   }
 +  Serial.println();
   Serial.println("WiFi connected");   Serial.println("WiFi connected");
   Serial.print("IP address: "); Serial.println(WiFi.localIP());   Serial.print("IP address: "); Serial.println(WiFi.localIP());
 +  Serial.println();
  
-  // Initialize the DallasTemperature DS18B20 class (not strictly necessary with the client class, but good practice). +  dht.begin(); // Initialize the DHT sensor 
-  sensors.begin();  +  delay(10000);
- +
-  Serial.println("Ready for LOOP!");+
 } }
  
 void loop() { void loop() {
-  // Send the command to get temperaturesrequest to all devices on the bus +  // Reading temperature or humidity takes about 250 milliseconds! 
-  sensors.requestTemperatures();+  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 
 +  float h = dht.readHumidity(); 
 +  // Read temperature as Celsius (the default) 
 +  float t = dht.readTemperature();
  
-  // Read temperature in "temp" variable +  // Check if any reads failed and exit early (to try again). 
-  float temp = sensors.getTempCByIndex(0);+  if (isnan(h) || isnan(t)) { 
 +    Serial.println("Failed to read from DHT sensor!"); 
 +    return; 
 +  }
      
-  // Connect to the HOST and send data via GET method   +  // Connect to the HOST and send data via GET method 
-  if (client.connect(HOST, 80)) { +  WiFiClient client; // Use WiFiClient class to create TCP connections 
-    Serial.println("Connected"); +   
-  +  char host[50]           // Joining two chars is little bit difficultMake new array, 50 bytes long 
-    // Make a HTTP request. +  strcpy(host, domain);     // Copy /domainin to the /host/ 
-    client.print("GET /?"); +  strcat(host, ".tmep.cz"); // Add ".tmep.cz" at the end of the /host/. /host/ is now "/domain/.tmep.cz" 
-    client.print(GUID); // guid: "mojemereni" +  const int httpPort = 80
-    client.print("="); +   
-    client.print(temp); // temperature value +  Serial.print("Connecting to "); Serial.println(host); 
-    //client.print("&humV="); +  if (!client.connect(host, httpPort)) { 
-    //client.print(60); // humidity value +    // If you didn't get a connection to the server
-    client.println(HTTP/1.1")+
-    client.print("Host:"); client.println(HOST); +
-    client.println("Connection: close"); +
-    client.println();     +
-  } else +
-    // If you didn't get a connection to the server:+
     Serial.println("Connection failed");     Serial.println("Connection failed");
 +    return;
   }   }
 +  Serial.println("Client connected");
  
-  // Wait for DELAY, default 1 minute +  // Make an url. We need: /?guid=t&humV=h 
-  delay(DELAY);+  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);
 } }
 </file> </file>