Go Down

Topic: ESP8266 to Arduino UNO - UDP receiver (Read 576 times) previous topic - next topic

Jordo

Mar 14, 2017, 04:47 pm Last Edit: Mar 14, 2017, 07:34 pm by Jordo
Hello everyone,

A few days ago I received my ESP8266 modules.
Her is how i connected my ESP to my Arduino UNO
Arduino tx   -> ESP rx
Arduino rx   -> ESP tx
Arduino gnd -> ESP gnd
3,3v           -> ESP vcc

With an empty arduino i could confgure my ESP and connect it to WIFI.
But now I am trying to use my Arduino with ESP as UDP receiver. And now I am not able to connect the Arduino to the internet at all.

On internet i found an library named ESP8266wifi.h and the following code, thats all I could find.
(yeah i know, I am not that good in searching this kind of things on google)

Also everytime i want to compile my code, it says 'WiFi' was not declared...


Code: [Select]
#include <WiFiUdp.h>
#include <ESP8266wifi.h>

const char* ssid = "********";
const char* password = "********";

WiFiUDP Udp;
unsigned int localUdpPort = 8081;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
char  replyPacekt[] = "Received";  // a reply string to send back

void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.print("Connecting to, ");
  Serial.println(ssid);
  WiFi.begin();

  Udp.begin(localUdpPort);
  Serial.print("Now listening at IP, ");
  Serial.println(WiFi.localIP());
  Serial.print("UDP port, ");
  Serial.print(localUdpPort);

}


void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    Serial.print("Received ");
    Serial.print(packetSize);
    Serial.print(", bytes from ");
    Serial.print(Udp.remoteIP());
    Serial.print(", at port ");
    Serial.println(Udp.remotePort());

    int len = Udp.read(incomingPacket, 255);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    Serial.print("UDP packet contents: ");
    Serial.println(incomingPacket);

    // send back a reply, to the IP address and port we got the packet from
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyPacekt);
    Udp.endPacket();
  }
}


I hope one of you have a sollution or a good tutorial for me.

- Jordo
- Jordo

pert

On internet i found an library named ESP8266wifi.h
Post a link to where you downloaded the library from. Use the chain links icon on the toolbar to make the link clickable.

and the following code
Post a link to where you got the code from.

I am not that good in searching this kind of things on google
I am, but I still shouldn't be required to search all over the internet to find a library and code that may or may not be the ones you're using.

Also everytime i want to compile my code, it says 'WiFi' was not declared...
It says a lot more than that. When you encounter an error you'll see a button on the right side of the orange bar "copy error messages". Click that button and then paste the error in a reply here using code tags as you have done with your code above.

Jordo

First, thanks for your fast reply!


The link wereld in found the code
https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/udp-examples.md

I made al little change at the print lines. Because i don't use printf.


Link for the library
https://github.com/ekstrand/ESP8266wifi


The error code:
Code: [Select]
Arduino: 1.6.12 (Windows 10), Board:"Arduino/Genuino Uno"

C:\Users\****\Documents\Program\ESP8266_UDP\ESP8266_UDP.ino: In function 'void setup()':

ESP8266_UDP:19: error: 'WiFi' was not declared in this scope

   WiFi.begin();

   ^

exit status 1
'WiFi' was not declared in this scope


I hope this is all the info you need, if not, let me know

Thanks!
-Jordo
- Jordo

pert

If you look closely at the code you'll notice the filename ESP8266WiFi.h has different capitalization than the library you're using, ESP8266wifi.h. This is a clue. Further investigation will show that https://github.com/esp8266/Arduino is the "ESP8266 core for Arduino". Which is used for programming the ESP8266 directly rather than programming an Uno connected to an ESP8266 as you're doing. Thus that code you found will absolutely not work for your application and is in fact not even written for the library you're using. It's quite confusing that there are two different libraries, ESP8266WiFi and ESP8266wifi which are for somewhat similar but actually quite different purposes which have almost identical filenames which in fact are interchangeable on non-case sensitive operating systems like Windows.

I would recommend you to use this library:
https://github.com/bportaluri/WiFiEsp
It's very good and does support UDP but unfortunately the author insists on leaving debug output turned on, which prints to Serial. Since it appears your ESP8266 is also connected to the Serial pins this causes the library to not work unless you make the following change:
https://github.com/bportaluri/WiFiEsp/pull/46/files
The library includes example code for UDP but you will have to make some adjustments since it is intended to be used with the ESP8266 connected to other pins on the Arduino and using SoftwareSerial library.

Jordo

Thanks for your help!
I am gonna try these codes and Let it know.

Indeed it's very confusing that there are two library's with the same name. I thought they had a typo or something.

Quote
The library includes example code for UDP but you will have to make some adjustments since it is intended to be used with the ESP8266 connected to other pins on the Arduino and using SoftwareSerial library.
Softwareserial is not a problem. But I heard that softwareserial is not useable with a baudrate of 115200.
Do you know something about that?

Again thank you very much!

-jordo
- Jordo

pert

I heard that softwareserial is not useable with a baudrate of 115200.
That's my understanding also, though I've never used SoftwareSerial. It's a problem because the typical default baud rate of the ESP8266 is 115200 but that can be changed via AT commands. If your ESP8266 is connected to pins 0 and 1 of the Uno then you don't need to use SoftwareSerial because you can use hardware serial instead.

Jordo

Quote
It's a problem because the typical default baud rate of the ESP8266 is 115200 but that can be changed via AT commands.
I think I need yo do that, because the library you recommended needs software serial.
This AT command is useable on every esp?
Or is that for the New onesie r something?

-Jordo
- Jordo

pert

I think I need yo do that, because the library you recommended needs software serial.
Wrong. The WiFiEsp library can use hardware or software serial. The example sketches are written to use the SoftwareSerial library on boards such as Uno that only have a single hardware serial port but they can easily be modified to work with hardware serial. However, I would recommend against using Serial for both communication with the ESP8266 and debug output to the Serial Monitor at the same time. It does work somewhat but the debug output can interfere with the communication with the ESP8266.

This AT command is useable on every esp?
Or is that for the New onesie r something?
Any ESP8266 that has the AT firmware installed. Most ship with the AT firmware. If it doesn't have it installed then you will need to install it before you can use the ESP8266 with either the WiFiEsp or ESP8266wifi libraries.

Jordo

I did all steps, changed the softserial to serial, but the code still cannot connect to my ESP8266.

Code: [Select]
/*
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>

char ssid[] = "****";            // your network SSID (name)
char pass[] = "****";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

unsigned int localPort = 10002;  // local port to listen on

char packetBuffer[255];          // buffer to hold incoming packet
char ReplyBuffer[] = "RECEIVED";      // a string to send back

WiFiEspUDP Udp;

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  //Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present");
    // don't continue:
    digitalWrite(13, HIGH);
    while (true);
//Code keeps stuck here, because it thinks there is no ESP
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    //Serial.print("Attempting to connect to WPA SSID: ");
    //Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  //Serial.println("Connected to wifi");
  printWifiStatus();

  //Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);

//  Serial.print("Listening on port ");
//  Serial.println(localPort);
}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
//    Serial.print("Received packet of size ");
//    Serial.println(packetSize);
//    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
//    Serial.print(remoteIp);
//    Serial.print(", port ");
//    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
//    Serial.println("Contents:");
//    Serial.println(packetBuffer);

    // 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();
  }
}


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);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
//  Serial.print("signal strength (RSSI):");
//  Serial.print(rssi);
//  Serial.println(" dBm");
}
- Jordo

Jordo

I did all steps, changed the softserial to serial, but the code still cannot connect to my ESP8266.
I also comment al Prints, because that will influence the connection with the ESP.

Code: [Select]
/*
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>

char ssid[] = "****";            // your network SSID (name)
char pass[] = "****";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

unsigned int localPort = 10002;  // local port to listen on

char packetBuffer[255];          // buffer to hold incoming packet
char ReplyBuffer[] = "RECEIVED";      // a string to send back

WiFiEspUDP Udp;

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  //Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present");
    // don't continue:
    digitalWrite(13, HIGH);
    while (true);
   //Code keeps stuck here, because it thinks there is no ESP
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    //Serial.print("Attempting to connect to WPA SSID: ");
    //Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  //Serial.println("Connected to wifi");
  printWifiStatus();

  //Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);

//  Serial.print("Listening on port ");
//  Serial.println(localPort);
}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
//    Serial.print("Received packet of size ");
//    Serial.println(packetSize);
//    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
//    Serial.print(remoteIp);
//    Serial.print(", port ");
//    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
    }
//    Serial.println("Contents:");
//    Serial.println(packetBuffer);

    // 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();
  }
}


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);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
//  Serial.print("signal strength (RSSI):");
//  Serial.print(rssi);
//  Serial.println(" dBm");
}


I hope i made a typo...

- Jordo
- Jordo

pert

Did you make this change to the library:
https://github.com/bportaluri/WiFiEsp/pull/46/files
I can provide specific instructions for how to do that if necessary.

I hope i made a typo...
You did:
Code: [Select]
/*
That line causes your code to not compile.

Jordo

#11
Mar 16, 2017, 11:00 pm Last Edit: Mar 16, 2017, 11:07 pm by Jordo
Did you make this change to the library:
https://github.com/bportaluri/WiFiEsp/pull/46/files
Yes i made this changes.

But there is no compile problem,
The code keeps stopping at this point in the code:

Code: [Select]
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present");
    // don't continue:
    digitalWrite(13, HIGH);
    while (true);
   //Code keeps stuck here, because it thinks there is no ESP
  }


Like if there is no ESP at all.
I tried different ways to connect the ESP to the arduino, but nothing helped.

And with the Softserial as suggest by the maker, it works partly. Sometimes it connects to the wifi, and thats it.


- Jordo
- Jordo

pert

When you were successfully using the ESP8266 previously was it at 115200 baud? Were you using AT commands exclusively?

Please say exactly how you have the ESP8266 connected to the Arduino.

Jordo

#13
Mar 16, 2017, 11:18 pm Last Edit: Mar 16, 2017, 11:28 pm by Jordo
hen i connect my ESP via Hardware Serial to my Arduino, and the Arduino has no code uploaded, i have fully control of the ESP with AT commands.

When i upload my code configured for Hardware serial (TX ESP to TX Arduino) as you suggest, there is no response from the ESP. The code keep stopping in the whileloop i showed in last post.

When i upload my code configured for SoftwareSerial (TX ESP to Virtual RX Arduino) as the library maker sugest, the ESP give Response between 1 out of 3 and 1 out of 5 of the times.
When the ESP give response, the Serial monitor says:

Code: [Select]

Attempting to connect to WPA SSID: *****
SSID:
IP Address: 0.0.0.0
signal strength (RSSI):-403 dBm
Listening on port 10002


When the ESP does not give response
The Serial monitor says:
Code: [Select]

[WiFiEsp] Cannot initialize ESP module
WiFi shield not present


Is this what you mean?

- Jordo
- Jordo

pert

hen i connect my ESP via Hardware Serial to my Arduino, and the Arduino has no code uploaded, i have fully control of the ESP with AT commands.
At what baud rate?

Please say exactly which pins on the ESP8266 are connected to which pins on the Arduino.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy