Chat here is you are connecting ESP-xx type modules to existing AVR based Arduino

Moderator: igrr

User avatar
By blacktrooper2
#66461 Hi all,

I would like to use an ESP8266 to avoid cables !

So here are my specifications :
- not change my previous Arduino codes -> UART
- as fast as possible (of course)
- reliable (of course again)
- avoid the use of external app -> only arduino IDE + libraries
- TCP for data transmission check (so no UDP)

The system would like :
Arduino <---UART---> ESP8266 <---WIFI---> ROUTER <---WIFI---> Computer


So i tried, and tried, an tried again to modify several codes I found on the web.
I have something not too bad. I am able to:
- send data from Arduino to ESP via UART (9600 bauds)
- send data from ESP to Computer via existing WIFI
- receive data from Computer to ESP via the same existing WIFI
- soon receive data from ESP to Arduino via UART

But at this point, I can already notice that wifi slows down and even lag.
To check my program, instead of hooking futur sensors, I'm generating a signal on Arduino.
It's a basic ascending and descending integer from 0 to 1024.
At each loop, I try to send it to the computer.

I notice that the first seconds, from 2 sec to 10 sec, everything is fine.
The signal received via wifi is visualised on MaxMSP and is good.
But after a while, I notice that there is randomly half second lags and a slower reception of the data.

NB :
1) I tried with a real router and my phone as an Access Point -> same results
2) ESP8266-12
3) UART connection over 9600 bauds between ESP and Arduino looks unstable. If someone has an idea to improve it....

I give you my full code and screen shots. I would pleased if it can be used by someone else and improved for the community benefits.


Arduino code:
Code: Select all//   ARD UNO        ESP8266
//         D10 <-> TX                         // Unhook before upload sketch
//         D11 <-> RX                         // Unhook before upload sketch
//         GND <-> GND

#include <SoftwareSerial.h>
#define     RXpin      10                     // MOD
#define     TXpin      11                     // MOD
String      UARTsendData;                     // AUTO

SoftwareSerial espSerial(RXpin, TXpin);

void setup() { // -----------------------------------------------------------------------
  Serial.begin(57600);                        // For com with computer
  espSerial.begin(9600);                      // For com with ESP
}

void loop() { // ------------------------------------------------------------------------
  // Generating a triangle wave forms
  for (int i = 0; i < 1024; i++) {         
    UARTsendData = i;                     
    UARTsend();                           
  }                                       
  for (int i = 1023; i > 0; i--) {         
    UARTsendData = i;                   
    UARTsend();                           
  }                                     
}

void UARTsend() { // --------------------------------------------------------------------
  espSerial.print(UARTsendData);
  espSerial.println("F");
}






ESP code:
Code: Select all
//   ARD UNO        ESP8266
//         D10 <-> TX                         // Unhook before upload sketch
//         D11 <-> RX                         // Unhook before upload sketch
//         GND <-> GND

#include <ESP8266WiFi.h>
#define   RcvCount 10
#define   Tempo    1

const char*       ssid             = "Z3K";                      // MOD
const char*       pwd              = "pwd";                      // MOD
const char*       hostIP           = "192.168.43.220";           // MOD
const int         sendPort         = 8889;                       // MOD
const int         recvPort         = 8890;                       // MOD

String            UARTrcvData;                            // AUTO
char              UARTrcvBuffer;                          // AUTO
int               UARTrcvCount;                           // AUTO

String            WIFIsendData;                           // AUTO
String            WIFIrcvData;                            // AUTO
char              WIFIrcvBuffer;                          // AUTO
int               WIFIrcvCount;                           // AUTO

WiFiServer server(recvPort);
WiFiClient client;

void setup() { // -----------------------------------------------------------------------
  Serial.begin(9600);                         // For com with ARD
  WIFIconnect();
  WIFIserverConnect();
  server.begin();
  client.setNoDelay(1);
  Serial.println("\nInitialized");
}

void loop() { // -----------------------------------------------------------------------
    UARTreceive();                       
    WIFIsendData = UARTrcvData;         
    WIFIsend();                         
    WIFIreceive();                       
}

void WIFIconnect() { // ----------------------------------------------------------------
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pwd);

  while (WiFi.status() != WL_CONNECTED) {
    delay(Tempo);
  }
}

void WIFIserverConnect() { // ----------------------------------------------------------
  if (!client.connect(hostIP, sendPort)) {
  }
}

void WIFIreceive() { // ----------------------------------------------------------------
  WIFIrcvCount = 0;
  WIFIrcvBuffer = 'N';
  WIFIrcvData = "";

  WiFiClient client = server.available();

  if (client.connected()) {
    while (WIFIrcvBuffer != 'F' && WIFIrcvCount < RcvCount) {
      WIFIrcvBuffer = client.read();
      if (WIFIrcvBuffer != 'F') {
        WIFIrcvData += WIFIrcvBuffer;
        WIFIrcvCount++;
      }
    }
    if (debug == true) {
      Serial.println(WIFIrcvData);
    }
  }

  client.flush();
}

void WIFIsend() { // -------------------------------------------------------------------
  if (WiFi.status() != WL_CONNECTED) {
    WIFIconnect();
  }

  client.flush();

  if (client.connected()) {
    client.println(WIFIsendData);
  }
}

void UARTreceive() { // ----------------------------------------------------------------
  UARTrcvCount = 0;
  UARTrcvBuffer == 'N';
  UARTrcvData = "";

  while (UARTrcvBuffer != 'F' && UARTrcvCount < RcvCount) {
    while (Serial.available() > 0) {
      UARTrcvCount = UARTrcvCount + 1;
      UARTrcvBuffer = (char)Serial.read();
      if (UARTrcvBuffer != 'F') {
        UARTrcvData += UARTrcvBuffer;
      }
    }
  }
}
You do not have the required permissions to view the files attached to this post.
User avatar
By Teong
#72518
Mai Ehab wrote:i have been searching for someone dealing with ESP8266 with Arduino and sending data on serial and finally found your post here , can you please help me to get to understand your code better ?

i hv the same problem also
did anyone have the solutio ?