Chat freely about anything...

User avatar
By Ian Phill
#57154 Hello everyone. Somebody can to explain me how function the serial port between devices and ESP8266 Modules? I've two device, one transmitter and one receiver, it's communicating through two cables:
TRANSMITTER RECEIVER
(TX) ============> (RX)
(RX) ============> (TX)
But now I wanna do wireless, for that I use two ESP8266 Modules, the Module A will get the signal from TRANSMITTER and will put in the server, after that, the Module B will get the same signal and will deliver to RECEIVER. Below I try to explain the connection:
TRANSMITTER Module A Module B RECEIVER
(TX) ============> (RX) wireless (TX) ==========> (RX)
(RX) ============> (TX) wireless (RX) ==========> (TX)

In the bottom I show you me code in both modules, I've been working in this project for three days and I haven't been able to solve this yet. I appreciate whatever help. Thank you so much in advance.

//====== MODULE A ========//
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "ESPap";
const char *password = "thereisnospoon";
int incomingByte = 0; // for incoming serial data

ESP8266WebServer server(80);

void handleRoot() {
server.send(200, "text/html", String("<h1>@") + incomingByte + "</h1>");
}

void setup() {
delay(1000);
Serial.begin(9600);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
server.on("/index.html", handleRoot);
server.begin();
}

void loop() {
server.handleClient();
if (Serial.available() > 0) {
incomingByte = Serial.read(); // read the incoming byte:
}

server.send(200, "text/html", String("<h1>@") + incomingByte + "</h1>");
delay(1000);
}

//========= MODULE B =========//
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

const char WIFI_SSID[] = "ESPap";
const char WIFI_PSK[] = "thereisnospoon";

const char http_site[] = "192.168.4.1";
const int http_port = 80;

char strVal[8];
int pos;
bool flag = false;

WiFiClient client;

void setup() {
Serial.begin(9600);

connectWiFi();

if ( !getPage() ) {
Serial.println("GET request failed");
}
}

void loop() {

// If there are incoming bytes, print them
if ( client.available() ) {
char c = client.read();
//Parsing the HTML request
if(c=='@') {
flag = true;
}
else if(flag && c!='<') {
strVal[pos] = c;
pos++;
}
else {
flag = false;
}
}

// If the server has disconnected, stop the client and WiFi
if ( !client.connected() ) {
//print to incoming value
Serial.println(strVal);

// Close socket and wait for disconnect from WiFi
client.stop();
if ( WiFi.status() != WL_DISCONNECTED ) {
WiFi.disconnect();
}

connectWiFi();

if ( !getPage() ) {
Serial.println("GET request failed");
}
}
}

// Attempt to connect to WiFi
void connectWiFi() {
// Set WiFi mode to station (client)
WiFi.mode(WIFI_STA);

// Initiate connection with SSID and PSK
WiFi.begin(WIFI_SSID, WIFI_PSK);

// Blink LED while we wait for WiFi connection
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
}
}

// Perform an HTTP GET request to a remote page
bool getPage() {
// Attempt to make a connection to the remote server
if ( !client.connect(http_site, http_port) ) {
return false;
}

// Make an HTTP GET request
client.println("GET /index.html HTTP/1.1");
client.print("Host: ");
client.println(http_site);
client.println("Connection: close");
client.println();

pos = 0;

return true;
}