- Notifications
You must be signed in to change notification settings - Fork 22
Description
Hardware:
Board: DOIT Esp32 DevKit v1
Core Installation/update date: last release
IDE name: Platform.io
Flash Frequency: 40Mhz
Upload Speed: 115200
Description:
I'm working on a project to upload on a Google Sheet accelerations read by the MPU6050, connected with wifi. There are 2 taks, one to read accelerations (readData) and one to upload them using HTTP (postRequest). The problem is: everything works, until when this error occurs! At that point the accelerometer doesn't show up anymore even running a simple i2c scanner sketch. Dead. If I change accelerometer (did it 3 times), everything works again... This for 10mins, or 1 day maybe, but sooner or later the error returns. I attach also the hardware layout I am using. Am I burning accelerometers? Is it just a code problem? pls save me
Sketch:
#include <Arduino.h> #include "I2Cdev.h" #include "MPU6050.h" #include "Wire.h" #include "WiFi.h" #include "HTTPClient.h" #include "freertos/FreeRTOS.h" #include <string.h> #define CORE_DEBUG_LEVEL (5) //FROM 1 TO 5 #define READ_DELAY 20 //20 ms means 50hz #define POST_DELAY 10000 #define SEND_POST 1 MPU6050 accel(0x69); // SSID and Password static const char* ssid = "G"; static const char* password = "f1"; static const char* url = "https://hooks.zapier.com/hooks"; //data String x_buf, y_buf, z_buf; bool read_en; //rtos static TaskHandle_t xTaskPost = NULL, xTaskData = NULL; static SemaphoreHandle_t xMutex; // Establish a Wi-Fi connection with your router void initWifi() { Serial.print("Connecting to: "); Serial.print(ssid); WiFi.begin(ssid, password); int timeout = 10 * 4; // 10 seconds while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) { delay(250); Serial.print("."); } Serial.println(""); if(WiFi.status() != WL_CONNECTED) { Serial.println("Failed to connect to WIFI"); while(1); } Serial.print("WiFi connected in: "); Serial.print(millis()); Serial.print(", IP address: "); Serial.println(WiFi.localIP()); } void initMPU(){ Serial.print("Waiting MPU....."); while (!accel.testConnection()){ Serial.print(accel.getDeviceID()); Serial.print("."); delay(300); } Serial.println("."); Serial.println("MPU6050 connection successful"); accel.initialize(); delay(1000); // set offsets accel.calibrate(-5109, -5497, 9133, 38, -152, 29); //low pass filter Serial.println("Low-pass filter at 42Hz"); accel.setDLPFMode(MPU6050_DLPF_BW_42); } // Make an HTTP request to the web service void postRequest(void * pvParameters ) { String jsonObject; while(1){ while(xSemaphoreTake(xMutex, (TickType_t) 1) != pdTRUE); if (read_en && xPortGetFreeHeapSize()>150000){ //if not reading, or not enough space, do not concatenate anymore Serial.println(String("Heap size free: ") + xPortGetFreeHeapSize()); jsonObject.concat(String("{\"value1\":\"") + x_buf); x_buf = (char*) NULL; //invalidate jsonObject.concat(String("\",\"value2\":\"") + y_buf); y_buf = (char*) NULL; //invalidate jsonObject.concat(String("\",\"value3\":\"") + z_buf + "\"}\n"); z_buf = (char*) NULL; //invalidate } else{ Serial.println("Not enough space, waiting to POST successfully..."); Serial.println(String("Heap size: ") + xPortGetFreeHeapSize()); Serial.println(String("Reading accelerations: ") + (read_en?"enabled":"disabled")); } xSemaphoreGive(xMutex); Serial.println(String("Connecting to ") + url); HTTPClient http; http.begin(url); //http.addHeader("authorization: Bearer ", auth_token); http.addHeader("content-type:", "application/json"); #if SEND_POST int httpResponseCode = http.POST((uint8_t *)jsonObject.c_str(),jsonObject.length()); #else int httpResponseCode = 0; Serial.println(jsonObject); #endif //Serial.println(jsonObject); if(httpResponseCode > 0){ String response = http.getString(); //Get the response to the request Serial.println(httpResponseCode); //Print return code Serial.println(response); //Print request answer jsonObject = (char*) NULL; while(xSemaphoreTake(xMutex, (TickType_t) 1) != pdTRUE); if (!read_en){ read_en = true; Serial.println("Read now enabled again"); } xSemaphoreGive(xMutex); Serial.println("json empty!"); Serial.println(String("Heap size: ") + xPortGetFreeHeapSize()); } else{ Serial.print("Error on sending POST: "); Serial.println(httpResponseCode); } http.end(); //Free resources vTaskDelay(POST_DELAY); } } void readData(void * pvParameters ) { for(;;){ int16_t ax, ay, az; accel.getAcceleration(&ax, &ay, &az); while (xSemaphoreTake(xMutex, (TickType_t) 1) != pdTRUE); if (read_en){ bool read_x = x_buf.concat(ax + String(" ")); bool read_y = y_buf.concat(ay + String(" ")); bool read_z = z_buf.concat(az + String(" ")); read_en = read_x && read_y && read_z; if (!read_en){ Serial.println("READ NOW DISABLED"); } } xSemaphoreGive(xMutex); vTaskDelay(READ_DELAY); } } void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) Wire.begin(); delay(1000); // initialize serial communication Serial.begin(115200); //mpu initMPU(); //wifi delay(4000); initWifi(); read_en = true; xMutex = xSemaphoreCreateMutex(); xTaskCreate( postRequest, /* Function that implements the task. */ "PostReqTask", /* Text name for the task. */ 4096, /* Stack size in words, not bytes. */ ( void * ) 1, /* Parameter passed into the task. */ tskIDLE_PRIORITY,/* Priority at which the task is created. */ &xTaskPost ); /* Used to pass out the created task's handle. */ xTaskCreate( readData, /* Function that implements the task. */ "ReadDataTask", /* Text name for the task. */ 4096, /* Stack size in words, not bytes. */ ( void * ) 1, /* Parameter passed into the task. */ tskIDLE_PRIORITY,/* Priority at which the task is created. */ &xTaskData ); /* Used to pass out the created task's handle. */ } void loop(){}
Debug Messages:
[E][esp32-hal-i2c.c:1426] i2cCheckLineState(): Bus Invalid State, TwoWire() Can't init sda=1, scl=0 Waiting MPU.....0.0.0.0.0.0.