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í verze | Následující verzeObě strany příští revize | ||
zarizeni:esp8266 [2016/05/30 10:07] – 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 tmep.ino> | + | ===== Teplota ===== |
+ | |||
+ | {{: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | <file c tmep_send_temperature.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:// | + | // but for humidity You will need DHT11 or DHT22 sensor and code will need some modifications |
- | // Include WiFi stuff | + | #include < |
- | #include < | + | #include < |
- | + | #include < | |
- | // Include Temperature stuff for DS18B20 | + | |
- | #include < | + | |
- | #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 oneWireBus = 5; // Pin where is DS18B20 connected |
- | + | ||
- | // Use WiFiClient class to create TCP connections | + | |
- | WiFiClient client; | + | |
// Create Temperature object " | // Create Temperature object " | ||
- | OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/ | + | OneWire oneWire(oneWireBus); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/ |
DallasTemperature sensors(& | DallasTemperature sensors(& | ||
Řádek 45: | Řádek 46: | ||
// 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(); | ||
- | // Initialize the DallasTemperature DS18B20 class (not strictly necessary with the client class, but good practice). | + | |
- | sensors.begin(); | + | delay(10000); |
+ | } | ||
+ | |||
+ | 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(" | ||
+ | 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, | ||
+ | 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. We need: /?guid=t | ||
+ | String url = "/?"; | ||
+ | url += guid; | ||
+ | url += " | ||
+ | url += t; | ||
+ | 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 | ||
+ | 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:// | ||
+ | // 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() { | ||
+ | // Start serial | ||
+ | Serial.begin(115200); | ||
+ | delay(10); | ||
+ | Serial.println(); | ||
+ | |||
+ | // Connect to the WiFi | ||
+ | Serial.print(" | ||
+ | WiFi.begin(ssid, | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | Serial.println(); | ||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(); | ||
- | | + | |
+ | delay(10000); | ||
} | } | ||
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); | ||
} | } | ||
</ | </ |