Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By CheapB
#16353
bernd331 wrote:Hi,

I try to extend the example. If a Client send something to the ESP8266, the ESP8266 should answer with something. See the code... The ESP8266 answers onyl one time. Then the ESP8266 stop answering to the Client.

I'm sure, I miss something. But what? Any idea?

Thanks and greets, Bernd

Code: Select all/*
 * 31 mar 2015
 * This sketch display UDP packets coming from an UDP client.
 * On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
 *
 * Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
 *
 */

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

extern "C" {  //required for read Vdd Voltage
#include "user_interface.h"
  // uint16 readvdd33(void);
}

int status = WL_IDLE_STATUS;
const char* ssid = "UPC0864334";  //  your network SSID (name)
const char* pass = "geheim990";       // your network password

unsigned int localPort = 12345;      // local port to listen for UDP packets

byte packetBuffer[512]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // setting up Station AP
  WiFi.begin(ssid, pass);
 
  // Wait for connect to AP
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries=0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30){
      break;
    }
  }
  Serial.println();


printWifiStatus();

  Serial.println("Connected to wifi");
  Serial.print("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);
}

void loop()
{
  int noBytes = Udp.parsePacket();
  String received_command = "";
 
  if ( noBytes ) {
    Serial.print(millis() / 1000);
    Serial.print(":Packet of ");
    Serial.print(noBytes);
    Serial.print(" received from ");
    Serial.print(Udp.remoteIP());
    Serial.print(":");
    Serial.println(Udp.remotePort());
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,noBytes); // read the packet into the buffer

    // display the packet contents in HEX
    for (int i=1;i<=noBytes;i++)
    {
      Serial.print(packetBuffer[i-1],HEX);
      received_command = received_command + char(packetBuffer[i - 1]);
      if (i % 32 == 0)
      {
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();
   
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("Answer from ESP8266 ChipID#");
    Udp.print(system_get_chip_id());
    Udp.write("#IP of ESP8266#");
    Udp.println(WiFi.localIP());
    Udp.endPacket();
   
    Serial.println(received_command);
    Serial.println();
  } // end if


}

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:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}


I had similar problems when using the preconfigured ZIP environment. After installing Arduino separately and this repository https://github.com/sandeepmistry/esp8266-Arduino it worked better
User avatar
By bernd331
#16370 Hi,

thanks for the answers. But finaly I compiled my "Arduino for ESP8266" by myself. It was suprisingly easy with this help:
viewtopic.php?f=26&t=2397
As Byozayturay in his post said: Takes a Long time. Round about an hour. But now all works fine under Windows 10 insider preview 10074 64Bit.

Maybe this code helps others. It's a UDP script which sends back "answer" to the Sender.

Also, when the ESP8266 starts, it broadcasts it's Chip-ID and IP-Address.

Code: Select all/*
 * 31 mar 2015
 * This sketch display UDP packets coming from an UDP client.
 * On a Mac the NC command can be used to send UDP. (nc -u 192.168.1.101 2390).
 *
 * Configuration : Enter the ssid and password of your Wifi AP. Enter the port number your server is listening on.
 *
 */

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

extern "C"
{
  #include "user_interface.h" 
}

int status = WL_IDLE_STATUS;
const char* ssid = "UPC0864334";  //  your network SSID (name)
const char* pass = "secret";   // your network password

unsigned int localPort = 2390;    // local port to listen for UDP packets

IPAddress ipMulti (192,168,0,255);

byte packetBuffer[512]; //buffer to hold incoming and outgoing packets

WiFiUDP Udp;  // A UDP instance to let us send and receive packets over UDP

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // setting up Station AP
  WiFi.begin(ssid, pass);
 
  // Wait for connect to AP
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries=0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30){
      break;
    }
  }
  Serial.println();

  printWifiStatus();

  Serial.println("Connected to wifi");
  Serial.print("Udp server started at port ");
  Serial.println(localPort);
  Udp.begin(localPort);
  multicas_at_start_esp8266();
}

void loop()
{
  int noBytes = Udp.parsePacket();
  if ( noBytes ) {
    Serial.print(millis() / 1000);
    Serial.print(":Packet of ");
    Serial.print(noBytes);
    Serial.print(" received from ");
    Serial.print(Udp.remoteIP());
    Serial.print(":");
    Serial.println(Udp.remotePort());
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,noBytes); // read the packet into the buffer

    // display the packet contents in HEX
    for (int i=1;i<=noBytes;i++){
      Serial.print(packetBuffer[i-1],HEX);
      if (i % 32 == 0){
        Serial.println();
      }
      else Serial.print(' ');
    } // end for
    Serial.println();
//    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Send answer to Packet-Sender
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Send Answer to Network (Broadcast)   
    Udp.write("answer");
    Udp.endPacket();
  } // end if
}

void multicas_at_start_esp8266() //When ESP8266 starts, Broadcast presence of ESP8255
{
  Udp.beginPacket(ipMulti, 2390);   
  Udp.write("ESP8266 ID#");
  Udp.print(system_get_chip_id());
  Udp.write("#ESP8266 IP#");
  Udp.println(WiFi.localIP());
  //Udp.writeln("answer");
  Udp.endPacket();
 
}

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:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}


To send a UDP broadcast, I simply send the UDP-Packet to the IP 192.168.0.255. My subnetmask is 255.255.255.0.

To test, I use "Hercules Setup Utility by HW-Group". Nice tool for TCP, UDP and Serial under Windows.

Greetz, Bernd
User avatar
By gerardwr
#16377 @bernd331

Your sketch runs OK with Arduino 1.6.3 + esp8266 addition from https://github.com/sandeepmistry/esp8266-Arduino.

The sketch receives the UDP packet and the answer packet is received by the client ( I used the Packet Sender application on Mac).

Nice idea to send a multicast at startup. Did not check that the multicast packet at startup is received. How did you check this?

Remark : You chose 192.168.0.255 as the multicast address. Acc. to http://www.tldp.org/HOWTO/Multicast-HOWTO-2.html multicast addresses are in the range 224.0.0.0 - 239.255.255.255

Thanks for sharing.
User avatar
By gr0b
#16415 So what was the fix for UDP only sending the first response ?
I have the same issue using the latest zip package and wanting to know to fix it if I just need to recompile like suggested or if can just update a library.