Outils pour utilisateurs

Outils du site


dev:esp:esp32:exemples:serveur-udp

Serveur UDP

L'ESP se connecte au WiFi et écoute en UDP sur le port 3333. Il attend une commande sous forme de chaîne de caractères on/off et allume en ou éteint en fonction la LED intégrée sur le port 2. Ce croquis s'appuie sur l'exemple WiFiUDPClient

#include <WiFi.h>
#include <WiFiUdp.h>
 
// WiFi network name and password:
const char * networkName = "Mon SSID";
const char * networkPswd = "Ma clé wifi";
char packetBuffer[255];
const char * msg_error = "Commande invalide";
const unsigned char LED = 2;
 
 
 
//IP address to send UDP data to:
// either use the ip address of the server or 
// a network broadcast address
const char * udpAddress = "192.168.1.18";
const unsigned int udpPort = 3333;
 
//Are we currently connected?
boolean connected = false;
 
//The udp library class
WiFiUDP udp;
 
void setup(){
  // Initilize hardware serial:
  Serial.begin(115200);
 
  //Connect to the WiFi network
  connectToWiFi(networkName, networkPswd);
 
  //listen on udpPort
  udp.begin(udpPort);
 
  //
  pinMode(LED,OUTPUT);
  digitalWrite(LED, LOW);
 
}
 
void loop(){
  //only control data when connected
  if(connected){
    int packetSize = udp.parsePacket();
    if(packetSize)
    {
 
      Serial.print("pk size:");
      Serial.println(packetSize, DEC);
 
      udp.read(packetBuffer, packetSize);
      udp.flush();
 
      packetBuffer[packetSize-1] = '\0';
 
      Serial.print("packetBuffer='");
      Serial.print(packetBuffer);
      Serial.println('\'');
 
      String request = packetBuffer;
      //request = request.substring(0,packetSize);
      String cmd_on = String("on");
 
      if(request.equalsIgnoreCase("on"))
      {
        Serial.println("Commande ON");
        digitalWrite(LED, HIGH);
      }
      else if(request.equalsIgnoreCase("off"))
      {
        Serial.println("Commande OFF");
        digitalWrite(LED, LOW);
 
      }
      else
      {
        Serial.println(msg_error);
      }
    }
 
  }
  //Wait for 1 second
  delay(1000);
 
}
 
void connectToWiFi(const char * ssid, const char * pwd){
  Serial.println("Connecting to WiFi network: " + String(ssid));
 
  // delete old config
  WiFi.disconnect(true);
  //register event handler
  WiFi.onEvent(WiFiEvent);
 
  //Initiate connection
  WiFi.begin(ssid, pwd);
 
  Serial.println("Waiting for WIFI connection...");
}
 
//wifi event handler
void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set 
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());  
          //initializes the UDP state
          //This initializes the transfer buffer
          udp.begin(WiFi.localIP(),udpPort);
          connected = true;
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          connected = false;
          break;
    }
}
dev/esp/esp32/exemples/serveur-udp.txt · Dernière modification : 2021/02/01 21:51 de 127.0.0.1