diff --git a/src/buzz/buzz.cpp b/src/buzz/buzz.cpp index 059630bfe..f8314fe51 100644 --- a/src/buzz/buzz.cpp +++ b/src/buzz/buzz.cpp @@ -1,5 +1,6 @@ #include "buzz.h" #include "configuration.h" +#include "NodeDB.h" #ifndef PIN_BUZZER @@ -42,17 +43,19 @@ const int DURATION_1_8 = 125; // 1/8 note const int DURATION_1_4 = 250; // 1/4 note void playTones(const ToneDuration *tone_durations, int size) { - for (int i = 0; i < size; i++) { - const auto &tone_duration = tone_durations[i]; + if (config.network.eth_enabled != true) { + for (int i = 0; i < size; i++) { + const auto &tone_duration = tone_durations[i]; #ifdef M5STACK - Tone.tone(tone_duration.frequency_khz); - delay(tone_duration.duration_ms); - Tone.mute(); + Tone.tone(tone_duration.frequency_khz); + delay(tone_duration.duration_ms); + Tone.mute(); #else - tone(PIN_BUZZER, tone_duration.frequency_khz, tone_duration.duration_ms); + tone(PIN_BUZZER, tone_duration.frequency_khz, tone_duration.duration_ms); #endif - // to distinguish the notes, set a minimum time between them. - delay(1.3 * tone_duration.duration_ms); + // to distinguish the notes, set a minimum time between them. + delay(1.3 * tone_duration.duration_ms); + } } } diff --git a/src/main.cpp b/src/main.cpp index 6ecd71baa..a783f023b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,10 @@ #include "mqtt/MQTT.h" #endif +#if HAS_ETHERNET +#include "mqtt/MQTT.h" +#endif + #include "LLCC68Interface.h" #include "RF95Interface.h" #include "SX1262Interface.h" @@ -279,11 +283,12 @@ void setup() #ifdef ARCH_NRF52 nrf52Setup(); #endif - playStartMelody(); // We do this as early as possible because this loads preferences from flash // but we need to do this after main cpu iniot (esp32setup), because we need the random seed set nodeDB.init(); + playStartMelody(); + // Currently only the tbeam has a PMU power = new Power(); power->setStatusHandler(powerStatus); @@ -440,7 +445,7 @@ void setup() } #endif -#if HAS_WIFI +#if HAS_WIFI || HAS_ETHERNET mqttInit(); #endif diff --git a/src/mesh/eth/ethClient.cpp b/src/mesh/eth/ethClient.cpp index 92c1997e4..86e4e56c9 100644 --- a/src/mesh/eth/ethClient.cpp +++ b/src/mesh/eth/ethClient.cpp @@ -7,25 +7,30 @@ // Startup Ethernet bool initEthernet() { + + config.network.eth_enabled = true; + config.network.eth_mode = Config_NetworkConfig_EthMode_DHCP; + if (config.network.eth_enabled) { - Ethernet.init( SS ); +#ifdef PIN_ETHERNET_RESET + pinMode(PIN_ETHERNET_RESET, OUTPUT); + digitalWrite(PIN_ETHERNET_RESET, LOW); // Reset Time. + delay(100); + digitalWrite(PIN_ETHERNET_RESET, HIGH); // Reset Time. +#endif + + Ethernet.init( ETH_SPI_PORT, PIN_ETHERNET_SS ); uint8_t mac[6]; + int status = 0; + getMacAddr(mac); // FIXME use the BLE MAC for now... - - if (Ethernet.hardwareStatus() == EthernetNoHardware) { - DEBUG_MSG("Ethernet shield was not found.\n"); - } else if (Ethernet.linkStatus() == LinkOFF) { - DEBUG_MSG("Ethernet cable is not connected.\n"); - } else if (config.network.eth_mode == Config_NetworkConfig_EthMode_DHCP) { + + if (config.network.eth_mode == Config_NetworkConfig_EthMode_DHCP) { DEBUG_MSG("starting Ethernet DHCP\n"); - if (Ethernet.begin(mac) == 0) { - DEBUG_MSG("DHCP failed\n"); - } else{ - DEBUG_MSG("DHCP assigned IP %s\n",Ethernet.localIP()); - } + status = Ethernet.begin(mac); } else if (config.network.eth_mode == Config_NetworkConfig_EthMode_STATIC) { DEBUG_MSG("starting Ethernet Static\n"); Ethernet.begin(mac, config.network.eth_config.ip, config.network.eth_config.dns, config.network.eth_config.subnet); @@ -33,16 +38,34 @@ bool initEthernet() DEBUG_MSG("Ethernet Disabled\n"); return false; } + + if (status == 0) { + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + DEBUG_MSG("Ethernet shield was not found.\n"); + return false; + } else if (Ethernet.linkStatus() == LinkOFF) { + DEBUG_MSG("Ethernet cable is not connected.\n"); + return false; + } else{ + DEBUG_MSG("Unknown Ethernet error.\n"); + return false; + } + } else { + DEBUG_MSG("Local IP %u.%u.%u.%u\n",Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]); + DEBUG_MSG("Subnet Mask %u.%u.%u.%u\n",Ethernet.subnetMask()[0], Ethernet.subnetMask()[1], Ethernet.subnetMask()[2], Ethernet.subnetMask()[3]); + DEBUG_MSG("Gateway IP %u.%u.%u.%u\n",Ethernet.gatewayIP()[0], Ethernet.gatewayIP()[1], Ethernet.gatewayIP()[2], Ethernet.gatewayIP()[3]); + DEBUG_MSG("DNS Server IP %u.%u.%u.%u\n",Ethernet.dnsServerIP()[0], Ethernet.dnsServerIP()[1], Ethernet.dnsServerIP()[2], Ethernet.dnsServerIP()[3]); + } return true; } else { - DEBUG_MSG("Not using Ethernet\n"); return false; } } bool isEthernetAvailable() { + if (!config.network.eth_enabled) { return false; } else if (Ethernet.hardwareStatus() == EthernetNoHardware) { @@ -52,4 +75,4 @@ bool isEthernetAvailable() { } else { return true; } -} \ No newline at end of file +} diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 7deba4668..9d6c68225 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -8,7 +8,9 @@ #include "mesh/generated/mqtt.pb.h" #include "mesh/generated/telemetry.pb.h" #include "sleep.h" +#if HAS_WIFI #include +#endif #include #include @@ -189,7 +191,12 @@ bool MQTT::wantsLink() const } } +#if HAS_WIFI return hasChannel && WiFi.isConnected(); +#endif +#if HAS_ETHERNET + return hasChannel && (Ethernet.linkStatus() == LinkON); +#endif } int32_t MQTT::runOnce() @@ -346,9 +353,9 @@ std::string MQTT::downstreamPacketToJson(MeshPacket *mp) msgPayload = Json::object{ {"time", (int)decoded->time}, {"pos_timestamp", (int)decoded->timestamp}, - {"latitude_i", decoded->latitude_i}, - {"longitude_i", decoded->longitude_i}, - {"altitude", decoded->altitude} + {"latitude_i", (int)decoded->latitude_i}, + {"longitude_i", (int)decoded->longitude_i}, + {"altitude", (int)decoded->altitude} }; } else { DEBUG_MSG("Error decoding protobuf for position message!\n"); @@ -371,8 +378,8 @@ std::string MQTT::downstreamPacketToJson(MeshPacket *mp) {"description", decoded->description}, {"expire", (int)decoded->expire}, {"locked", decoded->locked}, - {"latitude_i", decoded->latitude_i}, - {"longitude_i", decoded->longitude_i}, + {"latitude_i", (int)decoded->latitude_i}, + {"longitude_i", (int)decoded->longitude_i}, }; } else { DEBUG_MSG("Error decoding protobuf for position message!\n"); diff --git a/src/mqtt/MQTT.h b/src/mqtt/MQTT.h index 9d80c7d91..4412e9526 100644 --- a/src/mqtt/MQTT.h +++ b/src/mqtt/MQTT.h @@ -5,7 +5,12 @@ #include "concurrency/OSThread.h" #include "mesh/Channels.h" #include +#if HAS_WIFI #include +#endif +#if HAS_ETHERNET +#include +#endif /** * Our wrapper/singleton for sending/receiving MQTT "udp" packets. This object isolates the MQTT protocol implementation from @@ -16,7 +21,12 @@ class MQTT : private concurrency::OSThread // supposedly the current version is busted: // http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html // WiFiClientSecure wifiClient; +#if HAS_WIFI WiFiClient mqttClient; +#endif +#if HAS_ETHERNET + EthernetClient mqttClient; +#endif PubSubClient pubSub; // instead we supress sleep from our runOnce() callback diff --git a/variants/rak4631/platformio.ini b/variants/rak4631/platformio.ini index 3d4e46d30..fccffb51f 100644 --- a/variants/rak4631/platformio.ini +++ b/variants/rak4631/platformio.ini @@ -3,11 +3,12 @@ extends = nrf52840_base board = wiscore_rak4631 build_flags = ${nrf52840_base.build_flags} -Ivariants/rak4631 -D RAK_4631 -build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak4631> + +build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak4631> + + lib_deps = ${nrf52840_base.lib_deps} + ${networking_base.lib_deps} melopero/Melopero RV3028@^1.1.0 - beegee-tokyo/RAK13800-W5100S@^1.0.1 + https://github.com/caveman99/RAK13800-W5100S.git#main debug_tool = jlink ; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm) ;upload_protocol = jlink \ No newline at end of file diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index c0bc2e5c1..904491b6a 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -231,6 +231,10 @@ static const uint8_t SCK = PIN_SPI_SCK; #define HAS_ETHERNET 1 +#define PIN_ETHERNET_RESET 21 +#define PIN_ETHERNET_SS PIN_EINK_CS +#define ETH_SPI_PORT SPI1 + #ifdef __cplusplus } #endif