I'm encountering out-of-memory errors when attempting to publish MQTT messages in my application.
the application reads the temperature data from the onboard temperature sensor and then I format the temperature data into a JSON string representing an MQTT payload. The temperature value is rounded to one decimal place.
but when I try to publish the number, it prints out publish err: -1.
and I found online that its an "out of memory error"
these is my main.c code:
int main(){
stdio_init_all();
cyw43_arch_init();
cyw43_arch_enable_sta_mode();
//attempt to connect to wifi
// Configure ADC
adc_init();
adc_set_temp_sensor_enabled(true);
adc_select_input(4);
while(cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID,WIFI_PASSWORD,CYW43_AUTH_WPA2_AES_PSK,30000))
{
printf("attempting to connect...... \n");
}
mqtt_client_t *client = mqtt_client_new();
printf("connected......\n");
if (client != NULL) {
mqtt_do_connect(client);
}
//initialise server
while(1){
uint16_t raw = adc_read();
const float conversion_factor = 3.3f / (1<<12);
float result = raw * conversion_factor;
float temp = 27 - (result -0.706)/0.001721;
temp=roundf(temp*100)/100;
printf("Temp = %f C\n", temp);
char payload_string[52];
sprintf(payload_string, "{\"temperature\": \"%.1f\"}", temp);
mqtt_do_publish(client, "pico/temp", payload_string, 0);
sleep_ms(5000);
}
}
and this is my my_mqtt.c
void mqtt_do_connect(mqtt_client_t *client) {
struct mqtt_connect_client_info_t ci;
err_t err;
// Set up an empty client info structure
memset(&ci, 0, sizeof(ci));
// Minimal amount of information required is client identifier, so set it here
ci.client_id = "pico";
// Convert broker IP string to ipv4
ip_addr_t mqtt_ip;
ipaddr_aton(MQTT_IP, &mqtt_ip);
/*
* Initiate client and connect to server, if this fails immediately an error code is returned otherwise
* mqtt_connection_cb will be called with connection result after attempting to establish a connection with
* the server.
*/
err = mqtt_client_connect(client, &mqtt_ip, MQTT_PORT, mqtt_connection_cb, 0, &ci);
if (err != ERR_OK) {
printf("mqtt_connect return %d\n", err);
}
}
static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) {
if (status == MQTT_CONNECT_ACCEPTED) {
printf("mqtt_connection_cb: Successfully connected\n");
} else {
printf("mqtt_connection_cb: Disconnected, reason: %d\n", status);
// Attempt to reconnect when connection fails
mqtt_do_connect(client);
}
}
void mqtt_do_publish(mqtt_client_t *client, char *topic, char *payload, void *arg) {
err_t err;
u8_t qos = 1; // 0 1 or 2, see MQTT specification
u8_t retain = 0; // Don't want to retain payload
err = mqtt_publish(client, topic, payload, strlen(payload), qos, retain, mqtt_pub_request_cb, arg);
if (err != ERR_OK) {
printf("Publish err: %d\n", err);
}
}
// Called when publish is complete either with success or failure
static void mqtt_pub_request_cb(void *arg, err_t result) {
if (result != ERR_OK) {
printf("Publish result: %d\n", result);
}
}
Im currently working on a ubuntu virtual machine. Can you please help me and thank you.
And i also have another question What is the recommended approach for determining the MQTT broker’s IP address? I’ve currently set it as the IPv4 address of my laptop. Is this the appropriate configuration? Im using mosquitto
I'm encountering out-of-memory errors when attempting to publish MQTT messages in my application. the application reads the temperature data from the onboard temperature sensor and then I format the temperature data into a JSON string representing an MQTT payload. The temperature value is rounded to one decimal place. but when I try to publish the number, it prints out publish err: -1. and I found online that its an "out of memory error" these is my main.c code:
and this is my my_mqtt.c
Im currently working on a ubuntu virtual machine. Can you please help me and thank you. And i also have another question What is the recommended approach for determining the MQTT broker’s IP address? I’ve currently set it as the IPv4 address of my laptop. Is this the appropriate configuration? Im using mosquitto