Arduino UART with esp01

Hello everyone,
I was using raspberry pi pico then i decided to use arduino. When i was using raspberry pi pico
i wrote this for communication between esp01 and raspberry pi pico

from machine import UART,Pin
import time
uart = UART(0, rx=Pin(1), tx=Pin(0), baudrate=115200,rxbuf=512)
while True:
time.sleep(1)
uart.write("https://jsonplaceholder.typicode.com/todos/1"+"\n")
print(uart.read())

I am trying to same thing with arduino here

#define RX 0
#define TX 1
unsigned char rxBuf[512];`

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

void loop() {
  while(Serial.available()>0)
  {
    delay(1000);
    Serial.write("https://jsonplaceholder.typicode.com/todos/1","\n");
    Serial.print(Serial.read());
  }
}

when i upload this code on arduino i saw garbages like
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"SSID":⸮}U⸮⸮⸮-⸮⸮⸮;⸮⸮⸮⸮3⸮⸮⸮?⸮⸮⸮m⸮⸮-⸮⸮]v⸮⸮u⸮n5N⸮⸮fw⸮224https://jsonplaceholder.typicode.com/todos/1
w⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮)+1⸮⸮5
and sometimes i saw
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"SSID":⸮}U⸮⸮⸮-⸮⸮⸮;⸮⸮⸮⸮3⸮⸮⸮?⸮⸮⸮m⸮⸮-⸮⸮]v⸮⸮u⸮n5N⸮⸮fw⸮224https://jsonplaceholder.typicode.com/todos/1 this and wdt 111132321354 something like that.Does anyone know how can i fix this?

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

What is sending the data to the Arduino? Still the ESP?

Where did you see that? Serial monitor? If so, depending on the Arduino board (Uno, Mega, ...), be aware that the pins 0 and 1 are shared with the USB interface.

I am using UNO.I am sending http to esp with arduino. Serial.print(Serial.read()); with this i want to see http request this:

"userId": 1,
	"id": 1,
	"title": "delectus aut autem",
	"completed": false

I see those question mark on serial monitor.

As pointed out, 'Serial' is used both for the USB port as for the communication between the ESP and the Arduino.
If you want the Arduino to function as an intermediate between the ESP and the USB Serial, you should either use another hwSerial if your device has one, or define a swSerial, but in that case you will need to first send the command to change the ESP's Baudrate to something that is reliable for the swSerial to receive (115200 isn't reliable for reception on swSerial, but 9600 kbps is)
What are you running on the ESP ? Standard firmware AT-commands ?
in that case,

Serial.write("https://jsonplaceholder.typicode.com/todos/1","\n");

this is not a valid command, and whether you use a PI or an Arduino, it will not have the desired result.

At the moment of course the ESP and the Arduino are both putting a signal on the same line (Probably)
Please show us how you are wiring this up. Are you using a voltage divider ?
And what is it you are trying to achieve as an end result ?

My arduino connected to computer.My arduino and esp conenction is:
Arduino's RX=Esp's TX, Arduino's TX=Esp's RX, Arduino's 3.3V =Esp's 3.3V and CH PD pin,GND=GND.
Esp code is working fine.When i use raspberry pi pico i saw http response.I am using Serial.write("https://jsonplaceholder.typicode.com/todos/1","\n") because my esp code needs https from outside.My esp code is String url = Serial.readStringUntil('\n'); so I need to http write from arduino.And esp code's are not AT commands. I though the problem is 512 thing.

Your ESP RX <-< Arduino TX should have a voltage divider in between. The ESP-01's RX pin is not 5v tolerant, so unless you want to fry it, either use a level shifter or a divider, to reduce the logic level to 3.3v

OK that is clear. Even better would be to share the ESP code, but i suppose we don't need it.

No, though the standard Serial buffer is only 128 bytes, this should easily be sufficient (there is rarely a need to devote 1/4 of the total available RAM to the Serial buffer)
The issue is the sharing of the UART with the USB.
Is there a way you can set the ESP-01's Serial port to a lower BAUD rate of 9600 kbps ?
In that case you can use swSerial to communicate with the ESP (that is assuming you are using an Arduino with a single UART)
There is of course the possibility that you have already fried the RX-pin due to omitting the voltage divider, but you can check that by hooking it back up to the Pi temporarily.

Then there are still some other programmatic misunderstandings to deal with, but let's first make sure you have it hooked up in a way that is appropriate.

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
 
#include <ESP8266HTTPClient.h>
 
#include <WiFiClientSecureBearSSL.h>
 
ESP8266WiFiMulti WiFiMulti;
 
void setup() {
Serial.begin(115200);
 
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("ssid", "password");
}
 
void loop() {
if ((WiFiMulti.run() == WL_CONNECTED)) {
 
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
 
client->setInsecure(); // client->setFingerprint(fingerprint);
 
HTTPClient https;
 
String url = Serial.readStringUntil('\n');
 
if (url.length()>1)
{
if (https.begin(*client, url)) {
 
int httpCode = https.GET();
 
if ((httpCode > 0) &&
(httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)) {
Serial.println(https.getString());
}
else {
Serial.println();
}
 
https.end();
} else {
Serial.println();
}
}
}
}

here is my esp code. If it isn't rxbuf thing then i dont know why this code is not working. I tried to pull serial begin 9600 but it get even worse. I need to write url from arduino because of esp's code:
String url = Serial.readStringUntil('\n');

#define RX 0
#define TX 1
unsigned char rxBuf[512];
void setup() {
  Serial.begin(115200);
  
}

void loop() {
  while(Serial.available()>0). // Actually don't need.Without this my code is still working.
  {
    delay(1000);
    Serial.print("jsonplaceholder.typicode.com/todos/1\n");
    delay(1000);
    Serial.print(Serial.readString());
  }

}

When i do that my code is working.But sometimes response come like this ⸮⸮"userId": 1,"id": 1,"title": "delectus autautem","completed": false. is that question marks a problem ? If it's a problem how can i fix those. Again thank you.

What you should do instead of using the UART on the Arduino 'both for sending to the ESP as to the Serial Monitor' is use swSerial to communicate between the ESP and the Arduino, and use hwSerial just for communication over USB.
so in your ESP code change the BAUD rate to 9600

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

connect the ESP to 2 different pins, but don't forget about the voltage divider on the ESP's RX pin. ! i can not stress that enough.

#define RX 2
#define TX 3
#include <SoftwareSerial.h>
SoftwareSerial espSerial(RX, TX); // RX, TX

void setup() {
  Serial.begin(115200);  /./ this is the baudrate for the USB
  espSerial.begin(9600);  
}

void loop() {  
  espSerial.print("jsonplaceholder.typicode.com/todos/1\n");
​ //delay(1000); // this is a rough method for letting the Serial buffer fill up
delay(50); // just enough to let the ESP respond should do.
         // ideally you would use millis() to send a request every second or so, and let Serial passthrough run
        // continously in the background.
 while(espSerial.available()>0)  {
    Serial.write(espSerial.read());
    if (espSerial.available()==0) delay(2); // i prefer this to prevent overflow and underrun.
  }
 delay(1000); // and do the delay here
}

Like this your Arduino communicates on 1 line with the ESP and passes results back to the Serial monitor on another.
Why do you actually need the Arduino though ? You could just let the ESP communicate with the SM directly.

jsonplaceholder is not my real api.I need arduino because when i get http request, i ll have a data then i will use that data with arduino. I took BAUD rate 9600 for now my system is working but sometimes my esp is going crayz and stopping giving me data and starts decoder cut or ???? something like that.I don't know why.

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (0):
epc1=0x40244460 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

stack>>>
ctx: sys
sp: 3fffeda0 end: 3fffffb0 offset: 0190
3fffef30: 4024446e 3fffdab0 00000000 3ffe84e4
3fffef40: 40243db7 3fffdab0 00000000 4020b660
3fffef50: 3ffe9000 40000f49 3fffdab0 40000f49
3fffef60: 40000e19 00000005 00065528 00000000
3fffef70: 00000000 aa55aa55 000000f0 401047dd
3fffef80: 401047e3 00065528 00000000 cdad0541
3fffef90: 4010000d e6643a8c b296b97f 1bf571f4
3fffefa0: 40242c88 3fffef3c 40242c41 3ffffab8
3fffefb0: 3fffffc0 00000000 00000000 feefeffe
3fffefc0: feefeffe feefeffe feefeffe feefeffe
3fffefd0: feefeffe feefeffe feefeffe feefeffe
3fffefe0: feefeffe feefeffe feefeffe feefeffe
3fffeff0: feefeffe feefeffe feefeffe feefeffe
3ffff000: feefeffe feefeffe feefeffe feefeffe
3ffff010: feefeffe feefeffe feefeffe feefeffe
3ffff020: feefeffe feefeffe feefeffe feefeffe
3ffff030: feefeffe feefeffe feefeffe feefeffe
3ffff040: feefeffe feefeffe feefeffe feefeffe
3ffff050: feefeffe feefeffe feefeffe feefeffe
3ffff060: feefeffe feefeffe feefeffe feefeffe
3ffff070: feefeffe feefeffe feefeffe feefeffe
3ffff080: feefeffe feefeffe feefeffe feefeffe
3ffff090: feefeffe feefeffe feefeffe feefeffe
3ffff0a0: feefeffe feefeffe feefeffe feefeffe
3ffff0b0: feefeffe feefeffe feefeffe feefeffe
3ffff0c0: feefeffe feefeffe feefeffe feefeffe
3ffff0d0: feefeffe feefeffe feefeffe feefeffe

like this.
My ESP crashes running some code. How to troubleshoot it? — ESP8266 Arduino Core 3.1.2-21-ga348833 documentation here is solution xd

Don't know why the ESP crashes, maybe try an older core and see if problem persist.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.