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í verzeNásledující verzeObě strany příští revize | ||
zarizeni:esp8266 [2016/05/30 10:01] – vytvořeno multitricker | zarizeni:esp8266 [2016/06/06 10:50] – 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 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 | + | <file c tmep_send_temperature.ino> |
- | #define GUID " | + | // 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 | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Define settings | ||
+ | const char* ssid = "---wifi ssid---"; // WiFi SSID | ||
+ | const char* pass = "---wifi pass---"; // WiFi password | ||
+ | const char* domain | ||
+ | const char* guid = "---guid---"; | ||
+ | const long sleep = 600000; | ||
+ | const byte oneWireBus = 5; // Pin where is DS18B20 connected | ||
+ | |||
+ | // Create Temperature object | ||
+ | OneWire oneWire(oneWireBus); | ||
+ | DallasTemperature sensors(& | ||
- | // 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(" | + | 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() { | ||
- | | + | |
+ | float t = sensors.getTempCByIndex(0); | ||
+ | if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :) | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
| | ||
- | if (client.connect(SERVER, 80)) { | + | |
- | Serial.println(" | + | WiFiClient client; // Use WiFiClient class to create TCP connections |
+ | |||
+ | char host[50]; | ||
+ | strcpy(host, | ||
+ | strcat(host, | ||
+ | const int httpPort = 80; | ||
+ | |||
+ | Serial.print(" | ||
+ | | ||
+ | | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
+ | | ||
- | int rndm = random(10, 30); | + | // Make an url. We need: /?guid=t |
- | + | String url = "/?"; | |
- | // Make a HTTP request. | + | url += guid; |
- | client.print(" | + | url += " |
- | client.print(String(GUID)); // guid | + | url += t; |
- | | + | Serial.print(" |
- | client.print(rndm); // value | + | |
- | | + | // Make a HTTP GETrequest. |
- | | + | client.print(String(" |
- | | + | " |
- | | + | " |
- | + | ||
- | // Just for debugging. GET /?mojemereni=20 | + | // Workaroud for timeout |
- | Serial.print(SERVER); Serial.print(" "); | + | unsigned long timeout = millis(); |
- | Serial.print(" | + | while (client.available() == 0) { |
- | | + | if (millis() - timeout > 5000) { |
- | Serial.print(" | + | |
- | Serial.print(rndm); | + | client.stop(); |
- | Serial.println(" | + | return; |
- | Serial.print(" | + | |
- | Serial.println(" | + | } |
- | Serial.println(); | + | |
- | + | Serial.println(); | |
- | } else { | + | |
- | // if you didn't get a connection to the server: | + | |
+ | delay(sleep); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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://wiki.tmep.cz/ | ||
+ | // | ||
+ | // 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 = "---wifi 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() { | ||
+ | // Start serial | ||
+ | | ||
+ | delay(10); | ||
+ | | ||
+ | |||
+ | // Connect to the WiFi | ||
+ | | ||
+ | WiFi.begin(ssid, pass); | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | | ||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(); | ||
+ | |||
+ | dht.begin(); | ||
+ | delay(10000); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // 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) | ||
+ | float t = dht.readTemperature(); | ||
+ | |||
+ | // Check if any reads failed and exit early (to try again). | ||
+ | if (isnan(h) || isnan(t)) { | ||
+ | Serial.println(" | ||
+ | | ||
+ | } | ||
+ | |||
+ | // 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, | ||
+ | const int httpPort = 80; | ||
+ | |||
+ | Serial.print(" | ||
+ | if (!client.connect(host, | ||
+ | // 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 | ||
+ | | ||
} | } | ||
</ | </ |