ASCII of integer to integer in Arduino

635 views Asked by At

I am working on a project with LoRa and Arduino and I am facing a weird issue where when I transmit a integer the receiver receives at ASCII value which is not good at my case because I wanted to transmit sensor data(3 digits) which is not possible by ASCII. I will also attach my code(converted into a basic integer code for testing) I need a Solution to fix this BTW I am using Arduino UNO for transmitting and Arduino Mega for receiving and SX1278 LoRa Module for both transmitting and receiving.

Transmitter Code(Arduino UNO):

    #include <SPI.h>
    #include <LoRa.h>
    
    int val = 5;
    void setup() {
      Serial.begin(9600);
      while (!Serial);
    
      Serial.println("LoRa Sender");
    
      if (!LoRa.begin(433E6)) {
        Serial.println("Starting LoRa failed!");
        while (1);
      }
    }
    
    void loop() {
      Serial.print("Sending packet: ");
    
      // send packet
      LoRa.beginPacket();
      LoRa.print(val);
      LoRa.endPacket();
    
      delay(500);
    }

Receiver Code(Arduino Mega):

   #include <SPI.h>
   #include <LoRa.h>
   
   #define LORA_SS 53
   #define LORA_RST 9
   #define LORA_DIO0 8
   int val;
   
   void setup() {
     pinMode(LORA_SS, OUTPUT);
     digitalWrite(LORA_SS, HIGH);
   
     LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
     Serial.begin(9600);
     
     while (!Serial);
   
     Serial.println("LoRa Receiver");
   
     if (!LoRa.begin(433E6)) {
       Serial.println("Starting LoRa failed!");
       while (1);
     }
   }
   
   void loop() {
     // try to parse packet
     int packetSize = LoRa.parsePacket();
     if (packetSize) {
       // received a packet
       Serial.print("Received packet '");
   
       // read packet
       while (LoRa.available()) {
         //Serial.print((char)LoRa.read());
         int val = LoRa.read();
        
       }
      
       Serial.print(val);
       // print RSSI of packet
       Serial.print("' with RSSI ");
       Serial.println(LoRa.packetRssi());
     }
   }

Output of the receiver :

Received packet '53' with RSSI -5
1

There are 1 answers

0
Rohit On BEST ANSWER

To Fix this issue just send the value as an integer, since LoRa receives as char/string same it in a string variable in the receiver and convert it into integer with toInt(); and that will fix it!