1 Answer
- Newest
- Most votes
- Most comments
0
Hi Obinex. You can use the functions in the BSP to access the sensor data. Then construct a JSON document to publish.
The demo you've been using publishes a plaintext message here: https://github.com/aws/amazon-freertos/blob/main/demos/coreMQTT/mqtt_demo_mutual_auth.c#L981-L993
You can find the BSP drivers for the various sensors here: https://github.com/aws/amazon-freertos/tree/main/vendors/st/stm32l475_discovery/BSP/B-L475E-IOT01
So you want some code like this to create a JSON document instead of a plaintext string:
int16_t accel[3], magneto[3];
float gyro[3];
BSP_ACCELERO_AccGetXYZ(accel);
BSP_GYRO_GetXYZ(gyro);
BSP_MAGNETO_GetXYZ(magneto);
++publishCount;
snprintf(publishPayload, sizeof(publishPayload), "{\"sequence\":%ld"
",\"payload\": {"
"\"temperature\":%.2f"
",\"humidity\":%.2f"
",\"pressure\":%.2f"
",\"accelerometer\":{\"x\":%d,\"y\":%d,\"z\":%d}"
",\"gyroscope\":{\"x\":%.2f,\"y\":%.2f,\"z\":%.2f}"
",\"magnetometer\":{\"x\":%d,\"y\":%d,\"z\":%d}"
"}}",
publishCount,
BSP_TSENSOR_ReadTemp(),
BSP_HSENSOR_ReadHumidity(),
BSP_PSENSOR_ReadPressure(),
accel[0], accel[1], accel[2],
gyro[0], gyro[1], gyro[2],
magneto[0], magneto[1], magneto[2]
);
If you are asking how to display it in the cloud, I suggest you read the following blog. Pattern 4 is one of the simplest ways to get started.
Relevant content
- asked 2 years ago
- asked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 2 years ago
In the code above, what file would I put that code in?
Well the demos are just demos. You could certainly modify the MQTT mutual auth demo to publish that instead of "Hello World!". That's only an example snippet though to illustrate the concept.
I went to the link but I'm still not sure what to do, by the way this project is for my Final Year Project 2 for university.
The mutual auth demo sends the string defined by
mqttexampleMESSAGE
. At the most basic level you would replace that string with the JSON document created by the quoted fragment.Ok, but from the code provided above where would I find "publishPayload" and "publishCount" those variables are not initialized. Also does that code need to be put in a function so the function can then be called by mqttexampleMESSAGE?