Today I managed to get an esp8266-01 module to make a server on my local network and send data. I then decided to try writing a separate arduino code to send information to the esp module and the esp module to send the received information to the local server. The problem is that the ESP module creates the server but does not receive the information from the arduino.
Arduino code:
#include <SoftwareSerial.h>
SoftwareSerial espSerial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
espSerial.begin(115200);
}
void loop() {
// Send number 1 to ESP8266
espSerial.println("1");
delay(1000); // Wait for 1 second
}
the UNO uses 5V logic so you should have a potential divider on the UNO Tx line to convert the 5V signal to 3V for the ESP-01, e.g. here I am using UNO pins 8 and 9
also rather than SoftwareSerial I use Alt SoftSerial, e.g.
// UNO - ESP-01 AltSoftSerial test - ESP-01 loopsback - character transmitted to it are echoed back
// requires program ESP-01_Serial_loopback loaded in to ESP-01 and serial connections (see below)
// note that at baudrates above 38400 some echoed characters are corrupted
#include <AltSoftSerial.h>
// load program ESP-01_Serial_loopback in to ESP-01
// ESP-01 U0TXD to UNO RX pin 8
// ESP-01 U0RXD to UNO TX pin 9
// ESP-01 GPIO0 and GPIO2 need to have pull-up resistors connected to ensure the module starts up correctly
// The ESP-01S has 12K resistors on the board for GPIO0, RST and CH_PD
AltSoftSerial espSerial;
void setup() {
Serial.begin(115200);
Serial.println("AltSoftSerial test");
//Begin serial communication with Arduino and SIM800L
espSerial.begin(38400);
Serial.println("ESP-01 serial intialized");
Serial.println("load ESP-01_Serial_loopback in to ESP-01");
Serial.println("ESP-01 U0TXD to UNO RX pin 8");
Serial.println("ESP-01 U0RXD to UNO TX pin 9");
}
void loop() {
if (Serial.available()) {
char command = Serial.read();
//Serial.println(command);
espSerial.print(command);
}
while (espSerial.available()) {
char reponse = espSerial.read();
Serial.print(reponse);
}
}
In your code i do not see any part that actually does the Serial reception ? Your UNO code does send
"1/n" , but nowhere do you do anything about reception on the ESP.
And all of the above (seldom do i see a post where so much information is correct), observe also the Baudrate on softwareSerial, i does ot get mentioned (though corrected) but 115200 is not a very reliable baudrate for softwareSerial, for sure not on reception, and a bit iffy on transmission
This actually looks like the opposite of what you intended. It takes an argument from the server (chances are this argument doesn't exist but that is a different matter)
and writes that to the Serial port. Whereas you want read from the Serial port, and display on the webpage.
I don't think it's a perfect method but there should be no reason why almost the same should not work on the ESP unless there is a hardware issue.
Main thing is that transmission at 9600bps takes about 1 ms / byte, and reception is much faster.
So far you have succeeded because the whole string has been transmitted (and received) before parsing starts.
There are loads of examples for Serial transmission and reception to be found, many of which are none-blocking in nature, this is what you want.
have a look at Serial input basics
The problem with this module is that I have no way to follow through the serial monitor what happens to it if the information is transmitted from the arduino to the esp. I think it will be much easier if I buy an esp32 which I can always monitor in real time.
That is not true strictly speaking, There are 2 available Serial output pins available, but you would need to receive that information and convert it from TTL to USB. Another option would be for the ESP to just echo back whatever it receives. I have Serial transmission between boards stable, but i still use the builtin led on GPIO 1 (also TX0 ) to let me know where i am in a program. But if you think a nodeMCU orESP32 will solve your problem it could be your solution. As a guitarist i always figure that more practice is more important than a better guitar. The practice can also be applied on another guitar.
One of the questions i have with your current project is 'why ?' do you want to send data from an arduino to an ESP, but i assumed 9incorrectly) that it was for educational purposes.
very simple test program
ESP-01S reads characters from serial U0RXD (GPIO3) and echo back to U0TXD (GPIO1)
// ESP-01 blink LED and read characters from serial U0RXD and echo back to U0TXD
void setup() {
Serial.begin(38400);
delay(1000);
Serial.println();
Serial.println("\n\nESP-01 blinking LED and read characters from serial U0RXD and echo back to U0TXD ");
Serial.print("LED blinking pin ");
Serial.println(LED_BUILTIN);
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
static long timer = millis();
if (millis() - timer > 500) {
timer = millis();
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
// read serial U0RXD and echo it back to U0TXD
while (Serial.available() > 0) {
char ch = Serial.read();
Serial.write(ch);
}
}
this UNO program reads on pins 8and transmis on pin 9
// UNO - ESP-01 AltSoftSerial test - ESP-01 loopsback - character transmitted to it are echoed back
// requires program ESP-01_Serial_loopback loaded in to ESP-01 and serial connections (see below)
// note that at baudrates above 38400 some echoed characters are corrupted
#include <AltSoftSerial.h>
// load program ESP-01_Serial_loopback in to ESP-01
// ESP-01 U0TXD to UNO RX pin 8
// ESP-01 U0RXD to UNO TX pin 9
// ESP-01 GPIO0 and GPIO2 need to have pull-up resistors connected to ensure the module starts up correctly
// The ESP-01S has 12K resistors on the board for GPIO0, RST and CH_PD
AltSoftSerial espSerial;
void setup() {
Serial.begin(115200);
Serial.println("AltSoftSerial test");
//Begin serial communication with Arduino and SIM800L
espSerial.begin(38400);
Serial.println("ESP-01 serial intialized");
Serial.println("load ESP-01_Serial_loopback in to ESP-01");
Serial.println("ESP-01 U0TXD to UNO RX pin 8");
Serial.println("ESP-01 U0RXD to UNO TX pin 9");
}
void loop() {
if (Serial.available()) {
char command = Serial.read();
//Serial.println(command);
espSerial.print(command);
}
while (espSerial.available()) {
char reponse = espSerial.read();
Serial.print(reponse);
}
}
connect as diagram in post 4, e.g.
ESP-01 pin GPIO1 U0TXD to UNO pin 8 RX
ESP-01 pin GPIO3 U0RXD to UNO pin 9 TX
text entered on the UNO serial monitor to transmitted to the ESP-01 which echoes it back and displayed on the serial monitor
as @jremington stated in post 18 the UNO software serial does not work well at high baudrates
the above UNO code uses AltSoftSerial at 38400baud