Wifi Serial Bridge - ESP8266

Hi,

I have two Arduino Mega boards, both with built in ESP6288 wifi. I want to establish serial communication between the two arduino’s. I have done this previously with the HC-12 wireless module which was fairly straight forward.

I have not flashed the ESP module’s with firmware, simply unpackaged them and plugged them in. I’ve successfully got them both scanning for wireless networks as a test, but I need a little assistance on the serial comms.

I’ve adapted the code below from my previous HC-12 wireless serial project. However I’m unsure of how to configure the ESP to operate in serial transmission and on what channel. Using the HC-12 it was a simple case of

  // setup of the hc12 module (Transmit)
  digitalWrite(7,LOW); // enter AT command mode
  delay(2000);
  Serial1.write("AT+B115200"); // 115200
  delay(100);
  Serial1.write("AT+C001"); // Channel 1
  delay(100);
  Serial1.write("AT+FU3"); // FU3 trans mode
  delay(100);
  digitalWrite(7,HIGH); // enter transparent mode
  delay(100);

Below is the code I’m using on my current Mega / ESP8266 project.

Transmitter:

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

int intTS = 5;
int intTL = 3;

void setup() {
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial3.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial3);

  // check for the presence of the shield
  
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // Print WiFi MAC address
  printMacAddress();
}

void loop()
{
  // Transmit inputs to machine
  transInput();
}

void transInput() {
  static unsigned long lastDatatime;
  if (millis() - lastDatatime >= 100) {

  Serial3.print("<");
  Serial3.print(intTS);
  Serial3.print(", ");
  Serial3.print(intTL);
  Serial3.println(">");
    
    lastDatatime = millis(); 
       
   }
}

// Output to console
void serialOutput() {
  Serial.print("<");
  Serial.print(intTS);
  Serial.print("/");
  Serial.print(intTL);
  Serial.println(">");
}

void printMacAddress()
{
  // get your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  
  // print MAC address
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

Code for the Reciever:

const byte buffSize = 64;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;
int upLink = 0;

char messageFromPC[buffSize] = {0};
int intTS = 0;
int intTL = 0;

unsigned long curMillis;

unsigned long prevReplyToPCmillis = 0;
unsigned long replyToPCinterval = 1000;

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

void setup() {
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial3.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial3);

  // check for the presence of the shield
  
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // Print WiFi MAC address
  printMacAddress();
}

void loop()
{

 curMillis = millis();

  // Validate functioning wifi uplink
  wifiLinkstatus();

  // Recieve data from controller
  recData();

   // Output to machine
  if (upLink == 1) {

    if (newDataFromPC) {
    newDataFromPC = false;
    Serial.print("<TS: ");
    Serial.print(intTS);
    Serial.print(" TL: ");
    Serial.print(intTL);
    Serial.println(">");
  }
    
    } else {
    Serial.println("No communication with controller");
  }
 
}

void wifiLinkstatus() {

  static unsigned long lastDatatime;
  
   if (Serial3.available() == 0) {
      if (millis() - lastDatatime >= 1000) {
        upLink = 0; }
   } else {
      upLink = 1;
      lastDatatime = millis();
   }
}

void recData() {

    // receive data from PC and save it into inputBuffer
    
  if(Serial3.available() > 0) {

    char x = Serial3.read();

      // the order of these IF clauses is significant
      
    if (x == endMarker) {
      readInProgress = false;
      newDataFromPC = true;
      inputBuffer[bytesRecvd] = 0;
      parseData();
    }
    
    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;
      bytesRecvd ++;
      if (bytesRecvd == buffSize) {
        bytesRecvd = buffSize - 1;
      }
    }

    if (x == startMarker) { 
      bytesRecvd = 0; 
      readInProgress = true;
    }
  }
}
 
void parseData() {

    // split the data into its parts
    
  char * strtokIndx; // this is used by strtok() as an index
  
  strtokIndx = strtok(inputBuffer,",");      // get the first part - the string
  intTS = atoi(strtokIndx);
  
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  intTL = atoi(strtokIndx);     // convert this part to an integer

}

void printMacAddress()
{
  // get your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  
  // print MAC address
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

Any help appreciated.

Thanks

Ignore me. Sorry.

the esp8266 can be configured with AT commands for transparent TCP connection, but not with the WiFiEsp library.

the WiFiEsp library and my new WiFiEspAT library wrap the AT commands in standard arduino networking API known from Arduino Ethernet or WiFi libraries.

In this API WiFiClient object handles a TCP socket. A normal TCP socket is connected to IP address and port. WiFiServer starts a listening socket on a port. If server on listening socket is contacted by a remote client socket, it creates a local socket connected with the remote client socket on a free port and returns a WiFiClient object wrapping the socket. Everything you write or print to a WiFiClient is send to that one remote socket.

If your client board creates a WiFiClient and connects it to IP address and port of the WiFiServer on your 'server' board, then you get there a WiFiClient from server.available() and this two WiFiClient objects are connected. What you write/print on one side you read only from the WiFiClient object on the other side.

client socket

if (client.connect(serverIP, PORT)) {
client.print("request\n");
String response = client.readStringUntil('\n');
Serial.println(response);
client.stop();
}

server side

WiFiClient client = server.available();
if (client && client.connected()) {
String request = client.readStringUntil('\n');
Serial.println(request);
client.print("response\n");
client.stop();
}

Thanks Juraj.

I'll have a good look into this later, it might take some getting my head around.

But basically what your saying is don't use the WiFiEsp library, use WiFiEspAT and configure the ESP for use in transparent TCP mode with a server / client setup. What is printed on Serial3 of the server arduino is recieved on Serial3 of the client arduino?

Have you any further examples of using your WifiEspAT library to configure the ESP as a server / client config in transparent mode?

Thanks,

Jim_cliff:
Thanks Juraj.

I'll have a good look into this later, it might take some getting my head around.

But basically what your saying is don't use the WiFiEsp library, use WiFiEspAT and configure the ESP for use in transparent TCP mode with a server / client setup. What is printed on Serial3 of the server arduino is recieved on Serial3 of the client arduino?

Have you any further examples of using your WifiEspAT library to configure the ESP as a server / client config in transparent mode?

Thanks,

no that is not what I say. the libraries have the same API and both use AT commands inside.

you can configure the transparent mode only directly using AT commands.