Int to uint8 value

Hi,

I have an integer value that has to be converted into uint8_t . Till that moment I have not reached a solution. Receiver (Server2) in my Lora_Mesh network keeps printing NULL or symbols .

// Node1_Lora_Mesh_client
 
 
#define RH_MESH_MAX_MESSAGE_LEN 50
 
#include <RHMesh.h>
#include <RH_RF95.h>
#include <SPI.h>
#include <inttypes.h>
#include <Arduino_BuiltIn.h>


#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
 
// Singleton instance of the radio driver
RH_RF95 driver;
 

RHMesh manager(driver, CLIENT_ADDRESS);
 
void setup() 
{
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("RF95 init failed");
  
}
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
unsigned long previousMillis=0;
 
void loop()
{
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= 30000)
  { 
    previousMillis = currentMillis;
    
    int analog = analogRead(A0); //read in as int.
    Serial.println(analog);
   
    Serial.println("Sending to manager_mesh_server2");
    
    // Send a message to a rf95_mesh_server
    // A route to the destination will be automatically discovered.
    if (manager.sendtoWait(data, sizeof(data), SERVER2_ADDRESS) == RH_ROUTER_ERROR_NONE)
      {
        // It has been reliably delivered to the next node.
        // Now wait for a reply from the ultimate server
        uint8_t len = sizeof(buf);
        uint8_t from;
        
        if (manager.recvfromAckTimeout(buf, &len, 3000, &from))
        {
        Serial.print("got reply from : 0x");
        Serial.print(from, HEX);
        Serial.print(": ");
        Serial.println((char*)buf);
        }
        else
        {
      Serial.println("No reply, is rf95_mesh_server1, rf95_mesh_server2 and rf95_mesh_server3 running?");
        }
    }
  else
     Serial.println("sendtoWait failed. Are the intermediate mesh servers running?");
  }
}

And on the server side

//Node3 Lora_Mesh_Server2

#define RH_MESH_MAX_MESSAGE_LEN 50
 
#include <RHMesh.h>
#include <RH_RF95.h>
#include <SPI.h>
 

#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
 
// Singleton instance of the radio driver
RH_RF95 driver;
int h;

RHMesh manager(driver, SERVER2_ADDRESS);
 
void setup() 
{
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("RF95 init failed");
  
}
 
uint8_t replydata[] = "MQ value received at Server2";


void loop()
{
  
  uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
  
  uint8_t len = sizeof(buf);
  uint8_t from;
  
  if (manager.recvfromAck(buf, &len, &from))
  {
     
    Serial.print("MQ Value from : 0x");
    Serial.print(from, HEX);
    Serial.print(": ");
    Serial.println((char *)buf);
    
    // Send a reply back to the originator client
    if (manager.sendtoWait(replydata, sizeof(replydata), from) != RH_ROUTER_ERROR_NONE)
      Serial.println("sendtoWait failed");
  }
}

What’s data ???

Of course

// Node1_Lora_Mesh_client
 
 
#define RH_MESH_MAX_MESSAGE_LEN 50
 
#include <RHMesh.h>
#include <RH_RF95.h>
#include <SPI.h>
#include <inttypes.h>
#include <Arduino_BuiltIn.h>


#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
 
// Singleton instance of the radio driver
RH_RF95 driver;
 

RHMesh manager(driver, CLIENT_ADDRESS);
 
void setup() 
{
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("RF95 init failed");
  
}
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
unsigned long previousMillis=0;
 
void loop()
{
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= 30000)
  { 
    previousMillis = currentMillis;
    
    int data = analogRead(A0); //read in as int.
    Serial.println(data));
   
    Serial.println("Sending to manager_mesh_server2");
    
    // Send a message to a rf95_mesh_server
    // A route to the destination will be automatically discovered.
    if (manager.sendtoWait(data, sizeof(data), SERVER2_ADDRESS) == RH_ROUTER_ERROR_NONE)
      {
        // It has been reliably delivered to the next node.
        // Now wait for a reply from the ultimate server
        uint8_t len = sizeof(buf);
        uint8_t from;
        
        if (manager.recvfromAckTimeout(buf, &len, 3000, &from))
        {
        Serial.print("got reply from : 0x");
        Serial.print(from, HEX);
        Serial.print(": ");
        Serial.println((char*)buf);
        }
        else
        {
      Serial.println("No reply, is rf95_mesh_server1, rf95_mesh_server2 and rf95_mesh_server3 running?");
        }
    }
  else
     Serial.println("sendtoWait failed. Are the intermediate mesh servers running?");
  }
}

And what did you expect to see there if you print the value as symbols:

?

Wha's arduino board you are using as client and as server?

Arduino UNO

try this:

if (manager.recvfromAck(buf, &len, &from))
  
        {
        Serial.print("Received from : 0x");
        Serial.print(from, HEX);
        Serial.print(", length = ");
        Serial.print(len);
        Serial.print(" :");
        if (len == sizeof(int)) {
         int temp_int;
         memcpy((uint8_t*)&temp_int, buf, sizeof(int));
         Serial.println(temp_int);
         }
         else  Serial.println(" NAN");
        }

Try and show the output

Note - I just editing the code, it is for server side

Server output : got reply from : 0x1, length = 1 : NAN

As I see, your sending code also incorrect.
Try to replace this line

to this:

if (manager.sendtoWait((uint8_t*) &data, sizeof(data), SERVER2_ADDRESS) == RH_ROUTER_ERROR_NONE)

Maybe you still have errors there.
See the library code, it says in the comments how to use the functions

Made it work following the instructions of the libraries . Thanx for your help

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