Chat freely about anything...

User avatar
By RichardS
#44984 Below you can find code I found on the web and it will not work.

It connects and all looks good, then I open putty and try to connect to port 8888 and IP that the serial window returns, and I get a connection refused.

Yes my PC and the ESP8266 are on the same router, I am 192.168.1.20 and the esp is 192.168.1.22

RichardS

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

// wifi connection variables
const char* ssid = "***";
const char* password = "***";
boolean wifiConnected = false;

// UDP variables
unsigned int localPort = 8888;
WiFiUDP UDP;
boolean udpConnected = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back

void setup() {
  delay(2000);
 
  // Initialise Serial connection
  Serial.begin(9600);

  // Initialise wifi connection
  wifiConnected = connectWifi();

  // only proceed if wifi connection successful
  if (wifiConnected) {
    udpConnected = connectUDP();
    if (udpConnected) {
      // initialise pins
    }
  }
}

void loop() {
  // check if the WiFi and UDP connections were successful
  if (wifiConnected) {
    if (udpConnected) {

      // if there’s data available, read a packet
      int packetSize = UDP.parsePacket();
      if (packetSize)
      {
        Serial.println("");
        Serial.print("Received packet of size ");
        Serial.println(packetSize);
        Serial.print("From ");
        IPAddress remote = UDP.remoteIP();
        for (int i = 0; i < 4; i++)
        {
          Serial.print(remote[i], DEC);
          if (i < 3)
          {
            Serial.print(".");
          }
        }
        Serial.print(", port ");
        Serial.println(UDP.remotePort());

        // read the packet into packetBufffer
        UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
        Serial.println("Contents: ");
        int value = packetBuffer[0] * 256 + packetBuffer[1];
        Serial.println(value);

        // send a reply, to the IP address and port that sent us the packet we received
        UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
        UDP.write(ReplyBuffer);
        UDP.endPacket();

        // turn LED on or off depending on value recieved
      }
      delay(10);
    }
  }
}

// connect to UDP – returns true if successful or false if not
boolean connectUDP() {
  boolean state = false;

  Serial.println("");
  Serial.println("Connecting to UDP");

  if (UDP.begin(localPort) == 1) {
    Serial.println("Connection successful");
    state = true;
  }
  else {
    Serial.println("Connection failed");
  }

  return state;
}
 
 
// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
  boolean state = true;
  int i = 0;

  WiFi.begin(ssid, password);

  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 10) {
      state = false;
      break;
    }
    i++;
  }
  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("");
    Serial.println("Connection failed.");
  }
  return state;
}
User avatar
By RichardS
#44986 Arduino 1.6.4 and board manager 2.1.0

RichardS
User avatar
By bbx10node
#44998 I have not used putty in years but the last time I looked it does TCP but not UDP. I suggest looking for a netcat (also known as ncat and nc) for Windows. netcat supports TCP and UDP.

If using Linux or similar the netcat command to connect to a UDP server/listener is:

Code: Select all$ nc -u  <ESP IP ADDR> 8888
User avatar
By RichardS
#45003 Guess I did not get enough sleep last night, found a terminal prog that supports UDP, it works!

In one direction to the ESP8266, when the ESP send data back it gets the PORT wrong, if I manually enter the PORT # to the correct # it works, why is it getting the the UDP.remotePort() wrong?

Thanks!