MQTT is working over ethernet

raytac-diy
Thomas Göttgens 2022-10-26 00:07:02 +02:00
rodzic 9fac57b713
commit a66538fe55
7 zmienionych plików z 84 dodań i 31 usunięć

Wyświetl plik

@ -1,5 +1,6 @@
#include "buzz.h" #include "buzz.h"
#include "configuration.h" #include "configuration.h"
#include "NodeDB.h"
#ifndef PIN_BUZZER #ifndef PIN_BUZZER
@ -42,6 +43,7 @@ const int DURATION_1_8 = 125; // 1/8 note
const int DURATION_1_4 = 250; // 1/4 note const int DURATION_1_4 = 250; // 1/4 note
void playTones(const ToneDuration *tone_durations, int size) { void playTones(const ToneDuration *tone_durations, int size) {
if (config.network.eth_enabled != true) {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
const auto &tone_duration = tone_durations[i]; const auto &tone_duration = tone_durations[i];
#ifdef M5STACK #ifdef M5STACK
@ -54,6 +56,7 @@ void playTones(const ToneDuration *tone_durations, int size) {
// to distinguish the notes, set a minimum time between them. // to distinguish the notes, set a minimum time between them.
delay(1.3 * tone_duration.duration_ms); delay(1.3 * tone_duration.duration_ms);
} }
}
} }
#ifdef M5STACK #ifdef M5STACK

Wyświetl plik

@ -42,6 +42,10 @@
#include "mqtt/MQTT.h" #include "mqtt/MQTT.h"
#endif #endif
#if HAS_ETHERNET
#include "mqtt/MQTT.h"
#endif
#include "LLCC68Interface.h" #include "LLCC68Interface.h"
#include "RF95Interface.h" #include "RF95Interface.h"
#include "SX1262Interface.h" #include "SX1262Interface.h"
@ -279,11 +283,12 @@ void setup()
#ifdef ARCH_NRF52 #ifdef ARCH_NRF52
nrf52Setup(); nrf52Setup();
#endif #endif
playStartMelody();
// We do this as early as possible because this loads preferences from flash // 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 // but we need to do this after main cpu iniot (esp32setup), because we need the random seed set
nodeDB.init(); nodeDB.init();
playStartMelody();
// Currently only the tbeam has a PMU // Currently only the tbeam has a PMU
power = new Power(); power = new Power();
power->setStatusHandler(powerStatus); power->setStatusHandler(powerStatus);
@ -440,7 +445,7 @@ void setup()
} }
#endif #endif
#if HAS_WIFI #if HAS_WIFI || HAS_ETHERNET
mqttInit(); mqttInit();
#endif #endif

Wyświetl plik

@ -7,25 +7,30 @@
// Startup Ethernet // Startup Ethernet
bool initEthernet() bool initEthernet()
{ {
config.network.eth_enabled = true;
config.network.eth_mode = Config_NetworkConfig_EthMode_DHCP;
if (config.network.eth_enabled) { 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]; uint8_t mac[6];
int status = 0;
getMacAddr(mac); // FIXME use the BLE MAC for now... getMacAddr(mac); // FIXME use the BLE MAC for now...
if (Ethernet.hardwareStatus() == EthernetNoHardware) { if (config.network.eth_mode == Config_NetworkConfig_EthMode_DHCP) {
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) {
DEBUG_MSG("starting Ethernet DHCP\n"); DEBUG_MSG("starting Ethernet DHCP\n");
if (Ethernet.begin(mac) == 0) { status = Ethernet.begin(mac);
DEBUG_MSG("DHCP failed\n");
} else{
DEBUG_MSG("DHCP assigned IP %s\n",Ethernet.localIP());
}
} else if (config.network.eth_mode == Config_NetworkConfig_EthMode_STATIC) { } else if (config.network.eth_mode == Config_NetworkConfig_EthMode_STATIC) {
DEBUG_MSG("starting Ethernet Static\n"); DEBUG_MSG("starting Ethernet Static\n");
Ethernet.begin(mac, config.network.eth_config.ip, config.network.eth_config.dns, config.network.eth_config.subnet); 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"); DEBUG_MSG("Ethernet Disabled\n");
return false; 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; return true;
} else { } else {
DEBUG_MSG("Not using Ethernet\n"); DEBUG_MSG("Not using Ethernet\n");
return false; return false;
} }
} }
bool isEthernetAvailable() { bool isEthernetAvailable() {
if (!config.network.eth_enabled) { if (!config.network.eth_enabled) {
return false; return false;
} else if (Ethernet.hardwareStatus() == EthernetNoHardware) { } else if (Ethernet.hardwareStatus() == EthernetNoHardware) {

Wyświetl plik

@ -8,7 +8,9 @@
#include "mesh/generated/mqtt.pb.h" #include "mesh/generated/mqtt.pb.h"
#include "mesh/generated/telemetry.pb.h" #include "mesh/generated/telemetry.pb.h"
#include "sleep.h" #include "sleep.h"
#if HAS_WIFI
#include <WiFi.h> #include <WiFi.h>
#endif
#include <assert.h> #include <assert.h>
#include <json11.hpp> #include <json11.hpp>
@ -189,7 +191,12 @@ bool MQTT::wantsLink() const
} }
} }
#if HAS_WIFI
return hasChannel && WiFi.isConnected(); return hasChannel && WiFi.isConnected();
#endif
#if HAS_ETHERNET
return hasChannel && (Ethernet.linkStatus() == LinkON);
#endif
} }
int32_t MQTT::runOnce() int32_t MQTT::runOnce()
@ -346,9 +353,9 @@ std::string MQTT::downstreamPacketToJson(MeshPacket *mp)
msgPayload = Json::object{ msgPayload = Json::object{
{"time", (int)decoded->time}, {"time", (int)decoded->time},
{"pos_timestamp", (int)decoded->timestamp}, {"pos_timestamp", (int)decoded->timestamp},
{"latitude_i", decoded->latitude_i}, {"latitude_i", (int)decoded->latitude_i},
{"longitude_i", decoded->longitude_i}, {"longitude_i", (int)decoded->longitude_i},
{"altitude", decoded->altitude} {"altitude", (int)decoded->altitude}
}; };
} else { } else {
DEBUG_MSG("Error decoding protobuf for position message!\n"); DEBUG_MSG("Error decoding protobuf for position message!\n");
@ -371,8 +378,8 @@ std::string MQTT::downstreamPacketToJson(MeshPacket *mp)
{"description", decoded->description}, {"description", decoded->description},
{"expire", (int)decoded->expire}, {"expire", (int)decoded->expire},
{"locked", decoded->locked}, {"locked", decoded->locked},
{"latitude_i", decoded->latitude_i}, {"latitude_i", (int)decoded->latitude_i},
{"longitude_i", decoded->longitude_i}, {"longitude_i", (int)decoded->longitude_i},
}; };
} else { } else {
DEBUG_MSG("Error decoding protobuf for position message!\n"); DEBUG_MSG("Error decoding protobuf for position message!\n");

Wyświetl plik

@ -5,7 +5,12 @@
#include "concurrency/OSThread.h" #include "concurrency/OSThread.h"
#include "mesh/Channels.h" #include "mesh/Channels.h"
#include <PubSubClient.h> #include <PubSubClient.h>
#if HAS_WIFI
#include <WiFiClient.h> #include <WiFiClient.h>
#endif
#if HAS_ETHERNET
#include <EthernetClient.h>
#endif
/** /**
* Our wrapper/singleton for sending/receiving MQTT "udp" packets. This object isolates the MQTT protocol implementation from * 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: // supposedly the current version is busted:
// http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html // http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html
// WiFiClientSecure wifiClient; // WiFiClientSecure wifiClient;
#if HAS_WIFI
WiFiClient mqttClient; WiFiClient mqttClient;
#endif
#if HAS_ETHERNET
EthernetClient mqttClient;
#endif
PubSubClient pubSub; PubSubClient pubSub;
// instead we supress sleep from our runOnce() callback // instead we supress sleep from our runOnce() callback

Wyświetl plik

@ -3,11 +3,12 @@
extends = nrf52840_base extends = nrf52840_base
board = wiscore_rak4631 board = wiscore_rak4631
build_flags = ${nrf52840_base.build_flags} -Ivariants/rak4631 -D RAK_4631 build_flags = ${nrf52840_base.build_flags} -Ivariants/rak4631 -D RAK_4631
build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak4631> +<mesh/eth/> build_src_filter = ${nrf52_base.build_src_filter} +<../variants/rak4631> +<mesh/eth/> +<mqtt/>
lib_deps = lib_deps =
${nrf52840_base.lib_deps} ${nrf52840_base.lib_deps}
${networking_base.lib_deps}
melopero/Melopero RV3028@^1.1.0 melopero/Melopero RV3028@^1.1.0
beegee-tokyo/RAK13800-W5100S@^1.0.1 https://github.com/caveman99/RAK13800-W5100S.git#main
debug_tool = jlink debug_tool = jlink
; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm) ; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm)
;upload_protocol = jlink ;upload_protocol = jlink

Wyświetl plik

@ -231,6 +231,10 @@ static const uint8_t SCK = PIN_SPI_SCK;
#define HAS_ETHERNET 1 #define HAS_ETHERNET 1
#define PIN_ETHERNET_RESET 21
#define PIN_ETHERNET_SS PIN_EINK_CS
#define ETH_SPI_PORT SPI1
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif