I2C连接ESP32 Master和ESP8266从Platformio定制引脚

发布于 2025-02-03 23:56:46 字数 3245 浏览 3 评论 0原文

董事会:

ESP-WROOM-32 38PIN
ESP8266MOD

图表类似:

ESP32 G21 -> ESP8266 D2 (and 5 volts with 2k resistor from ESP8266)
ESP32 G22 -> ESP8266 D1 (and 5 volts with 2k resistor from ESP8266)
ESP32 GND -> ESP8266 G 

Platform.ini Client

[platformio]
default_envs = esp8266dev
data_dir = src/data

[env:esp8266dev]
platform = espressif8266
framework = arduino
board = nodemcuv2
board_build.mcu = esp8266
board_build.partitions = huge_app.csv
upload_protocol = esptool
lib_deps = 
    bblanchon/ArduinoJson @ ^6.19.4
upload_speed = 115200
monitor_speed = 9600
upload_port = COM14
monitor_port = COM14

platform.ini主人

[env:fm-devkit]
platform = espressif32
board = fm-devkit
framework = arduino
; upload_speed = 115200
; monitor_speed = 115200
monitor_speed = 9600
upload_port = COM12
monitor_port = COM12

,但是当它起作用时,我希望在ESP8266板上更改RX和TX的端口

#include <Arduino.h>
#include "Wire.h"

// GPIO21 SDA
#define SDA 33

// GPIO22 SLC
#define SCL 36

int x = 0x1234;

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

  // Wire.begin(SDA, SCL);
  Wire.begin();
  Wire.setClock(100000);

  // Wire.beginTransmission(0x21); // roll calling for the Slave
  // byte busStatus = Wire.endTransmission();
  // if (busStatus != 0x00)
  // {
  //   Serial.print("I2C Bus communication problem...!" + String(busStatus));
  //   while (1)
  //     ; // wait for ever
  // }
  // Serial.println("Slave found!");
}

void loop()
{
  Serial.print(".");
  // Wire.requestFrom(0x21, 1, true);
  // int a = Wire.read();
  // Serial.println("Send " + String(a));
  Wire.beginTransmission(0x21); // address byte in write mode=0100001+0=01000010= 0x42 (queued)
  // Wire.write(highByte(x));      // 0x12 is queued
  // Wire.write(lowByte(x));       // 0x34 is queued
  Wire.write("HOLA");
  Wire.endTransmission(); // all queued data bytes are sent on ACK
  //-------------------
  delay(1000); // test interval
}

,以及ESP8266的客户端代码:

#include <Arduino.h>
#include <ArduinoJson.h>
#include <Wire.h>

#include "functions.hpp"


void setup()
{
    Serial.begin(SPEED);

    pinMode(RELAY1, OUTPUT); // Mode OUTPUT
    pinMode(RELAY2, OUTPUT); // Mode OUTPUT
    // pinMode(TX, FUNCTION_3); // Mode GPIO
    // pinMode(RX, FUNCTION_3); // Mode GPIO

    Wire.begin(SDA_PIN,SCL_PIN, I2C_SLAVE_ADDR);
    // Wire.begin(RX, TX, I2C_SLAVE_ADDR);
    Wire.setClock(CLOCK);
    Wire.onRequest(eventoSolicitud); // registrar evento de solicitud de datos
    Wire.onReceive(eventoRecepcion); // registrar evento de recepcion de datos

    digitalWrite(RELAY2, HIGH);
}

void loop()
{

    delay((tBucle * 1000));

}

void eventoRecepcion(int howMany)
{

  Serial.print("datos i2c");

  while (1 < Wire.available()) // loop through all but the last byte
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);      // print the character
  }

  int x = Wire.read(); // receive byte as an integer
  Serial.println(x);   // print the integer
}

此代码可行,但从未获得数据。 如果用于TX和RX和Wire.Begin(RX,TX,I2C_SLAVE_ADDR)的无效销钉;董事会没有做出回应。

什么是黄?

我做了很多测试,但我无法正常工作

Boards:

ESP-WROOM-32 38PIN
ESP8266MOD

Diagram like:

ESP32 G21 -> ESP8266 D2 (and 5 volts with 2k resistor from ESP8266)
ESP32 G22 -> ESP8266 D1 (and 5 volts with 2k resistor from ESP8266)
ESP32 GND -> ESP8266 G 

Platformio.ini Client

[platformio]
default_envs = esp8266dev
data_dir = src/data

[env:esp8266dev]
platform = espressif8266
framework = arduino
board = nodemcuv2
board_build.mcu = esp8266
board_build.partitions = huge_app.csv
upload_protocol = esptool
lib_deps = 
    bblanchon/ArduinoJson @ ^6.19.4
upload_speed = 115200
monitor_speed = 9600
upload_port = COM14
monitor_port = COM14

Platformio.ini Master

[env:fm-devkit]
platform = espressif32
board = fm-devkit
framework = arduino
; upload_speed = 115200
; monitor_speed = 115200
monitor_speed = 9600
upload_port = COM12
monitor_port = COM12

But when it works I hope to change the ports for rx and tx on the esp8266 board

#include <Arduino.h>
#include "Wire.h"

// GPIO21 SDA
#define SDA 33

// GPIO22 SLC
#define SCL 36

int x = 0x1234;

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

  // Wire.begin(SDA, SCL);
  Wire.begin();
  Wire.setClock(100000);

  // Wire.beginTransmission(0x21); // roll calling for the Slave
  // byte busStatus = Wire.endTransmission();
  // if (busStatus != 0x00)
  // {
  //   Serial.print("I2C Bus communication problem...!" + String(busStatus));
  //   while (1)
  //     ; // wait for ever
  // }
  // Serial.println("Slave found!");
}

void loop()
{
  Serial.print(".");
  // Wire.requestFrom(0x21, 1, true);
  // int a = Wire.read();
  // Serial.println("Send " + String(a));
  Wire.beginTransmission(0x21); // address byte in write mode=0100001+0=01000010= 0x42 (queued)
  // Wire.write(highByte(x));      // 0x12 is queued
  // Wire.write(lowByte(x));       // 0x34 is queued
  Wire.write("HOLA");
  Wire.endTransmission(); // all queued data bytes are sent on ACK
  //-------------------
  delay(1000); // test interval
}

and this client code for esp8266:

#include <Arduino.h>
#include <ArduinoJson.h>
#include <Wire.h>

#include "functions.hpp"


void setup()
{
    Serial.begin(SPEED);

    pinMode(RELAY1, OUTPUT); // Mode OUTPUT
    pinMode(RELAY2, OUTPUT); // Mode OUTPUT
    // pinMode(TX, FUNCTION_3); // Mode GPIO
    // pinMode(RX, FUNCTION_3); // Mode GPIO

    Wire.begin(SDA_PIN,SCL_PIN, I2C_SLAVE_ADDR);
    // Wire.begin(RX, TX, I2C_SLAVE_ADDR);
    Wire.setClock(CLOCK);
    Wire.onRequest(eventoSolicitud); // registrar evento de solicitud de datos
    Wire.onReceive(eventoRecepcion); // registrar evento de recepcion de datos

    digitalWrite(RELAY2, HIGH);
}

void loop()
{

    delay((tBucle * 1000));

}

void eventoRecepcion(int howMany)
{

  Serial.print("datos i2c");

  while (1 < Wire.available()) // loop through all but the last byte
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);      // print the character
  }

  int x = Wire.read(); // receive byte as an integer
  Serial.println(x);   // print the integer
}

This code work but never get data.
If uncoment pinMode for tx and rx and Wire.begin(RX, TX, I2C_SLAVE_ADDR); board no make response.

What is wong?

I did a lot of tests but I can't get it to work

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文