From t-o-f

Arduino: ESP8266 : UDP

Contents (hide)

  1. 1. Exemple de code pour se connecter à un réseau WIFI
  2. 2. Exemple pour se connecter à un réseau WIFI et ouvrir une connexion UDP
    1. 2.1 Code Arduino
    2. 2.2 Code Max
  3. 3. Code Processing

1.  Exemple de code pour se connecter à un réseau WIFI

// INCLUDE ESP8266WiFi:
#include <ESP8266WiFi.h>

// Please change the following values with your network settings:
const char* ssid     = "PetitPet";
const char* password = "freedomostie";

IPAddress ip(192, 168, 25, 10);
IPAddress gateway(192, 168, 25, 1);
IPAddress subnet(255, 255, 255, 0);


//===========
//== SETUP ==
//===========
void setup() {

        // INITIATE SERIAL COMMUNICATION FOR DEBUGGING.
        Serial.begin(57600);

        Serial.println("***STARTING WIFI***");

        // BEGIN WIFI
        WiFi.config(ip , gateway , subnet );
        WiFi.begin(ssid, password);

        // WAIT UNTIL CONNECTED
        while (WiFi.status() != WL_CONNECTED) {
                Serial.print(".");
                delay(10);
        }

        // PRINT CONNECTION SETTINGS
        Serial.println();
        Serial.println("WiFi connected, IP address: ");
        Serial.println( WiFi.localIP() );


}

void loop() {


}

2.  Exemple pour se connecter à un réseau WIFI et ouvrir une connexion UDP

2.1  Code Arduino


// INCLUDE ESP8266WiFi:
#include <ESP8266WiFi.h>

// Please change the following values with your network settings:
const char* ssid     = "NOM DU RESEAU";
const char* password = "MOT DE PASSE";

IPAddress ip(192, 168, 25, 10);
IPAddress gateway(192, 168, 25, 1);
IPAddress subnet(255, 255, 255, 0);



// INCLUDE ESP8266 UDP
#include <WiFiUdp.h>
WiFiUDP udp;

// UDP
int udpReceivePort = 7777;
IPAddress udpTxIp = IPAddress(192, 168, 25, 125);
int udpTxPort = 7890;

// UDP BUFFERS
#define UDP_RX_BUFFER_MAX_SIZE 256
char udpRxBuffer[UDP_RX_BUFFER_MAX_SIZE];

#define UDP_TX_BUFFER_MAX_SIZE 256
char udpTxBuffer[UDP_TX_BUFFER_MAX_SIZE];

//===========
//== SETUP ==
//===========
void setup() {

        // INITIATE SERIAL COMMUNICATION FOR DEBUGGING.
        Serial.begin(57600);

        Serial.println("***STARTING WIFI***");

        // BEGIN WIFI
        WiFi.config(ip , gateway , subnet );
        WiFi.begin(ssid, password);

        // WAIT UNTIL CONNECTED
        while (WiFi.status() != WL_CONNECTED) {
                Serial.print(".");
                delay(10);
        }
        //

        // PRINT CONNECTION SETTINGS
        Serial.println();
        Serial.println("WiFi connected, IP address: ");
        Serial.println( WiFi.localIP() );


        udp.begin(udpReceivePort); // BEGIN LISTENING ON UDP PORT udpReceivePort

}


//==========
//== LOOP ==
//==========
void loop() {


        // CHECK IF AN UDP PACKET WAS RECEIVED:
        // udp.parsePacket() RETURNS ture IF IT AN UDP PACKET WAS RECEIVED
        if ( udp.parsePacket() ) {

                // COPY THE PACKET INTO A BUFFER
                // udp.read() RETURNS THE NUMBER OF chars THAT WERE RECEIVED
                int packetSize = udp.read(udpRxBuffer, UDP_RX_BUFFER_MAX_SIZE);

                Serial.println("***UDP***");
                Serial.print("Received packet of size: ");
                Serial.println(packetSize);
                Serial.print("From: ");
                IPAddress remoteIp = udp.remoteIP();
                Serial.println(remoteIp);

                // DO SOMETING WITH THE DATA IN THE RX BUFFER
                // FOR EXAMPLE, PRINT IT OUT
                Serial.print("CONTENTS: ");
                for ( int i=0; i < packetSize; i++ ) {
                        Serial.print((int) udpRxBuffer[i]);
                        Serial.print(" ");
                }
                Serial.println();

                // SEND SOME DATA BACK
                sendUdpData();

        }
}

//===================
//== SEND UPD DATA ==
//===================
void sendUdpData() {



        // PUT 3 RANDOM BYTES IN THE TX BUFFER
        udpTxBuffer[0] = 12;
        udpTxBuffer[1] = 255;
        udpTxBuffer[2] = 255;

        udp.beginPacket( udpTxIp , udpTxPort );
        udp.write( udpTxBuffer ,  3 );
        udp.endPacket();

}

2.2  Code Max

<pre><code>
----------begin_max5_patcher----------
526.3ocsU0zabBCD8Lqz9evxm2xh8xm4Vk5wdp4XUUkWvM3HvFYaBaZT+uW+
Azfxx1rAkfPX4wimwu277vSa2D.OJNQUPvMfuCBBdxXIvYyZIXxP.rkbprgn
bNBKEssTtFtabQM8j1svWDC7FAoBnqofaIUjVvWYGkD4ifZpjZboVq6tY+9g
ggPkc8v598lWtnhtG8u.xpbgSb79Ogd1JuukwanZ2g.MYsinKqY769ojVp83
HIMLZGHtv8EcvNfiBi.+XVjD85oPE4r9msarilgcWMQvoCly347fGZ8UceiV
RYOPkfr7hnEg2aGcoN.kmY+hhSbnCeYzgmL+KAWqX+l5huYCS189peri5iOr
goL0V.jYJwOGU614jVmSvOKYjF36Nu0IYyjUyno70RSHbtc3.90noOPQvsTd
kQBfJvgnz7PbRHJBjYdVBoYqPtG6.WpWJfxdMnhtXk2WwWMSzRUJxczyohEu
YmtLRw+GjhvdUeg6pMtHLYsH8CAl4w.7A.ZwK5wucz5qqifMIY8k0yAqOFla
57y586NH1EdAEnD8xxoHN15BL6vTQUZFmnYB9Lmx8NcYt9pSW70jtr2szkt9
zMRtjtNSme03N7YxHjtWHsyy24my39491wPI8A1zV7ETHQZDFZipnW5a9dJc
TLAaM+1Tx6YNuMVrX0lZmV01rV0Q7nxoo2tw3veAtkSx3A
-----------end_max5_patcher-----------
</code></pre>

3.  Code Processing


// PRESS A KEY TO SEND AN UDP PACKET
// =================================

// Install the UDP library from the library manager
// import UDP library
import hypermedia.net.*;

UDP udp;  // define the UDP object

void setup() {

        size(200,200);

        // create a new datagram connection on port 6000
        // and wait for incomming message
        udp = new UDP( this, 7890 );
        //udp.log( true );     // <-- printout the connection activity
        udp.listen( true );
}


void draw() {
        background(0);
}

void keyPressed() {

        byte[] data = new byte[3];

        data[0] = (byte)key;
        data[1] = (byte)random(256);
        data[2] = (byte)random(256);

        print("Sending: ");
        for ( int i =0 ; i < data.length; i++ ) {
                print( (data[i] & 0xFF) +" "); // The & 0xFF converts the signed byte to unsigned byte
        }
        println();

        udp.send( data, "192.168.25.10" , 7777 );

}

/**
* To perform any action on datagram reception, you need to implement this
* handler in your code. This method will be automatically called by the UDP
* object each time he receive a nonnull message.
*/
void receive( byte[] data, String ip, int port ) {


        // print the result
        print( "received from "+ip+" on port "+port +":" );
        for ( int i =0 ; i < data.length; i++ ) {
                print( (data[i] & 0xFF) +" "); // The & 0xFF converts the signed byte to unsigned byte
        }
        println();
}
Récupéré sur http://wiki.t-o-f.info/Arduino/ESP8266UDP
Page mise à jour le 07 November 2018 à 10h26