Esp32 is not able to get data from I2C and send this data to aws iot hub at the same time

I've been working on a project in which i need a gateway. So to emulate the functionalities of a gateway, I have decided to use esp32. I have been using esp32c3 mini by xiao studio for my smart sensors to transmit data using bluetooth. My gateway comprises of two esp32 devices connected to each other through I2C. So one of these esp32 devices receives the data using BLE and sends this data to the next esp32 using I2C

The second esp32 receives the data through I2C and transmits the data to aws

But i noticed how when the aws connection is established, the sensor data are not read and vice versa. What do i do? I'm assuming i'm going to face the same problem for sending the messages received by ble using I2C as well.

I really could use your help on this

#include <Wire.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "secrets.h" 

#define RECEIVER_ADDRESS 0x42
#define MAX_JSON_LENGTH 256

#define AWS_IOT_PUBLISH_TOPIC   "sensordatapub"
#define AWS_IOT_SUBSCRIBE_TOPIC "sensordatasub"

WiFiClientSecure net;
MQTTClient client;

char receivedData[MAX_JSON_LENGTH];
bool newData = false;

void i2cTask(void *pvParameters);
void awsTask(void *pvParameters);

void setup() {
  Serial.begin(9600);
  Wire.begin(RECEIVER_ADDRESS);

  xTaskCreatePinnedToCore(i2cTask, "i2cTask", 4096, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(awsTask, "awsTask", 8192, NULL, 1, NULL, 1);
}

void loop() {}

void i2cTask(void *pvParameters) {
  while (true) {
    int byteCount = Wire.available();
    if (byteCount > 0) {
      int bytesRead = Wire.readBytes(receivedData, MAX_JSON_LENGTH);
      receivedData[bytesRead] = '\0';
      newData = true;
      Serial.println("Received data from I2C:");
      Serial.println(receivedData);
    }
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}

void awsTask(void *pvParameters) {
  connectAWS();

  while (true) {
    if (newData) {
      processData();
      newData = false;
      Serial.println("Data processed and published to AWS.");
    }
    client.loop();
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }
}

void connectAWS() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.println("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Wi-Fi connected.");

  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  client.begin(AWS_IOT_ENDPOINT, 8883, net);
  client.onMessage(messageHandler);

  Serial.print("Connecting to AWS IoT... ");
  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if (!client.connected()) {
    Serial.println("\nFailed to connect to AWS IoT!");
    return;
  }

  client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
  Serial.println("\nAWS IoT Connected!");
}

void publishMessage(const char* id, int max_lvl, int min_lvl, int data) {
  StaticJsonDocument<200> doc;
  doc["id"] = id;
  doc["max_lvl"] = max_lvl;
  doc["min_lvl"] = min_lvl;
  doc["data"] = data;

  char jsonBuffer[512];
  serializeJson(doc, jsonBuffer);

  Serial.print("Publishing JSON data to AWS IoT: ");
  Serial.println(jsonBuffer);

  client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
}

void processData() {
  StaticJsonDocument<200> doc;
  DeserializationError error = deserializeJson(doc, receivedData);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    return;
  }

  const char* id = doc["id"];
  int max_lvl = doc["max_lvl"];
  int min_lvl = doc["min_lvl"];
  int data = doc["data"];

  publishMessage(id, max_lvl, min_lvl, data);
}

void messageHandler(String &topic, String &payload) {
  Serial.println("Incoming message on topic: " + topic + ", payload: " + payload);
}


I created two tasks here. I wanted to use both the cores

This is something that is addressed here but I can't seem to find the solution

This is the architecture

Welcome to the Forum! Read the forum guidelines to see how to properly ask a question and some good information on making a good post. You will get faster and better help if you post all your code as requested by the forum guidelines.
Please go back and fix your original post.
Also please describe what the hardware do you use.
Photos that clearly show your wiring can also be very helpful.

Welcome to the forum.

I have no solution, no advice, and not much to say. Sorry. But I do have a few notes:

Can you call them either ESP32 or ESP32-C3 ?
Espressif made them look and feel the same, but they are internally totally different.

The ESP32 works more or less as a I2C Slave. However, I strongly advice against it. I don't know how reliable the ESP32-C3 is as a I2C Slave.

The ESP32 has a spare UART port: Serial2. I don't know about the ESP32-C3.

When using the Seeed Studio XIAO ESP32-C3, then I have doubts about how updated the software is. Can you use the build environment by Espressif ?


Tips:
The Arduino Uno is still the easiest and most reliable to use. It took many years and even small bugs have been fixed. It is the most reliable option as a I2C Slave.
The ESP32 is also reliable, but be sure to use the newest Arduino IDE with the board definition by Espressif (the maker of the ESP32).
If you use the ESP32-C3, then there might be bugs or libraries that are not compatible. You get the least trouble with the newest Arduino IDE and the board definition by Espressif.
The Raspberry Pi Pico is cheap and Arduino runs on top of Mbed, which is also reliable.

As you don't seem to be using the Arduino Nano ESP32, I've moved your topic.

Thank you! And I'll work on this

Thank you

I'm actually using the esp32 board for I2C and not the xiao esp32-c3 mini. The esp32c3 is just to send data using ble to esp32.

The problem with WiFi and ECC and I2C might not be a problem. Perhaps it was a problem and has been solved. Perhaps it has nothing to do with your ESP32.

Can you do your project in a different way ? Without a ESP32 as a I2C Slave.
I'm sorry to say, but you are nowhere near a working ESP32 as a I2C Slave. You don't even know in how much trouble you are :scream:
Projects fail that use the I2C bus between processors. It is not that kind of bus.

Please don't use both cores. You will surely get into trouble if you do.
Espressif runs WiFi and all of those things on Core0 and Arduino runs on Core1. Keep it that way.

I'll give it a go and let you know how it goes

can i use SPI or UART between the microcontrollers instead of an i2c bus

The ESP32 has a spare hardware UART port: "Serial2".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.