Arduino UART ESP01 with 433 MHZ Receiver and Transmitter

Hello everyone, I am using arduino uno https://hackster.imgix.net/uploads/attachments/719718/after_programming_8mrh0Aoco7.jpg?auto=compress%2Cformat&w=1280&h=960&fit=max here is my esp and arduino connection.
My arduino and esp connection is fine i can take request from api.
I am using another arduino only connected to 433 MHZ Transmitter who send me numbers for rest api. Here is my transmitter code.

#include <RH_ASK.h>
#include <SPI.h> 
RH_ASK rf_driver;
void setup()
{
// Initialize ASK Object
rf_driver.init();
}
void loop()
{
const char *msg = "Number"; // sending number for my api like 533654
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
delay(1000);
}

And my receiver code is here

#include <ArduinoJson.h>
#include <RH_ASK.h>
#include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{ 
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
delay(1000); 
const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1) + 60;
DynamicJsonDocument doc(capacity);
delay(1000);
 DeserializationError error = deserializeJson(doc, message);
Serial.println(doc["attrName"].as<const char*>());
  
}
delay(1000);

}
}
When i write
DeserializationError error = deserializeJson(doc, message);
for json parse , I can't read transmitter message.I am reading transmiter message (533654 with reverse question mark or garbages) but i need to add this i guess for json parse so how can i add this correctly ? Does anyone know why its give me garbages when i add this DeserializationError error = deserializeJson(doc, message); code.
Any help will be appreciated. Thanks.

Your ESP's RX-pin Is not 5v Tolerant ! , so far so good, but time may change that.
About your question, i am not that familiar with Json at all, but i suspect a memory issue

unsigned char rxBuf[512];

Though never used and therefore probably not compiled, that is quite a chunk of memory (a quarter) to just give up. Combined with the Serial Rx & Tx buffers, and the use of the 'String' class for reception, that may be a cause.

String message = Serial.readString();

how many bytes are going to be in 'message' ?

DynamicJsonDocument doc(capacity);

And that is declared on the heap ?
I think it would be better to just send the data that you actually want, back to the UNO, and let the ESP do a bit more work.

i delete unsigned char but still not working so i add that again.
message is actually "49587465" i am sending with uint_8* so 1 byte i guess ?

and when i use assistant Arduino json v6 it gives me this

 // String input;

 StaticJsonDocument<48> doc;

 DeserializationError error = deserializeJson(doc, input);

 if (error) {
 Serial.print(F("deserializeJson() failed: "));
 Serial.println(error.f_str());
return;
 }
 const char* attribute= doc["attribute"]; // "

when i run it again on serial port i saw "49587465⸮"i can't see "49587465" don't know why.When i wirte this code closing this // DeserializationError error = deserializeJson(doc, message); i can see "07413943" clearly.

 #include <ArduinoJson.h>
 #include <RH_ASK.h>
 #include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{ 
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
delay(1000); 
const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(capacity);
delay(1000);
// DeserializationError error = deserializeJson(doc, message);
Serial.println(doc["attrName"].as<const char*>());
}
delay(1000);

I found the solution.It seems like i need to convert string this (String((char*)buf)) but not like this.Here is how i solved.

uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf)
if (rf_driver.recv(buf, &buflen))
{ 
  rf_driver.printBuffer("Got:",buf,buflen);
  String rcv;
  for (int i=0; i<buflen;i++){
    rcv+=(char)buf[i];
  }
 Serial.print("example.com/example"+rcv+";header");
 String input = Serial.readString();
 StaticJsonDocument<32> doc;

  DeserializationError error = deserializeJson(doc,input);

 if (error) {
 Serial.print(F("deserializeJson() failed: "));
 Serial.println(error.f_str());
 return;
}

float attribute= doc["attribute"]; // 600.1
Serial.println(float(attribute));
Serial.println(input);

Well yes, eh, to cast to a char* and then turn that into a String is silly, even if it is only so you can easily add it to other Strings (which might as well have been char* from the start). It is still using the String class, which is not very efficient, but i am happy it works now.

1 Like

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