Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
materiel:esp8266:accueil [2017/02/23 22:33] resonance [Serveur Web #2] |
materiel:esp8266:accueil [2019/04/04 09:21] resonance [Avec le logiciel Arduino] |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== ESP8266 ====== | ====== ESP8266 ====== | ||
- | {{: | + | {{: |
- | L’**ESP8266** est un micro-contrôleur pas cher (entre 4 et 10€) avec connexion WiFi intégrée, produit par les fabricants Espressif et AI-Thinker. Depuis 2014, une communauté s' | + | L’**ESP8266** est un micro-contrôleur pas cher (entre 4 et 10€) avec connexion WiFi intégrée, produit par les fabricants Espressif et AI-Thinker. Depuis 2014, une communauté s' |
On peut se servir du module pour : | On peut se servir du module pour : | ||
* **Créer un serveur Web**, pour visualiser les données d'un capteur sur un téléphone mobile ou un ordinateur via le navigateur Web ou bien pour lui envoyer des commandes comme fermer la porte du garage ou arroser les plantes (pour une utilisation un peu classique) | * **Créer un serveur Web**, pour visualiser les données d'un capteur sur un téléphone mobile ou un ordinateur via le navigateur Web ou bien pour lui envoyer des commandes comme fermer la porte du garage ou arroser les plantes (pour une utilisation un peu classique) | ||
+ | * **Communiquer avec un serveur Web**, pour envoyer ou recevoir des données sur le Web, à partir d'un site. Exemple avec [[http:// | ||
* **Créer un réseau maillé**, où chaque module envoie et reçoit des informations des autres. Voir notre projet [[projets: | * **Créer un réseau maillé**, où chaque module envoie et reçoit des informations des autres. Voir notre projet [[projets: | ||
* **Créer des connexions sans fils**, et donc envoyer et recevoir des informations par le WiFi en passant par d' | * **Créer des connexions sans fils**, et donc envoyer et recevoir des informations par le WiFi en passant par d' | ||
+ | |||
+ | <WRAP group> | ||
+ | <WRAP half column> | ||
+ | <WRAP half column> | ||
+ | </ | ||
- | {{youtube> | ||
===== Caractéristiques ===== | ===== Caractéristiques ===== | ||
* Documentation technique : {{: | * Documentation technique : {{: | ||
Ligne 22: | Ligne 27: | ||
L' | L' | ||
<WRAP group> | <WRAP group> | ||
- | <WRAP half column> | + | <WRAP half column> |
- | {{: | + | <WRAP half column> |
- | + | ||
- | <WRAP half column> | + | |
- | {{: | + | |
</ | </ | ||
+ | |||
+ | Pour débuter, nous vous conseiller deux cartes prêtes à être programmée, | ||
+ | |||
+ | <WRAP group> | ||
+ | <WRAP half column>// | ||
+ | {{: | ||
+ | <WRAP half column>// | ||
</ | </ | ||
- | Nous vous conseillons pour débuter de choisir une carte prête à être programmée (// | + | |
- | {{: | + | |
+ | |||
==== Alimentation ==== | ==== Alimentation ==== | ||
Ligne 42: | Ligne 53: | ||
{{: | {{: | ||
+ | |||
+ | |||
+ | **Wemos D1** | ||
+ | {{: | ||
===== Configuration ===== | ===== Configuration ===== | ||
On peut programmer l' | On peut programmer l' | ||
Ligne 53: | Ligne 68: | ||
==== Avec le logiciel Arduino ==== | ==== Avec le logiciel Arduino ==== | ||
+ | Voir si besoin cette page d' | ||
+ | |||
* Installer le logiciel Arduino | * Installer le logiciel Arduino | ||
* Ouvrir le logiciel et les " | * Ouvrir le logiciel et les " | ||
Ligne 63: | Ligne 80: | ||
* Par mesure de sécurité, vous pouvez aussi diminuer la vitesse d' | * Par mesure de sécurité, vous pouvez aussi diminuer la vitesse d' | ||
* exemple de vitesse " | * exemple de vitesse " | ||
+ | * Installer des bibliothèques avec des exemples (Menu Croquis > Inclure une bibliothèque > Gérer les bibliothèques > Taper esp8266) | ||
+ | |||
+ | {{ : | ||
+ | |||
<WRAP center round info 80%> | <WRAP center round info 80%> | ||
Ligne 68: | Ligne 89: | ||
</ | </ | ||
+ | Le microgiciel de l' | ||
+ | |||
+ | <WRAP center round info 80%> | ||
+ | Si la vitesse de transfert est souvent plus rapide que par USB, il peut arriver dans le cas d'une boucle principale ne contenant pas de //delay()// ou //yield()// que l'IDE d' | ||
+ | </ | ||
==== Avec une carte Arduino ==== | ==== Avec une carte Arduino ==== | ||
Vous pouvez aussi utiliser ces modules pour ajouter une connexion Wi-Fi à un projet utilisant déjà une Arduino. | Vous pouvez aussi utiliser ces modules pour ajouter une connexion Wi-Fi à un projet utilisant déjà une Arduino. | ||
Ligne 77: | Ligne 103: | ||
- | ===== Exemples ===== | + | <WRAP center round important 80%> |
+ | Sur d' | ||
+ | </ | ||
- | ==== Faire clignoter la LED ==== | ||
- | Rien d' | ||
- | |||
- | <code cpp> | ||
- | int led = 2; | ||
- | |||
- | void setup() { | ||
- | pinMode(led, | ||
- | } | ||
- | |||
- | void loop() { | ||
- | digitalWrite(led, | ||
- | delay(1000); | ||
- | digitalWrite(led, | ||
- | delay(1000); | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Plusieurs LED ==== | ||
- | * {{: | ||
- | |||
- | Sur la carte Wemos D1, les numéros de sortie sont un peu différents, | ||
- | |||
- | {{: | ||
- | |||
- | <code cpp> | ||
- | const int led1 = 14; //D11 | ||
- | const int led2 = 12; | ||
- | const int led3 = 13; | ||
- | |||
- | //PWM : 10 bits | ||
- | |||
- | int brightness = 0; // how bright the LED is (0 = full, 512 = dim, 1023 = off) | ||
- | int fadeAmount = 5; // how many points to fade the LED by | ||
- | const int delayMillis = 10;// how long to pause between each loop | ||
- | |||
- | void setup() { | ||
- | pinMode(led1, | ||
- | pinMode(led2, | ||
- | pinMode(led3, | ||
- | } | ||
- | |||
- | void loop() { | ||
- | // set the LED brightness | ||
- | analogWrite(led1, | ||
- | analogWrite(led2, | ||
- | analogWrite(led3, | ||
- | |||
- | // increment/ | ||
- | brightness = brightness + fadeAmount; | ||
- | |||
- | // limit to 10-bit (0-1023) | ||
- | if (brightness < 0) brightness = 0; | ||
- | if (brightness > 1023) brightness = 1023; | ||
- | |||
- | // reverse the direction of the fading at each end | ||
- | if (brightness == 0 || brightness == 1023) { | ||
- | fadeAmount = -fadeAmount; | ||
- | } | ||
- | |||
- | // pause so you can see each brightness level | ||
- | delay(delayMillis); | ||
- | } | ||
- | </ | ||
- | ==== Serveur Web ==== | ||
- | Éteindre ou allumer une LED avec le navigateur Web. L' | ||
- | |||
- | {{: | ||
- | |||
- | <code cpp> | ||
- | /* | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | */ | ||
- | |||
- | #include < | ||
- | |||
- | WiFiServer server(80); // Initialize the server on Port 80 | ||
- | |||
- | int LED_PIN = 2; | ||
- | |||
- | void setup() { | ||
- | pinMode(LED_PIN, | ||
- | WiFi.mode(WIFI_AP); | ||
- | WiFi.softAP(" | ||
- | server.begin(); | ||
- | Serial.begin(115200); | ||
- | IPAddress HTTPS_ServerIP= WiFi.softAPIP(); | ||
- | Serial.println(); | ||
- | Serial.print(" | ||
- | Serial.println(HTTPS_ServerIP); | ||
- | } | ||
- | |||
- | void loop() { | ||
- | WiFiClient client = server.available(); | ||
- | | ||
- | if (!client) { | ||
- | return; | ||
- | } | ||
- | | ||
- | Serial.println(" | ||
- | | ||
- | //Read what the browser has sent into a String class and print the request to the monitor | ||
- | //String request = client.readString(); | ||
- | String request = client.readStringUntil(' | ||
- | | ||
- | //Looking under the hood | ||
- | Serial.println(request); | ||
- | | ||
- | // Handle the Request | ||
- | if (request.indexOf("/ | ||
- | else if (request.indexOf("/ | ||
- | | ||
- | client.flush(); | ||
- | client.print(" | ||
- | delay(10); | ||
- | Serial.println(" | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Serveur Web #2 ==== | ||
- | Même chose avec un peu plus de contrôle. | ||
- | |||
- | {{: | ||
- | |||
- | <code cpp> | ||
- | /* | ||
- | * Access Point (AP) Web Server | ||
- | | ||
- | * This example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages to any connected client. | ||
- | | ||
- | * From https:// | ||
- | */ | ||
- | |||
- | #include < | ||
- | |||
- | // Pin Definitions | ||
- | const int LED_PIN = 2; // LED | ||
- | const int ANALOG_PIN = A0; // The only analog pin on the Thing | ||
- | const int DIGITAL_PIN = 12; // Digital pin to be read | ||
- | |||
- | WiFiServer server(80); | ||
- | |||
- | void setup() | ||
- | { | ||
- | // Pins | ||
- | Serial.begin(115200); | ||
- | pinMode(DIGITAL_PIN, | ||
- | pinMode(LED_PIN, | ||
- | digitalWrite(LED_PIN, | ||
- | | ||
- | // WiFi | ||
- | WiFi.mode(WIFI_AP); | ||
- | WiFi.softAP(" | ||
- | server.begin(); | ||
- | } | ||
- | |||
- | void loop() | ||
- | { | ||
- | // Check if a client has connected | ||
- | WiFiClient client = server.available(); | ||
- | if (!client) { | ||
- | return; | ||
- | } | ||
- | |||
- | // Read the first line of the request | ||
- | String req = client.readStringUntil(' | ||
- | Serial.println(req); | ||
- | client.flush(); | ||
- | |||
- | // Match the request | ||
- | int val = -1; // We'll use ' | ||
- | // request type (read/set) and value if set. | ||
- | if (req.indexOf("/ | ||
- | val = 1; // Will write LED low | ||
- | else if (req.indexOf("/ | ||
- | val = 0; // Will write LED high | ||
- | else if (req.indexOf("/ | ||
- | val = -2; // Will print pin reads | ||
- | // Otherwise request will be invalid. We'll say as much in HTML | ||
- | |||
- | // Set GPIO5 according to the request | ||
- | if (val >= 0) | ||
- | digitalWrite(LED_PIN, | ||
- | |||
- | client.flush(); | ||
- | |||
- | // Prepare the response. Start with the common header: | ||
- | String s = " | ||
- | s += " | ||
- | s += "< | ||
- | // If we're setting the LED, print out a message saying we did | ||
- | if (val >= 0) | ||
- | { | ||
- | s += "LED is now "; | ||
- | s += (val)?" | ||
- | } | ||
- | else if (val == -2) | ||
- | { // If we're reading pins, print out those values: | ||
- | s += " | ||
- | s += String(analogRead(ANALOG_PIN)); | ||
- | s += "< | ||
- | s += " | ||
- | s += String(digitalRead(DIGITAL_PIN)); | ||
- | } | ||
- | else | ||
- | { | ||
- | s += " | ||
- | } | ||
- | s += "</ | ||
- | |||
- | // Send the response to the client | ||
- | client.print(s); | ||
- | delay(1); | ||
- | Serial.println(" | ||
- | |||
- | // The client will actually be disconnected | ||
- | // when the function returns and ' | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ==== Send OSC to Pure Data ==== | ||
- | |||
- | <code cpp> | ||
- | /* | ||
- | | ||
- | | ||
- | | ||
- | */ | ||
- | |||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | //#include < | ||
- | |||
- | int status = WL_IDLE_STATUS; | ||
- | char ssid[] = " | ||
- | char pass[] = ""; | ||
- | WiFiUDP udp; | ||
- | |||
- | const IPAddress outIp(192, | ||
- | const unsigned int outPort = 9999; // MASTER PORT | ||
- | |||
- | void setup() { | ||
- | Serial.begin(115200); | ||
- | Serial.println(); | ||
- | Serial.print(" | ||
- | Serial.println(ssid); | ||
- | | ||
- | WiFi.begin(ssid, | ||
- | |||
- | while (WiFi.status() != WL_CONNECTED) { | ||
- | delay(500); | ||
- | Serial.print(" | ||
- | } | ||
- | Serial.println("" | ||
- | printWifiStatus(); | ||
- | } | ||
- | |||
- | void loop() { | ||
- | // SEND OSC | ||
- | OSCMessage msg("/ | ||
- | msg.add(" | ||
- | udp.beginPacket(outIp, | ||
- | msg.send(udp); | ||
- | udp.endPacket(); | ||
- | msg.empty(); | ||
- | delay(500); | ||
- | } | ||
- | |||
- | void printWifiStatus() { | ||
- | // print the SSID of the network you're attached to: | ||
- | Serial.print(" | ||
- | Serial.println(WiFi.SSID()); | ||
- | |||
- | // print your WiFi shield' | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | Serial.println(WiFi.localIP()); | ||
- | |||
- | // print the received signal strength: | ||
- | long rssi = WiFi.RSSI(); | ||
- | Serial.print(" | ||
- | Serial.print(rssi); | ||
- | Serial.println(" | ||
- | |||
- | Serial.println(" | ||
- | udp.begin(localPort); | ||
- | Serial.print(" | ||
- | Serial.println(udp.localPort()); | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Receive OSC from Pure Data ==== | ||
+ | ===== Exemples ===== | ||
+ | Tous les codes des exemples ici : {{: | ||
- | <code cpp> | + | * [[.: |
- | // Pd > bundle and typed message > packOSC > udpsend > routeur Wi-Fi | + | * [[.: |
- | // ESP8266 > OscBundle | + | |
- | + | * [[.: | |
- | #include < | + | |
- | #include <WiFiUdp.h> | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | + | ||
- | int status = WL_IDLE_STATUS; | + | |
- | char ssid[] = " | + | |
- | char pass[] = ""; | + | |
- | + | ||
- | // A UDP instance to let us send and receive packets over UDP | + | |
- | WiFiUDP Udp; | + | |
- | + | ||
- | const unsigned int localPort = 8888; | + | |
- | + | ||
- | OSCErrorCode error; | + | |
- | unsigned int ledState = LOW; // LOW means led is *on* | + | |
- | + | ||
- | void setup() { | + | |
- | | + | |
- | digitalWrite(BUILTIN_LED, | + | |
- | WiFi.begin(ssid, pass); // Connect to WiFi network | + | |
- | while (WiFi.status() != WL_CONNECTED) { | + | |
- | delay(500); | + | |
- | } | + | |
- | Udp.begin(localPort); | + | |
- | } | + | |
- | + | ||
- | void ledOut(OSCMessage &msg) { | + | |
- | ledState = msg.getInt(0); | + | |
- | digitalWrite(BUILTIN_LED, | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | OSCBundle bundle; | + | |
- | int _size = Udp.parsePacket(); | + | |
- | if (_size > 0) { | + | |
- | while (_size--) { | + | |
- | bundle.fill(Udp.read()); | + | |
- | } | + | |
- | if (!bundle.hasError()) { | + | |
- | | + | |
- | } else { | + | |
- | error = bundle.getError(); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + |