ruzne: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í verze | |||
| ruzne:esp8266 [2016/06/06 10:55] – odstraněno multitricker | ruzne:esp8266 [2016/06/06 10:56] (aktuální) – vytvořeno multitricker | ||
|---|---|---|---|
| Řádek 1: | Řádek 1: | ||
| + | ====== ESP8266 a zobrazení poslední teploty z tmep.cz ====== | ||
| + | Příklad jak naprogramovat ESP8266 v Arduino IDE od '' | ||
| + | |||
| + | **Web autora příkladu: | ||
| + | **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 a správnou adresu k JSONu na tmep.cz). | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <file c tmep_read_json_to_oled.ino> | ||
| + | // Simple sketch for reading data from the TMEP.cz (and write them on Serial and OLED display) | ||
| + | // by mikrom (http:// | ||
| + | // http:// | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #define OLED_RESET BUILTIN_LED | ||
| + | |||
| + | // Define settings | ||
| + | const char* ssid = " | ||
| + | const char* pass = " | ||
| + | const char* jsonurl = " | ||
| + | const long sleep = 600000; | ||
| + | |||
| + | Adafruit_SSD1306 display(OLED_RESET); | ||
| + | |||
| + | void setup() { | ||
| + | // Start serial | ||
| + | Serial.begin(115200); | ||
| + | delay(10); | ||
| + | Serial.println(); | ||
| + | |||
| + | // OLED display | ||
| + | display.begin(SSD1306_SWITCHCAPVCC, | ||
| + | display.clearDisplay(); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setTextSize(1); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.println(); | ||
| + | | ||
| + | // Connect to the WiFi | ||
| + | Serial.print(" | ||
| + | display.print(" | ||
| + | WiFi.begin(ssid, | ||
| + | while (WiFi.status() != WL_CONNECTED) { | ||
| + | delay(500); | ||
| + | Serial.print(" | ||
| + | display.print(" | ||
| + | display.display(); | ||
| + | } | ||
| + | Serial.println(); | ||
| + | display.println(); | ||
| + | Serial.println(" | ||
| + | display.println(" | ||
| + | Serial.print(" | ||
| + | display.println(" | ||
| + | Serial.println(); | ||
| + | | ||
| + | display.display(); | ||
| + | delay(10000); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // OLED display | ||
| + | display.clearDisplay(); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.drawRoundRect(1, | ||
| + | |||
| + | // Connect to the HOST and read data via GET method | ||
| + | WiFiClient client; // Use WiFiClient class to create TCP connections | ||
| + | | ||
| + | const char* host = " | ||
| + | const int httpPort = 80; | ||
| + | | ||
| + | Serial.print(" | ||
| + | if (!client.connect(host, | ||
| + | // If you didn't get a connection to the server | ||
| + | Serial.println(" | ||
| + | return; | ||
| + | } | ||
| + | Serial.println(" | ||
| + | |||
| + | // Make an url | ||
| + | char url[50]; | ||
| + | strncpy(url, | ||
| + | | ||
| + | Serial.print(" | ||
| + | |||
| + | // Make HTTP GET request | ||
| + | client.print(String(" | ||
| + | " | ||
| + | " | ||
| + | |||
| + | // Workaroud for timeout | ||
| + | unsigned long timeout = millis(); | ||
| + | while (client.available() == 0) { | ||
| + | if (millis() - timeout > 5000) { | ||
| + | Serial.println(">>> | ||
| + | client.stop(); | ||
| + | return; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Read JSON | ||
| + | String data; | ||
| + | bool capture = false; | ||
| + | String json; | ||
| + | while (client.available()) { | ||
| + | data = client.readStringUntil(' | ||
| + | // | ||
| + | // First few rows is header, it ends with empty line. | ||
| + | // This is unique ultrasimple, | ||
| + | // Start capture when { occurs and end capturing with }. | ||
| + | if(data.startsWith(" | ||
| + | capture = true; | ||
| + | } | ||
| + | if(data.startsWith(" | ||
| + | capture = false; | ||
| + | } | ||
| + | if(capture){ | ||
| + | json = json + data; // Joining row by row together in one nice JSON part. | ||
| + | } | ||
| + | } | ||
| + | json = json + " | ||
| + | Serial.println(json); | ||
| + | |||
| + | // Lets throw our json on ArduinoJson library and get results! | ||
| + | StaticJsonBuffer< | ||
| + | JsonObject& | ||
| + | if (!root.success()) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | // This is how our JSON looks. You can read any value of parameters | ||
| + | // { | ||
| + | // " | ||
| + | // " | ||
| + | // " | ||
| + | // " | ||
| + | // } | ||
| + | String cas = root[" | ||
| + | Serial.print(" | ||
| + | display.setTextSize(1); | ||
| + | display.setCursor(30, | ||
| + | display.print(" | ||
| + | display.setCursor(6, | ||
| + | display.print(cas); | ||
| + | | ||
| + | float teplota = root[" | ||
| + | display.setTextSize(4); | ||
| + | display.setCursor(6, | ||
| + | if (teplota == -127.00) { // If you have connected it wrong, Dallas read this temperature! :) | ||
| + | Serial.println(" | ||
| + | display.print(" | ||
| + | } else { | ||
| + | Serial.print(" | ||
| + | display.print(teplota, | ||
| + | display.setTextSize(3); | ||
| + | display.setCursor(105, | ||
| + | display.print(char(247)); | ||
| + | } | ||
| + | | ||
| + | //String vlhkost = root[" | ||
| + | //String umisteni = root[" | ||
| + | |||
| + | display.display(); | ||
| + | Serial.println(); | ||
| + | | ||
| + | // Wait for another round | ||
| + | delay(sleep); | ||
| + | } | ||
| + | </ | ||