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

Následující verze
Předchozí verze
Následující verzeObě strany příští revize
zarizeni:esp8266 [2016/05/30 10:01] – vytvořeno multitrickerzarizeni:esp8266 [2016/06/06 10:50] 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ů:** http://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 priklad> +===== Teplota =====
-// Kod pro tmep.cz, autor mikrom.cz +
-// Pokud máte např. v administraci zadané čidlo s doménou ahoj a GUID nastavené na mojemereni. +
-// http://ahoj.tmep.cz/?GUID=25.6+
  
-#include <ESP8266WiFi.h>+{{:zarizeni:esp:tmep_send_temperature.fzz|Schéma Fritzing}}
  
-#define WLAN_SSID "SSID" // WiFi SSID +{{:zarizeni:esp:tmep_send_temperature.png?500|}} 
-#define WLAN_PASS "HESLO" // WiFi password + 
-#define SERVER    "ahoj.tmep.cz" // domena.tmep.cz +<file c tmep_send_temperature.ino> 
-#define GUID      "mojemereni" // GUID+// 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 
 + 
 +#include <ESP8266WiFi.h>       // WiFi library 
 +#include <OneWire.h>           // OneWire communication library for DS18B20 
 +#include <DallasTemperature.h> // DS18B20 library 
 + 
 +// Define settings 
 +const char* ssid      = "---wifi ssid---"// WiFi SSID 
 +const char* pass      = "---wifi pass---"// WiFi password 
 +const char* domain    "---domain---";    // domain.tmep.cz 
 +const char* guid      "---guid---";      // mojemereni 
 +const long sleep      = 600000;            // How often send data to the server. Default is 600000ms = 10 minute 
 +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.
  
-// Use WiFiClient class to create TCP connections 
-WiFiClient client; 
-   
 void setup() { void setup() {
   // Start serial   // Start serial
Řádek 29: Řádek 45:
   Serial.println();   Serial.println();
  
-  // Connect to 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();
  
-  Serial.println("Ready for LOOP!");+  sensors.begin(); // Initialize the DallasTemperature DS18B20 class (not strictly necessary with the client class, but good practice). 
 +  delay(10000);
 } }
  
 void loop() { void loop() {
-  Serial.println("Connecting...");+  sensors.requestTemperatures();           // Send the command to get temperaturesrequest 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("Temp error!"); 
 +    return; 
 +  }
      
-  if (client.connect(SERVER80)) { +  // Connect to the HOST and send data via GET method 
-    Serial.println("Connected");+  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(hosthttpPort)) { 
 +    // If you didn't get a connection to the server 
 +    Serial.println("Connection failed"); 
 +    return; 
 +  } 
 +  Serial.println("Client connected");
  
-    int rndm random(10, 30); +  // Make an url. We need: /?guid=
-     +  String url = "/?"; 
-    // Make a HTTP request+         url += guid; 
-    client.print("GET /?"); +         url += "="; 
-    client.print(String(GUID)); // guid +         url += t; 
-    client.print("="); +  Serial.print("Requesting URL: "); Serial.println(url); 
-    client.print(rndm); // value +   
-    client.println(" HTTP/1.1"); +  // Make a HTTP GETrequest
-    client.print("Host:"); client.println(SERVER); +  client.print(String("GET ") + url + " HTTP/1.1\r\n"
-    client.println("Connection: close")+               "Host: " + host + "\r\n" +  
-    client.println(); +               "Connection: close\r\n\r\n"); 
-     + 
-    // Just for debuggingGET /?mojemereni=20 +  // Workaroud for timeout 
-    Serial.print(SERVER); Serial.print(" "); +  unsigned long timeout = millis(); 
-    Serial.print("GET /?"); +  while (client.available() == 0) { 
-    Serial.print(String(GUID)); +    if (millis() - timeout > 5000{ 
-    Serial.print("="); +      Serial.println(">>> Client Timeout !"); 
-    Serial.print(rndm); +      client.stop(); 
-    Serial.println(" HTTP/1.1"); +      return; 
-    Serial.print("Host:"); Serial.println(SERVER); +    
-    Serial.println("Connection: close"); +  } 
-    Serial.println(); + 
-     +  Serial.println(); 
-  } else +   
-    // if you didn't get a connection to the server:+  // Wait for another round 
 +  delay(sleep); 
 +
 +</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 
 +// 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 
 +// 
 +// Not tested, but should work! :) 
 + 
 +#include <ESP8266WiFi.h> // WiFi library 
 +#include "DHT.h"         // DHT sensor library 
 + 
 +// Define settings 
 +const char* ssid    = "---wifi ssid---"// WiFi SSID 
 +const char* pass    "---wifi pass---"; // WiFi password 
 +const char* domain  = "---domain---";    // domain.tmep.cz 
 +const char* guid    = "---guid---";      // mojemereni 
 +const long sleep    = 600000;            // How often send data to the serverDefault is 600000ms = 10 minute 
 +const byte dhtPin   = 5;                 // Pin where is DHT connected 
 +const byte dhtType  = 22;                // DHT type, for DHT11 = 11, for DHT22 22 
 + 
 +DHT dht(dhtPin, dhtType); // Initialize DHT sensor. 
 + 
 +void setup() { 
 +  // Start serial 
 +  Serial.begin(115200); 
 +  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(); 
 + 
 +  dht.begin(); // Initialize the DHT sensor 
 +  delay(10000); 
 +
 + 
 +void loop() { 
 +  // Reading temperature or humidity takes about 250 milliseconds! 
 +  // 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(); 
 + 
 +  // 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");     Serial.println("Connection failed");
 +    return;
   }   }
 +  Serial.println("Client connected");
  
-  delay(60000);+  // 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);
 } }
 </file> </file>