Hi @jyijyi
sample code below
#include <mosquitto.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <json-c/json.h>
#include <modbus.h>
#include <errno.h>
#include <modbus.h>
#include <unistd.h>
void on_connect(struct mosquitto *mosq, void *obj, int reason_code)
{
printf("on_connect: %s\n", mosquitto_connack_string(reason_code));
if(reason_code != 0){
mosquitto_disconnect(mosq);
}
}
void on_publish(struct mosquitto *mosq, void *obj, int mid)
{
printf(“Message with mid %d has been published.\n”, mid);
}
void publish_sensor_data(struct mosquitto *mosq)
{
modbus_t *ctx;
uint16_t data[113]; // buffer to hold the data read
int rc, i;
uint32_t sec_to = 10;
uint32_t usec_to = 0;
char key[114];
char value[114];
int nb_points = 1;
const int slave_address = 10; // the slave address of the Modbus device
// create a new context for the Modbus communication
ctx = modbus_new_rtu(“/dev/ttyHS1”, 9600, ‘E’, 8, 1);
modbus_get_response_timeout(ctx, &sec_to, &usec_to);
if (ctx == NULL) {
fprintf(stderr, “Error creating Modbus context\n”);
//return 1;
}
// set the slave address of the device we want to communicate with
modbus_set_slave(ctx, slave_address);
// connect to the Modbus device
if (modbus_connect(ctx) == -1) {
fprintf(stderr, “Error connecting to Modbus device: %s\n”, modbus_strerror(errno));
modbus_free(ctx);
//return 1;
}
// read 10 holding registers starting at address 0
rc = modbus_read_input_registers(ctx, 0, 113, data);
if (rc == -1) {
fprintf(stderr, “Error reading Modbus data: %s\n”, modbus_strerror(errno));
modbus_close(ctx);
modbus_free(ctx);
//return 1;
}
json_object *my_object = json_object_new_object();
if (my_object == NULL) {
printf("Error creating JSON object!\n");
//return EXIT_FAILURE;
}
// print the data that was read
for (i = 0; i < 114; i++) {
printf(“Data[%d]:%X\n”, i+1, (data[i]));
sprintf(key, “Reg_%d”, i+1);
sprintf(value, “%x”, (data[i]));
// cJSON_AddItemToObject(json, key, cJSON_CreateString(value));
json_object_object_add(my_object, key, json_object_new_string(value));
}
const char *json_string = json_object_to_json_string(my_object);
printf(“JSON: %s\n”, json_string);
// disconnect from the Modbus device and clean up
rc = mosquitto_publish(mosq, NULL, "newtopic", strlen(json_string), json_string, 0, false);
if(rc != MOSQ_ERR_SUCCESS){
fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(rc));
}
modbus_close(ctx);
modbus_free(ctx);
sleep(3);
}
int main()
{
struct mosquitto *mosq;
int rc;
mosquitto_lib_init();
mosq = mosquitto_new("TestClient", true, NULL);
if(mosq == NULL){
fprintf(stderr, "Error: Out of memory.\n");
return 1;
}
mosquitto_tls_set(mosq, "./AmazonRootCA1.pem", NULL, "./certificate.pem.crt", "./private.pem.key", NULL);
/* Configure callbacks. This should be done before connecting ideally. */
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_publish_callback_set(mosq, on_publish);
rc = mosquitto_connect_async(mosq, "aws-endpoint", 8883, 60);
if(rc != MOSQ_ERR_SUCCESS){
mosquitto_destroy(mosq);
fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
return 1;
}
rc = mosquitto_loop_start(mosq);
if(rc != MOSQ_ERR_SUCCESS){
mosquitto_destroy(mosq);
fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
return 1;
}
while(1){
publish_sensor_data(mosq);
}
mosquitto_lib_cleanup();
return 0;
}
cross compile commands below
-
source source /opt/swi/SWI9X07Y_02.37.07.00/environment-setup-armv7a-neon-poky-linux-gnueabi
-
$CC modbus_mqtt8.c -o out1.o -lmodbus -lmosquitto -ljson-c
please note inside gateway libjson-c.so.3 libjson-c.so.3.0.1 these are shared libraries available
So I installed same x86_64 version on my system also and gcc compile is successful (modbus_mqtt8.c -o out1.o -lmodbus -lmosquitto -ljson-c) i’m getting executable successfully
please note: aws-end point is there which i removed