zarizeni:esp8266
Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Následující verze | Předchozí verze | ||
| zarizeni:esp8266 [2016/05/30 10:01] – vytvořeno multitricker | zarizeni:esp8266 [2023/02/21 13:26] (aktuální) – multitricker | ||
|---|---|---|---|
| Řádek 3: | Řádek 3: | ||
| Příklad jak naprogramovat ESP8266 v Arduino IDE od '' | Příklad jak naprogramovat ESP8266 v Arduino IDE od '' | ||
| - | **Web autora příkladu:** http:// | + | **Web autora příkladů:** https:// |
| - | **Kontakt na autora:** mikrom@mikrom.cz | + | **Kontakt na autora: |
| 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:// | + | |
| - | #include < | + | {{: |
| - | #define WLAN_SSID " | + | {{: |
| - | #define WLAN_PASS " | + | |
| - | #define SERVER | + | |
| - | #define GUID " | + | |
| - | // Use WiFiClient class to create TCP connections | + | <file c tmep_send_temperature.ino> |
| - | WiFiClient | + | /* |
| + | Simple sketch for sending data to the TMEP.cz | ||
| + | by mikrom (http:// | ||
| + | http:// | ||
| + | |||
| + | If you send only temperature url will be: http:// | ||
| + | If you send also humidity url will be: http:// | ||
| + | But for humidity You will need DHT11 or DHT22 sensor and code will need some modifications, | ||
| + | |||
| + | Blinking with ESP internal LED according status: | ||
| + | LED constantly blinking = connecting | ||
| + | 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 | ||
| + | LED blink 1x per [sleep] = send data ok | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Define settings | ||
| + | const char ssid[] | ||
| + | const char pass[] | ||
| + | const char domain[] | ||
| + | const char guid[] | ||
| + | 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 " | ||
| + | OneWire oneWire(oneWireBus); | ||
| + | DallasTemperature sensors(& | ||
| + | |||
| + | 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(" | ||
| + | WiFi.begin(ssid, | ||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | digitalWrite(2, | ||
| + | delay(500); | ||
| + | // | ||
| + | Serial.print(F(" | ||
| + | } | ||
| + | Serial.println(); | ||
| + | Serial.println(F(" | ||
| + | Serial.print(F(" | ||
| + | Serial.println(); | ||
| + | |||
| + | sensors.begin(); | ||
| + | |||
| + | // LED on for 5 seconds after setup is done | ||
| + | digitalWrite(2, | ||
| + | delay(5000); | ||
| + | digitalWrite(2, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | sensors.requestTemperatures(); | ||
| + | float t = sensors.getTempCByIndex(0); | ||
| + | if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :) | ||
| + | Serial.println(F(" | ||
| + | // Blink 2 time when temp error | ||
| + | digitalWrite(2, | ||
| + | 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]; | ||
| + | strcpy(host, | ||
| + | strcat(host, | ||
| + | | ||
| + | Serial.print(F(" | ||
| + | if (!client.connect(host, | ||
| + | // If you didn't get a connection to the server | ||
| + | Serial.println(F(" | ||
| + | // Blink 3 times when host connection error | ||
| + | digitalWrite(2, | ||
| + | delay(1000); | ||
| + | return; | ||
| + | } | ||
| + | Serial.println(F(" | ||
| + | |||
| + | // Make an url. We need: /?guid=t | ||
| + | String url = "/?"; | ||
| + | url += guid; | ||
| + | url += " | ||
| + | url += t; | ||
| + | Serial.print(F(" | ||
| + | | ||
| + | // Make a HTTP GETrequest. | ||
| + | client.print(String(" | ||
| + | " | ||
| + | " | ||
| + | |||
| + | // Blik 1 time when send OK | ||
| + | digitalWrite(2, | ||
| + | | ||
| + | // Workaroud for timeout | ||
| + | unsigned long timeout = millis(); | ||
| + | while (client.available() == 0) { | ||
| + | if (millis() - timeout > 5000) { | ||
| + | Serial.println(F(">>> | ||
| + | client.stop(); | ||
| + | // Blink 4 times when client timeout | ||
| + | digitalWrite(2, | ||
| + | delay(1000); | ||
| + | return; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Serial.println(); | ||
| + | | ||
| + | // Wait for another round | ||
| + | delay(sleep*60000); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Teplota a vlhkost ===== | ||
| + | |||
| + | [[https:// | ||
| + | |Informace o senzorech.]] | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <file c tmep_send_temperature_and_humidity.ino> | ||
| + | // Simple sketch for sending data to the TMEP.cz | ||
| + | // by mikrom (http:// | ||
| + | // http:// | ||
| + | // | ||
| + | // If you send only temperature url will be: http:// | ||
| + | // If you send also humidity url will be: http:// | ||
| + | // but for humidity You will need DHT11 or DHT22 sensor and code will need some modifications | ||
| + | // | ||
| + | // Not tested, but should work! :) | ||
| + | |||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | // Define settings | ||
| + | const char* ssid = " | ||
| + | const char* pass = " | ||
| + | const char* domain | ||
| + | const char* guid = " | ||
| + | const long sleep = 600000; | ||
| + | const byte dhtPin | ||
| + | const byte dhtType | ||
| + | |||
| + | DHT dht(dhtPin, dhtType); // Initialize DHT sensor. | ||
| + | |||
| void setup() { | void setup() { | ||
| // Start serial | // Start serial | ||
| Řádek 29: | Řádek 183: | ||
| Serial.println(); | Serial.println(); | ||
| - | // Connect to WiFi | + | // Connect to the WiFi |
| - | Serial.print(" | + | Serial.print(" |
| - | WiFi.begin(WLAN_SSID, WLAN_PASS); | + | WiFi.begin(ssid, pass); |
| while (WiFi.status() != WL_CONNECTED) { | while (WiFi.status() != WL_CONNECTED) { | ||
| delay(500); | delay(500); | ||
| Serial.print(" | Serial.print(" | ||
| } | } | ||
| + | Serial.println(); | ||
| Serial.println(" | Serial.println(" | ||
| Serial.print(" | Serial.print(" | ||
| + | Serial.println(); | ||
| - | | + | |
| + | delay(10000); | ||
| } | } | ||
| void loop() { | void loop() { | ||
| - | | + | |
| - | | + | // Sensor readings may also be up to 2 seconds ' |
| - | if (client.connect(SERVER, 80)) { | + | float h = dht.readHumidity(); |
| - | | + | |
| + | float t = dht.readTemperature(); | ||
| - | int rndm = random(10, 30); | + | |
| - | + | | |
| - | | + | |
| - | client.print("GET /?"); | + | |
| - | | + | } |
| - | | + | |
| - | | + | |
| - | client.println(" | + | |
| - | | + | |
| - | | + | char host[50]; // Joining two chars is little bit difficult. Make new array, 50 bytes long |
| - | client.println(); | + | |
| - | + | | |
| - | | + | const int httpPort = 80; |
| - | | + | |
| - | Serial.print(" | + | |
| - | Serial.print(String(GUID)); | + | |
| - | Serial.print("="); | + | // If you didn't get a connection to the server |
| - | Serial.print(rndm); | + | |
| - | Serial.println(" | + | |
| - | Serial.print(" | + | |
| - | | + | |
| - | Serial.println(); | + | |
| - | + | ||
| - | } else { | + | |
| - | // if you didn't get a connection to the server: | + | |
| Serial.println(" | Serial.println(" | ||
| + | return; | ||
| } | } | ||
| + | Serial.println(" | ||
| - | delay(60000); | + | |
| + | String url = "/?"; | ||
| + | url += guid; | ||
| + | url += " | ||
| + | url += t; | ||
| + | url += "& | ||
| + | url += h; | ||
| + | Serial.print(" | ||
| + | |||
| + | // Make a HTTP GETrequest. | ||
| + | client.print(String(" | ||
| + | " | ||
| + | " | ||
| + | |||
| + | // Workaroud for timeout | ||
| + | unsigned long timeout = millis(); | ||
| + | while (client.available() == 0) { | ||
| + | if (millis() - timeout > 5000) { | ||
| + | Serial.println(">>> | ||
| + | client.stop(); | ||
| + | return; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Serial.println(); | ||
| + | |||
| + | // Wait for another round | ||
| + | | ||
| } | } | ||
| </ | </ | ||