Move network services into system

This commit is contained in:
fvanroie 2021-02-17 14:41:18 +01:00
parent 0b2b3fe7a8
commit 215c2645b3
11 changed files with 96 additions and 66 deletions

View File

@ -13,8 +13,8 @@
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_network.h" #include "hasp_network.h"
#include "../../hasp/hasp.h" #include "hasp/hasp.h"
#include "../../svc/hasp_mdns.h" #include "sys/svc/hasp_mdns.h"
#if HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0 #if HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0
void networkStart(void) void networkStart(void)
@ -41,60 +41,91 @@ void networkStop(void)
void networkSetup() void networkSetup()
{ {
#if HASP_USE_ETHERNET > 0 #if HASP_USE_ETHERNET > 0
ethernetSetup(); ethernetSetup();
#endif #endif
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
wifiSetup(); wifiSetup();
#endif #endif
} }
void networkLoop(void) void networkLoop(void)
{ {
#if HASP_USE_ETHERNET > 0 #if HASP_USE_ETHERNET > 0
ethernetLoop(); ethernetLoop();
#endif #endif
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
// wifiLoop(); // wifiLoop();
#endif #endif
#if HASP_USE_TASMOTA_CLIENT > 0
tasmotaclientLoop();
#endif // HASP_USE_TASMOTA_CLIENT
#if HASP_USE_HTTP > 0
httpLoop();
#endif // HTTP
#if HASP_USE_GPIO > 0
gpioLoop();
#endif // GPIO
#if HASP_USE_OTA > 0
otaLoop();
#endif // OTA
#if HASP_USE_MDNS > 0
mdnsLoop();
#endif // MDNS
#if HASP_USE_TELNET > 0
telnetLoop(); // Console
#endif // TELNET
} }
bool networkEvery5Seconds(void) bool networkEvery5Seconds(void)
{ {
#if HASP_USE_ETHERNET > 0 #if HASP_USE_ETHERNET > 0
return ethernetEvery5Seconds(); return ethernetEvery5Seconds();
#endif #endif
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
return wifiEvery5Seconds(); return wifiEvery5Seconds();
#endif #endif
return false; return false;
} }
bool networkEverySecond(void) bool networkEverySecond(void)
{ {
#if HASP_USE_ETHERNET > 0 bool connected = false;
// return ethernetEverySecond();
#endif #if HASP_USE_ETHERNET > 0
connected |= ethernetEverySecond();
#endif
#if HASP_USE_WIFI > 0
// connected |= wifiEverySecond();
#endif
#if HASP_USE_OTA > 0
otaEverySecond(); // progressbar
#endif
#if HASP_USE_WIFI > 0
// return wifiEverySecond();
#endif
return true; return true;
} }
void network_get_statusupdate(char * buffer, size_t len) void network_get_statusupdate(char * buffer, size_t len)
{ {
#if HASP_USE_ETHERNET > 0 #if HASP_USE_ETHERNET > 0
ethernet_get_statusupdate(buffer, len); ethernet_get_statusupdate(buffer, len);
#endif #endif
#if HASP_USE_WIFI > 0 #if HASP_USE_WIFI > 0
wifi_get_statusupdate(buffer, len); wifi_get_statusupdate(buffer, len);
#endif #endif
} }
#endif #endif

View File

@ -3,25 +3,25 @@
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "hasp_conf.h" #include "hasp_conf.h"
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_ota.h" #include "hasp_ota.h"
#include "../hasp/hasp_dispatch.h" #include "../../hasp/hasp_dispatch.h"
#include "../hasp/hasp.h" #include "../../hasp/hasp.h"
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h> #include <ESP8266httpUpdate.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#else #else
#include <HTTPClient.h> #include <HTTPClient.h>
#include <HTTPUpdate.h> #include <HTTPUpdate.h>
#include <WiFi.h> #include <WiFi.h>
#endif #endif
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
static WiFiClient otaClient; static WiFiClient otaClient;
std::string otaUrl = "http://ota.netwize.be"; std::string otaUrl = "http://ota.netwize.be";
@ -141,27 +141,27 @@ void otaSetup(void)
// delay(5000); // delay(5000);
}); });
#if HASP_USE_MQTT > 0 #if HASP_USE_MQTT > 0
ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); ArduinoOTA.setHostname(String(mqttGetNodename()).c_str());
#else #else
ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); ArduinoOTA.setHostname(String(mqttGetNodename()).c_str());
#endif #endif
// ArduinoOTA.setPassword(configPassword); // ArduinoOTA.setPassword(configPassword);
ArduinoOTA.setPort(otaPort); ArduinoOTA.setPort(otaPort);
#if ESP32 #if ESP32
#if HASP_USE_MDNS > 0 #if HASP_USE_MDNS > 0
ArduinoOTA.setMdnsEnabled(true); ArduinoOTA.setMdnsEnabled(true);
#else #else
ArduinoOTA.setMdnsEnabled(false); ArduinoOTA.setMdnsEnabled(false);
#endif #endif
// ArduinoOTA.setTimeout(1000); // ArduinoOTA.setTimeout(1000);
#endif #endif
ArduinoOTA.setRebootOnSuccess(false); // We do that ourselves ArduinoOTA.setRebootOnSuccess(false); // We do that ourselves
#ifdef OTA_PASSWORD #ifdef OTA_PASSWORD
ArduinoOTA.setPassword(OTA_PASSWORD); ArduinoOTA.setPassword(OTA_PASSWORD);
#endif #endif
ArduinoOTA.begin(); ArduinoOTA.begin();
LOG_INFO(TAG_OTA, F(D_SERVICE_STARTED)); LOG_INFO(TAG_OTA, F(D_SERVICE_STARTED));
@ -182,19 +182,19 @@ void otaEverySecond(void)
void otaHttpUpdate(const char * espOtaUrl) void otaHttpUpdate(const char * espOtaUrl)
{ // Update ESP firmware from HTTP { // Update ESP firmware from HTTP
#if HASP_USE_MDNS > 0 #if HASP_USE_MDNS > 0
mdnsStop(); // Keep mDNS responder from breaking things mdnsStop(); // Keep mDNS responder from breaking things
#endif #endif
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
// ESPhttpUpdate.onStart(update_started); // ESPhttpUpdate.onStart(update_started);
// ESPhttpUpdate.onEnd(update_finished); // ESPhttpUpdate.onEnd(update_finished);
// ESPhttpUpdate.onProgress(update_progress); // ESPhttpUpdate.onProgress(update_progress);
// ESPhttpUpdate.onError(update_error); // ESPhttpUpdate.onError(update_error);
ESP8266HTTPUpdate httpUpdate; ESP8266HTTPUpdate httpUpdate;
#else #else
HTTPUpdate httpUpdate; HTTPUpdate httpUpdate;
#endif #endif
httpUpdate.rebootOnUpdate(false); // We do that ourselves httpUpdate.rebootOnUpdate(false); // We do that ourselves
t_httpUpdate_return returnCode = httpUpdate.update(otaClient, espOtaUrl); t_httpUpdate_return returnCode = httpUpdate.update(otaClient, espOtaUrl);
@ -214,9 +214,9 @@ void otaHttpUpdate(const char * espOtaUrl)
dispatch_reboot(true); dispatch_reboot(true);
} }
#if HASP_USE_MDNS > 0 #if HASP_USE_MDNS > 0
mdnsStart(); mdnsStart();
#endif // HASP_USE_MDNS #endif // HASP_USE_MDNS
} }
#endif // ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32 #endif // ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32

View File

@ -2,7 +2,7 @@
For full license information read the LICENSE file in the project folder */ For full license information read the LICENSE file in the project folder */
#include "hasp_conf.h" #include "hasp_conf.h"
#if HASP_USE_TASMOTA_SLAVE > 0 #if HASP_USE_TASMOTA_CLIENT > 0
#include "hasp_slave.h" #include "hasp_slave.h"
#include "ArduinoJson.h" #include "ArduinoJson.h"
@ -52,7 +52,7 @@ void slave_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * at
slave.ExecuteCommand((char *)cBuffer); slave.ExecuteCommand((char *)cBuffer);
// Log after char buffers are cleared // Log after char buffers are cleared
LOG_TRACE(TAG_TASM, F("TAS PUB: %sstate/json = {\"p[%u].b[%u].%s\":\"%s\"}"), slaveNodeTopic, pageid, btnid, LOG_TRACE(TAG_TASM, F("TAS PUB: %sstate/json = {\"p[%u].b[%u].%s\":\"%s\"}"), slaveNodeTopic, pageid, btnid,
attribute, data); attribute, data);
} }
void slave_send_input(uint8_t id, const char * payload) void slave_send_input(uint8_t id, const char * payload)
@ -145,7 +145,7 @@ void TASMO_EVERY_SECOND(void)
void slaveSetup() void slaveSetup()
{ {
Serial2.begin(HASP_SLAVE_SPEED); Serial2.begin(HASP_TASMOTACLIENT_SPEED);
// slave.attach_FUNC_EVERY_SECOND(TASMO_EVERY_SECOND); // slave.attach_FUNC_EVERY_SECOND(TASMO_EVERY_SECOND);
slave.attach_FUNC_JSON(TASMO_TELE_JSON); slave.attach_FUNC_JSON(TASMO_TELE_JSON);
slave.attach_FUNC_COMMAND_SEND(TASMO_DATA_RECEIVE); slave.attach_FUNC_COMMAND_SEND(TASMO_DATA_RECEIVE);

View File

@ -1,15 +1,15 @@
/* MIT License - Copyright (c) 2020 Francis Van Roie /* MIT License - Copyright (c) 2020 Francis Van Roie
For full license information read the LICENSE file in the project folder */ For full license information read the LICENSE file in the project folder */
#ifndef HASP_SLAVE_H #ifndef HASP_TASMOTACLIENT_H
#define HASP_SLAVE_H #define HASP_TASMOTACLIENT_H
#include "ArduinoJson.h" #include "ArduinoJson.h"
#define HASP_SLAVE_SPEED 57600 #define HASP_TASMOTACLIENT_SPEED 57600
void TASMO_EVERY_SECOND(void); void TASMO_EVERY_SECOND(void);
void TASMO_DATA_RECEIVE(char *data); void TASMO_DATA_RECEIVE(char * data);
void slave_send_state(const __FlashStringHelper * subtopic, const char * payload); void slave_send_state(const __FlashStringHelper * subtopic, const char * payload);
void slave_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute, const char * data); void slave_send_obj_attribute_str(uint8_t pageid, uint8_t btnid, const char * attribute, const char * data);
void slave_send_input(uint8_t id, const char * payload); void slave_send_input(uint8_t id, const char * payload);
@ -18,5 +18,4 @@ void slave_send_statusupdate();
void slaveSetup(); void slaveSetup();
void slaveLoop(void); void slaveLoop(void);
#endif #endif