Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


Panneau latéral

materiel:esp8266:communications:accueil

ESP8266 : communications

L'ESP8266 et votre ordinateur se connectent au réseau d'un routeur Wi-Fi “linksys”, puis s'échangent des données.

Installer la bibliothèque OSC (Menu Croquis > Inclure une bibliothèque > Gérer les bibliothèques > Taper OSC).

Adresse IP statique

La plupart des exemples fonctionnent en adressant automatiquement le module Wi-Fi. Il est possible de configurer aussi une adresse statique, c'est-à-dire que la connaît avec la fonction Wifi.config (ip1, ip2, ip3). Veillez à ne pas utiliser deux fois la même adresse. Pour cela, on peut choisir des adresses au-delà des 10 premières.

// Setup
char ssid[] = "linksys"; // Name of the Wi-Fi network
char pass[] = ""; // No password
IPAddress ip(192, 168, 0, 110);  // Local IP   
IPAddress gateway(192, 168, 0, 1); // Router IP
IPAddress subnet(255, 255, 255, 0);
//...
 
void setup() {
  WiFi.config(ip, gateway, subnet); // Choose the static IP Address
  WiFi.begin(ssid, pass);   // WiFi Connection
  //...
}
 
//...

Send OSC to Pure Data

/* 
 *  Send OSC messages to PD
 *  
 *  Examples : OSC/ESP8266SendMessages
 */
 
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
//#include <Streaming.h>
 
int status = WL_IDLE_STATUS;
char ssid[] = "linksys";
char pass[] = "";
WiFiUDP udp;
 
const IPAddress outIp(192,168,1,110); // MASTER IP 
const unsigned int outPort = 9999;    // MASTER PORT
 
void setup() {
  Serial.begin(115200);
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  printWifiStatus();
}
 
void loop() {
  // SEND OSC
  OSCMessage msg("/test");
  msg.add("Hello from ESP8266");
  udp.beginPacket(outIp, outPort);
  msg.send(udp);
  udp.endPacket();
  msg.empty();
  delay(500);
}
 
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print your WiFi shield's IP address:
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
 
  Serial.println("Starting UDP");
  udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(udp.localPort());
}

Receive OSC from Pure Data

Pure Data controls the ESP8266.

// Pd > bundle and typed message > packOSC > udpsend > routeur Wi-Fi
// ESP8266 > OscBundle 
 
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
 
int status = WL_IDLE_STATUS;
char ssid[] = "linksys";          // your network SSID (name)
char pass[] = "";                 // your network password
 
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
 
const unsigned int localPort = 8888;        // local port to listen for UDP packets (here's where we send the packets)
 
OSCErrorCode error;
unsigned int ledState = LOW;              // LOW means led is *on*
 
void setup() {
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, ledState);    // turn *on* 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, ledState);
}
 
void loop() {
  OSCBundle bundle;
  int _size = Udp.parsePacket();
  if (_size > 0) {
    while (_size--) {
      bundle.fill(Udp.read());
    }
    if (!bundle.hasError()) {
       bundle.dispatch("/led", ledOut);
    } else {
      error = bundle.getError();
    }
  }
}

Receive UDP Packets from Pure Data

/* 
 *  Receive UDP Packets from Pure Data with [netsend]
 *  
 *  Connect the ESP8266 to the "linksys" Wi-Fi router
 * 
 */
 
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
 
const boolean DEBUG = false;
 
// WIFI
char ssid[] = "linksys";
char pass[] = "";
const unsigned int localPort = 8888;  // port
char packetBuffer[255]; // incoming
String data[3]; // data
int status = WL_IDLE_STATUS;
WiFiUDP udp;
 
void setup() {
 
  // Output
  pinMode(14,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  digitalWrite(14,HIGH);
  digitalWrite(12,HIGH);
  digitalWrite(13,HIGH);
 
  // WiFi Connection
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) 
  {
   delay(500);
  }
 
  // UDP Connection
  udp.begin(localPort);
 
  if (DEBUG) 
  {
    Serial.begin(115200);
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
  }
}
 
void loop() {
 
  // Read data if available
  int packetSize = udp.parsePacket();
  if (packetSize) 
  {
    // Read the packet into packetBuffer
    int len = udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
 
    // Parse with space delimiter ' ' and fill data[]
    String strData(packetBuffer);
    splitString(strData, ' ');
 
    analogWrite(data[0].toInt(), data[1].toInt() * 4);
 
    if (DEBUG) 
    {
      Serial.println(data[0]);
      Serial.println(data[1]);
     // Serial.println(data[2]);
    }
  }
}
 
 
// Méthode pour découper le message avec un séparateur (ou "parser")
void splitString(String message, char separator) {
  int index = 0;
  int cnt = 0;
    do {
      index = message.indexOf(separator); 
      // s'il y a bien un caractère séparateur
      if(index != -1) { 
          // on découpe la chaine et on stocke le bout dans le tableau
          data[cnt] = message.substring(0,index); 
          cnt++;
          // on enlève du message le bout stocké
          message = message.substring(index+1, message.length());
      } else {
         // après le dernier espace   
         // on s'assure que la chaine n'est pas vide
         if(message.length() > 0) { 
           data[cnt] = message.substring(0,index); // dernier bout
           cnt++;
         }
      }
   } while(index >=0); // tant qu'il y a bien un séparateur dans la chaine
}
/home/resonancg/www/wiki/data/pages/materiel/esp8266/communications/accueil.txt · Dernière modification: 2018/01/25 21:38 de resonance