#include "LCD_Test.h"
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "";
const char password = "";
IPAddress server(, *, *, *);
// Constants for LCD
const UWORD Imagesize = LCD_1IN28_HEIGHT * LCD_1IN28_WIDTH * 2;
UWORD *BlackImage; //pointer to point to a dynamically allocated arraye for image data
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
WiFiClient espClient;
PubSubClient client(espClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
Serial.println("MQTT connected. IP: ");
if (client.connect("arduinoClient")) {
Serial.println("connected");
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// Timer variables
unsigned long previousMillis = 0;
const long interval = 200; // interval to collect data (milliseconds)
// Data collection variables
const int numReadings = 10; // Number of readings to collect
float accReadingsX[numReadings];
float accReadingsY[numReadings];
float accReadingsZ[numReadings];
float gyroReadingsX[numReadings];
float gyroReadingsY[numReadings];
float gyroReadingsZ[numReadings];
int readIndex = 0; // Index for storing readings
void setup() {
Serial.begin(115200);
//Serial.println("Starting setup...");
// PSRAM Initialize
if (psramInit()) {
Serial.println("PSRAM is correctly initialized");
} else {
Serial.println("PSRAM not available");
}
if ((BlackImage = (UWORD *)ps_malloc(Imagesize)) == NULL){ //dynamic mem allocation
Serial.println("Failed to allocate memory for BlackImage");
exit(0);
}
// Module Initialization
if (DEV_Module_Init() != 0){
Serial.println("GPIO Init Fail!");
} else {
Serial.println("GPIO Init successful!");
}
// Initialize and clear the LCD
LCD_1IN28_Init(HORIZONTAL);
LCD_1IN28_Clear(WHITE);
// Create a new image cache and fill it with white
Paint_NewImage((UBYTE *)BlackImage, LCD_1IN28.WIDTH, LCD_1IN28.HEIGHT, 0, WHITE);
Paint_SetScale(65);
Paint_SetRotate(ROTATE_0);
Paint_Clear(WHITE);
// Display "Connecting to WiFi..." on the screen
//Paint_DrawString_EN(50, 120, "Connecting to WiFi...", &Font20, BLACK, WHITE);
//LCD_1IN28_Display(BlackImage);
Serial.println("LCD Initialized and message displayed.");
QMI8658_init();
Serial.println("QMI8658_init\r\n");
Serial.println(server);
//print(server)
client.setServer(server, 1883);
client.setCallback(callback);
// Set the buffer size to handle larger payloads
client.setBufferSize(8192);
Serial.print("Connecting to: ");
Serial.println(ssid);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected. IP: ");
Serial.println(WiFi.localIP());
delay(1500);
// Clear the screen and display WiFi connected message
Paint_Clear(WHITE);
Paint_DrawString_EN(50, 120, "WiFi connected", &Font20, BLACK, WHITE);
LCD_1IN28_Display(BlackImage);
Serial.println("WiFi connected and message displayed.");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (!client.connected()) {
reconnect();
}
// Collect sensor data
float acc[3], gyro[3];
QMI8658_read_xyz(acc, gyro, NULL);
// Store the readings
if (readIndex < numReadings) {
accReadingsX[readIndex] = acc[0];
accReadingsY[readIndex] = acc[1];
accReadingsZ[readIndex] = acc[2];
gyroReadingsX[readIndex] = gyro[0];
gyroReadingsY[readIndex] = gyro[1];
gyroReadingsZ[readIndex] = gyro[2];
readIndex++;
}
// If enough readings are collected, create and send the JSON message
if (readIndex >= numReadings) {
char msg[1024]; // Adjust buffer size as needed
snprintf(msg, sizeof(msg), "{\"accelerometer\":{\"accX\":[");
for (int i = 0; i < readIndex; i++) {
char temp[16]; // Buffer to store each reading as a string
snprintf(temp, sizeof(temp), "%.2f", accReadingsX[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "],\"accY\":[", sizeof(msg) - strlen(msg) - 1);
for (int i = 0; i < readIndex; i++) {
char temp[16];
snprintf(temp, sizeof(temp), "%.2f", accReadingsY[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "],\"accZ\":[", sizeof(msg) - strlen(msg) - 1);
for (int i = 0; i < readIndex; i++) {
char temp[16];
snprintf(temp, sizeof(temp), "%.2f", accReadingsZ[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "]},\"gyroscope\":{\"gyrX\":[", sizeof(msg) - strlen(msg) - 1);
for (int i = 0; i < readIndex; i++) {
char temp[16];
snprintf(temp, sizeof(temp), "%.2f", gyroReadingsX[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "],\"gyrY\":[", sizeof(msg) - strlen(msg) - 1);
for (int i = 0; i < readIndex; i++) {
char temp[16];
snprintf(temp, sizeof(temp), "%.2f", gyroReadingsY[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "],\"gyrZ\":[", sizeof(msg) - strlen(msg) - 1);
for (int i = 0; i < readIndex; i++) {
char temp[16];
snprintf(temp, sizeof(temp), "%.2f", gyroReadingsZ[i]);
strncat(msg, temp, sizeof(msg) - strlen(msg) - 1);
if (i < readIndex - 1) strncat(msg, ",", sizeof(msg) - strlen(msg) - 1);
}
strncat(msg, "]}}", sizeof(msg) - strlen(msg) - 1);
Serial.print("sensorData: ");
Serial.println(msg);
bool success = false;
for (int attempt = 0; attempt < 3; attempt++) { // Try publishing up to 3 times
if (client.publish("IoTPRISMLAB_sensorData", msg)) {
Serial.println("Data sent to MQTT");
//Serial.println("MQTT connected. IP: ");
success = true;
break;
} else {
Serial.println("Failed to send data to MQTT");
Serial.print("MQTT state: ");
Serial.println(client.state());
delay(1000); // Wait 1 second before retrying
}
}
if (!success) {
Serial.println("All attempts to send data to MQTT failed.");
}
readIndex = 0; // Reset read index for the next batch of readings
}
client.loop();
}
}
I want to measure acc and gyro on esp32/s3 and turn on the lcd for data aquisition and transfer the data via wifi but i receive the error :Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.