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())
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.
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,
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.
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.