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í verzePoslední revizeObě strany příští revize | ||
zarizeni:esp8266 [2016/05/30 10:07] – multitricker | zarizeni:esp8266 [2016/10/07 15:52] – 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ů:** http:// |
- | **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 tmep.ino> | + | ===== Teplota ===== |
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | <file c tmep_send_temperature.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, | ||
+ | |||
+ | 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 < | ||
+ | #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 | // Simple sketch for sending data to the TMEP.cz | ||
// by mikrom (http:// | // by mikrom (http:// | ||
// http:// | // http:// | ||
+ | // | ||
// If you send only temperature url will be: http:// | // If you send only temperature url will be: http:// | ||
// If you send also humidity url will be: http:// | // If you send also humidity url will be: http:// | ||
- | // Another nice example with bulding HTTP GET request is here: https://github.com/SensorsIot/ESP8266-Longterm-Sensor-Hourly/ | + | // 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 < |
- | #include < | + | # |
- | + | ||
- | // Include Temperature stuff for DS18B20 | + | |
- | # | + | |
- | #include < | + | |
// Define settings | // Define settings | ||
- | #define WLAN_SSID | + | const char* ssid = " |
- | #define WLAN_PASS | + | const char* pass = " |
- | #define HOST " | + | const char* domain |
- | #define GUID " | + | const char* guid = " |
- | #define DELAY 60000 | + | const long sleep = 600000; |
- | #define ONE_WIRE_BUS | + | const byte dhtPin |
+ | const byte dhtType | ||
- | // Use WiFiClient class to create TCP connections | + | DHT dht(dhtPin, dhtType); // Initialize DHT sensor. |
- | WiFiClient client; | + | |
- | + | ||
- | // Create Temperature object " | + | |
- | OneWire oneWire(ONE_WIRE_BUS); | + | |
- | DallasTemperature sensors(& | + | |
void setup() { | void setup() { | ||
Řádek 45: | Řádek 184: | ||
// Connect to the 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(); | ||
- | | + | |
- | sensors.begin(); | + | |
- | + | ||
- | | + | |
} | } | ||
void loop() { | void loop() { | ||
- | // Send the command | + | // Reading temperature or humidity takes about 250 milliseconds! |
- | | + | // Sensor readings may also be up to 2 seconds ' |
+ | float h = dht.readHumidity(); | ||
+ | // Read temperature as Celsius (the default) | ||
+ | | ||
- | // Read temperature in " | + | // Check if any reads failed and exit early (to try again). |
- | | + | |
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
| | ||
- | // Connect to the HOST and send data via GET method | + | // Connect to the HOST and send data via GET method |
- | | + | |
- | Serial.println(" | + | |
- | + | char host[50]; // Joining two chars is little bit difficult. Make new array, 50 bytes long | |
- | | + | |
- | | + | strcat(host, ".tmep.cz"); // Add ".tmep.cz" |
- | | + | const int httpPort = 80; |
- | client.print(" | + | |
- | | + | Serial.print(" |
- | | + | |
- | | + | // If you didn't get a connection to the server |
- | client.println(" | + | |
- | | + | |
- | | + | |
- | | + | |
- | } else { | + | |
- | // If you didn't get a connection to the server: | + | |
Serial.println(" | Serial.println(" | ||
+ | return; | ||
} | } | ||
+ | Serial.println(" | ||
- | // Wait for DELAY, default 1 minute | + | |
- | delay(DELAY); | + | 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(); | ||
+ | |||
+ | | ||
+ | delay(sleep); | ||
} | } | ||
</ | </ |