From 6a6d77d1856a96b15a1c69b030403d80246951cc Mon Sep 17 00:00:00 2001 From: Gabriel Lima Date: Sun, 30 Oct 2022 20:44:25 -0400 Subject: [PATCH] i2c hello world --- ESP8266/.vscode/settings.json | 16 +++++++++++++++- ESP8266/src/main.cpp | 19 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ESP8266/.vscode/settings.json b/ESP8266/.vscode/settings.json index 5c430fc..a11037c 100644 --- a/ESP8266/.vscode/settings.json +++ b/ESP8266/.vscode/settings.json @@ -1,4 +1,18 @@ { "C_Cpp.clang_format_fallbackStyle": "Visual Studio", - "C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4}" + "C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4}", + "files.associations": { + "array": "cpp", + "*.tcc": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "memory": "cpp", + "random": "cpp", + "initializer_list": "cpp", + "ranges": "cpp" + } } \ No newline at end of file diff --git a/ESP8266/src/main.cpp b/ESP8266/src/main.cpp index 1f78c43..0c4cc67 100644 --- a/ESP8266/src/main.cpp +++ b/ESP8266/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -36,6 +37,10 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) { lcd.clear(); lcd.setCursor(0, 0); lcd.printf("%s", message); + } else if (!strcmp(topic, ROOT_TOPIC "/i2c/set")) { + Wire.beginTransmission(0x3F); + Wire.printf("%s", message); + Wire.endTransmission(); } else { // If topic is not recognized, exit without publishing to /get Serial.printf("Unrecognized topic %s\n", topic); @@ -109,6 +114,7 @@ void connectEthernet() { SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); SPI.setFrequency(4000000); + uint8_t macaddr[6]; int present = ethernet.begin(WiFi.macAddress(macaddr)); if (!present) { @@ -132,6 +138,7 @@ void connectEthernet() { } void setup() { + delay(5000); pinMode(LED_BUILTIN, OUTPUT); Serial.begin(921600); Serial.println(); @@ -185,4 +192,14 @@ void setup() { Serial.println(settopic); } -void loop() { client.loop(); } \ No newline at end of file +uint8_t sensorData[4]; + +void loop() { + client.loop(); + Wire.requestFrom(0x3F, 4); + size_t bytesRead = Wire.readBytes(sensorData, 4); + if(bytesRead){ + client.publish(ROOT_TOPIC "/sensor/get", sensorData, bytesRead, false); + } + delay(100); +} \ No newline at end of file