2018-12-01 09:46:37 +01:00
|
|
|
Custom I²C Device
|
|
|
|
=================
|
|
|
|
|
2020-07-11 23:32:45 +02:00
|
|
|
Lots of devices communicate using the I²C protocol. If you want to integrate
|
2019-02-16 23:25:23 +01:00
|
|
|
a device into ESPHome that uses this protocol you can pretty much use almost
|
|
|
|
all Arduino-based code because the ``Wire`` library is also available in ESPHome.
|
2018-12-01 09:46:37 +01:00
|
|
|
|
|
|
|
See the other custom component guides for how to register components and make
|
|
|
|
them publish values.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
#include "esphome.h"
|
2018-12-01 09:46:37 +01:00
|
|
|
|
|
|
|
class MyCustomComponent : public Component {
|
|
|
|
public:
|
|
|
|
void setup() override {
|
|
|
|
// Initialize the device here. Usually Wire.begin() will be called in here,
|
|
|
|
// though that call is unnecessary if you have an 'i2c:' entry in your config
|
|
|
|
|
|
|
|
Wire.begin();
|
|
|
|
}
|
|
|
|
void loop() override {
|
|
|
|
// Example: write the value 0x42 to register 0x78 of device with address 0x21
|
|
|
|
Wire.beginTransmission(0x21);
|
|
|
|
Wire.write(0x78);
|
|
|
|
Wire.write(0x42);
|
|
|
|
Wire.endTransmission();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|