i2c hello world
This commit is contained in:
16
ESP8266/.vscode/settings.json
vendored
16
ESP8266/.vscode/settings.json
vendored
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <LiquidCrystal_I2C.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#include <credentials.hpp>
|
||||
|
||||
@@ -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(); }
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user