zarizeni:esp8266
Rozdíly
Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Obě strany předchozí revizePředchozí verzeNásledující verze | Předchozí verze | ||
| zarizeni:esp8266 [2016/06/06 10:50] – 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říkladů: | + | **Web autora příkladů: |
| **Kontakt na autora:** [[mikrom@mikrom.cz]] | **Kontakt na autora:** [[mikrom@mikrom.cz]] | ||
| Řádek 15: | Řádek 15: | ||
| <file c tmep_send_temperature.ino> | <file c tmep_send_temperature.ino> | ||
| - | // Simple sketch for sending data to the TMEP.cz | + | /* |
| - | // by mikrom (http:// | + | |
| - | // http:// | + | by mikrom (http:// |
| - | // | + | http:// |
| - | // If you send only temperature url will be: http:// | + | |
| - | // If you send also humidity url will be: http:// | + | If you send only temperature url will be: http:// |
| - | // but for humidity You will need DHT11 or DHT22 sensor and code will need some modifications | + | If you send also humidity url will be: http:// |
| + | | ||
| + | |||
| + | 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 < | #include < | ||
| Řádek 28: | Řádek 38: | ||
| // Define settings | // Define settings | ||
| - | const char* ssid = "---wifi ssid---"; | + | const char ssid[] = " |
| - | const char* pass = "---wifi pass---"; // WiFi password | + | const char pass[] = "---password---"; // WiFi password |
| - | const char* domain | + | const char domain[] = " |
| - | const char* guid = " | + | const char guid[] = " |
| - | const long sleep = 600000; // How often send data to the server. | + | const byte sleep = 1; // How often send data to the server. |
| - | const byte oneWireBus = 5; | + | const byte oneWireBus = 5; // Pin where is DS18B20 connected |
| // Create Temperature object " | // Create Temperature object " | ||
| - | OneWire oneWire(oneWireBus); | + | OneWire oneWire(oneWireBus); |
| DallasTemperature sensors(& | DallasTemperature sensors(& | ||
| void setup() { | void setup() { | ||
| + | pinMode(2, OUTPUT); // GPIO2, LED on ESP8266 | ||
| + | | ||
| // Start serial | // Start serial | ||
| Serial.begin(115200); | Serial.begin(115200); | ||
| Řádek 46: | Řádek 58: | ||
| // Connect to the WiFi | // Connect to the WiFi | ||
| - | Serial.print(" | + | Serial.print(F(" |
| WiFi.begin(ssid, | WiFi.begin(ssid, | ||
| while (WiFi.status() != WL_CONNECTED) { | while (WiFi.status() != WL_CONNECTED) { | ||
| + | digitalWrite(2, | ||
| delay(500); | delay(500); | ||
| - | Serial.print(" | + | |
| + | | ||
| } | } | ||
| Serial.println(); | Serial.println(); | ||
| - | Serial.println(" | + | Serial.println(F("WiFi connected" |
| - | Serial.print(" | + | Serial.print(F("IP address: ")); Serial.println(WiFi.localIP()); |
| Serial.println(); | Serial.println(); | ||
| sensors.begin(); | sensors.begin(); | ||
| - | | + | |
| + | // LED on for 5 seconds after setup is done | ||
| + | digitalWrite(2, | ||
| + | | ||
| + | digitalWrite(2, | ||
| } | } | ||
| void loop() { | void loop() { | ||
| - | sensors.requestTemperatures(); | + | sensors.requestTemperatures(); |
| float t = sensors.getTempCByIndex(0); | float t = sensors.getTempCByIndex(0); | ||
| if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :) | if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :) | ||
| - | Serial.println(" | + | Serial.println(F("Temp error!" |
| + | // Blink 2 time when temp error | ||
| + | digitalWrite(2, | ||
| + | delay(1000); | ||
| return; | return; | ||
| } | } | ||
| Řádek 75: | Řádek 96: | ||
| strcpy(host, | strcpy(host, | ||
| strcat(host, | strcat(host, | ||
| - | const int httpPort = 80; | ||
| | | ||
| - | Serial.print(" | + | Serial.print(F(" |
| - | if (!client.connect(host, | + | if (!client.connect(host, |
| // If you didn't get a connection to the server | // If you didn't get a connection to the server | ||
| - | Serial.println(" | + | Serial.println(F(" |
| + | // Blink 3 times when host connection error | ||
| + | digitalWrite(2, | ||
| + | delay(1000); | ||
| return; | return; | ||
| } | } | ||
| - | Serial.println(" | + | Serial.println(F(" |
| // Make an url. We need: /?guid=t | // Make an url. We need: /?guid=t | ||
| Řádek 90: | Řádek 113: | ||
| url += " | url += " | ||
| url += t; | url += t; | ||
| - | Serial.print(" | + | Serial.print(F(" |
| | | ||
| // Make a HTTP GETrequest. | // Make a HTTP GETrequest. | ||
| Řádek 97: | Řádek 120: | ||
| " | " | ||
| + | // Blik 1 time when send OK | ||
| + | digitalWrite(2, | ||
| + | | ||
| // Workaroud for timeout | // Workaroud for timeout | ||
| unsigned long timeout = millis(); | unsigned long timeout = millis(); | ||
| while (client.available() == 0) { | while (client.available() == 0) { | ||
| if (millis() - timeout > 5000) { | if (millis() - timeout > 5000) { | ||
| - | Serial.println(">>> | + | Serial.println(F(">>> |
| client.stop(); | client.stop(); | ||
| + | // Blink 4 times when client timeout | ||
| + | digitalWrite(2, | ||
| + | delay(1000); | ||
| return; | return; | ||
| } | } | ||
| Řádek 110: | Řádek 139: | ||
| | | ||
| // Wait for another round | // Wait for another round | ||
| - | delay(sleep); | + | delay(sleep*60000); |
| } | } | ||
| </ | </ | ||