Question/Issue:
Hello, does anyone know how to convert this micropython code into c/c++ pico-sdk using i2c, I need this for my AI project with the edge impulse c++ sdk, please help. The Raspberry Pi pico as the master and NodeMCU esp8266 as the slave.
Micropython code (working):
#Code for Raspberry Pi Pico
import utime
import machine
from machine import I2C
I2C_ADDR = 0x05 #I2C Address of NodeMCU
data = "BinFull" #Data to send to I2C device
def main():
print("Initalizing I2C as Master")
i2c = I2C(0, sda=machine.Pin(16), scl=machine.Pin(17), freq=10000)
print(i2c)
while True:
i2c.writeto(I2C_ADDR, data)
a = i2c.readfrom(I2C_ADDR, 8)
print(a.decode('utf-8'))
utime.sleep_ms(1000)
main()
C++ code using pico-sdk (not working):
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#define I2C_PORT i2c0
static int peripheral_address = 0x05;
int main()
{
char data[] = "BinFull";
char data_to_receive[] = "";
stdio_init_all();
i2c_init(I2C_PORT, 10000);
gpio_set_function(8, GPIO_FUNC_I2C);
gpio_set_function(9, GPIO_FUNC_I2C);
gpio_pull_up(8);
gpio_pull_up(9);
while (true)
{
i2c_write_blocking(I2C_PORT, peripheral_address, (uint8_t *) data, 7, true);
i2c_read_blocking(I2C_PORT, peripheral_address, (uint8_t *) data_to_receive, 8, false);
for (int i = 0; i < sizeof(data_to_receive); ++i)
{
printf("%c", data_to_receive[i]);
}
sleep_ms(1000);
}
return 0;
}