我试图让我的ESP8266连接并通过i2c总线发送消息。我正在使用NodeMcu开发板。引脚D1,D2和GND相互连接。
我主人的代码是:
#include <Wire.h>
void setup() {
Wire.begin(D1,D2); // join i2c bus (address optional for master)
Serial.begin(115200);
}
byte x = 0;
void loop() {
Wire.beginTransmission(8);
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
Serial.println("Transmitted");
x++;
delay(500);
}
我的奴隶ESP上的代码是:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
Serial.println("Received..");
/*
while (1 < Wire.available()) { // loop through all but the last
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
}
运行此功能不会在接收器芯片上输出。
我不确定,但我希望Arduino的Wire库能够将硬件I2C控制器用于ATMega。 Espressif固件中的I2C驱动程序似乎是通过GPIO进行I2C,这暗示ESP上没有hw控制器(无论如何它们的概率是多少)。所以你需要使用除Wire.h之外的其他东西,因此我建议 - 尝试下载一些东西,它可以通过GPIO为你的Arduino IDE下载I2C。像this ..也许,我没有尝试过。我知道不是一个完整的解决方案,但也许至少这有助于...祝你好运!
ESP8266(I2C主设备)到ESP8266(I2C从设备)的工作原理是2.5.0版本。看看我的评论on the ESP8266 GitHub