I'm trying to I2C connect a ESP32 Huzzah32 to a Bela CTAG Face. The problem is that I can't find the address name from the ESP32 when I scan on the Bela. I use: i2cdetect -y -r 1 (also nothing on ch. 2). I have connect SCL and SDA to P9.24 and P9.26 on the Bela and used pullup 10k resistors on each SDA and SCL with 3.3v + common GND wire between Bela and ESP32, so I think the connections should be right.
Sources:
render.cpp
_main.pd
ESP32I2c.cpp
ESP32I2c.cpp looks like this:
/***** esp32I2c.cpp *****/
#include "esp32I2c.h"
namespace esp32
{
I2C::I2C() {
// Default do nothing so far
};
bool I2C::begin(uint8_t bus, uint8_t addr) {
m_bus = bus;
m_addr = addr;
if (initI2C_RW(bus, addr, 0) > 0)
return false;
return true;
}
sensorData_t I2C::readData() {
sensorData_t retData;
auto res = read8bit(ROLL1_REG);
rt_printf("Reading res: %d\n", res);
return retData;
};
uint8_t I2C::read8bit(uint8_t reg) {
i2c_char_t inbuf, outbuf;
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
outbuf = reg;
messages[0].addr = m_addr;
messages[0].flags = 0;
messages[0].len = sizeof(outbuf);
messages[0].buf = &outbuf;
/* The data will get returned in this structure */
messages[1].addr = m_addr;
messages[1].flags = I2C_M_RD /* | I2C_M_NOSTART*/;
messages[1].len = sizeof(inbuf);
messages[1].buf = &inbuf;
/* Send the request to the kernel and get the result back */
packets.msgs = messages;
packets.nmsgs = 2;
if (ioctl(i2C_file, I2C_RDWR, &packets) < 0)
{
rt_printf("Unable to send data\n");
return 0;
}
return inbuf;
}
}
This is the ESP32 code:
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#define I2C_ADDR 0x20
// Defining a datatype for storing the sensor data
typedef struct sensorData_t {
int roll1, pitch1, roll2, pitch2;
} sensorData_t;
// Global data for easy access #thenoobway
sensorData_t sensorData;
void printSensorData() {
char msg[50];
sprintf(msg, "Sensor1 (%d,%d)\nSensor2 (%d,%d)", sensorData.roll1, sensorData.pitch1, sensorData.roll2, sensorData.pitch2);
Serial.println(msg);
}
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&sensorData, incomingData, sizeof(sensorData));
Serial.print("Bytes received: ");
Serial.println(len);
printSensorData();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Setup I2C bus
Wire.begin(I2C_ADDR);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}