From 6b3254812e08caf6ba3154b2df8cde24b79471dd Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Fri, 16 Nov 2018 19:13:15 +0100 Subject: [PATCH] extract wifi functionality from sprocket-core --- library.json | 8 ++-- platformio.ini | 5 +-- src/TemplatePlugin.cpp | 11 ------ src/TemplatePlugin.h | 26 ------------- src/WiFiNet.cpp | 73 ++++++++++++++++++++++++++++++++++++ src/WiFiNet.h | 74 +++++++++++++++++++++++++++++++++++++ src/examples/basic/config.h | 30 +++++++++------ src/examples/basic/main.cpp | 33 ++++++++--------- 8 files changed, 186 insertions(+), 74 deletions(-) delete mode 100644 src/TemplatePlugin.cpp delete mode 100644 src/TemplatePlugin.h create mode 100644 src/WiFiNet.cpp create mode 100644 src/WiFiNet.h diff --git a/library.json b/library.json index 38d576d..3222aa6 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { - "name": "sprocket-plugin-template", - "keywords": "sprocket, plugin", - "description": "Template for a sprocket plugin", + "name": "sprocket-network-wifi", + "keywords": "sprocket, plugin, wifi", + "description": "Wifi for Sprockets", "authors": { "name": "Patrick Balsiger", @@ -11,7 +11,7 @@ "repository": { "type": "git", - "url": "https://gitlab.com/wirelos/sprocket-plugin-template" + "url": "https://gitlab.com/wirelos/sprocket-network-wifi" }, "frameworks": "arduino", "platforms": "espressif8266, esp32", diff --git a/platformio.ini b/platformio.ini index 8fe8167..a4850bf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,11 +13,8 @@ lib_deps = SPIFFS ArduinoJson ArduinoOTA - ESPAsyncTCP ESP8266mDNS - ESP Async WebServer - painlessMesh - https://gitlab.com/wirelos/sprocket-lib.git#develop + https://gitlab.com/wirelos/sprocket-lib.git#feature/13-separation-of-concerns [env:basic] src_filter = +<*> - + diff --git a/src/TemplatePlugin.cpp b/src/TemplatePlugin.cpp deleted file mode 100644 index d98fb20..0000000 --- a/src/TemplatePlugin.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "TemplatePlugin.h" - -TemplatePlugin::TemplatePlugin(TemplateConfig cfg) -{ - templateConfig = cfg; -} - -void TemplatePlugin::activate(Scheduler *scheduler) -{ - PRINT_MSG(Serial, "TEMPLATE", "plugin activated"); -} diff --git a/src/TemplatePlugin.h b/src/TemplatePlugin.h deleted file mode 100644 index a32c461..0000000 --- a/src/TemplatePlugin.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __TEMPLATE_PLUGIN__ -#define __TEMPLATE_PLUGIN__ - -#define _TASK_SLEEP_ON_IDLE_RUN -#define _TASK_STD_FUNCTION - -#include -#include "utils/utils_print.h" - -using namespace std; -using namespace std::placeholders; - -struct TemplateConfig -{ - const char *var; -}; - -class TemplatePlugin : public Plugin -{ - public: - TemplateConfig templateConfig; - TemplatePlugin(TemplateConfig cfg); - void activate(Scheduler *scheduler); -}; - -#endif \ No newline at end of file diff --git a/src/WiFiNet.cpp b/src/WiFiNet.cpp new file mode 100644 index 0000000..e5e8005 --- /dev/null +++ b/src/WiFiNet.cpp @@ -0,0 +1,73 @@ +#include "WiFiNet.h" + +WiFiNet::WiFiNet( + int stationMode, + const char* stationSSID, + const char* stationPassword, + const char* apSSID, + const char* apPassword, + const char* hostname, + int connectTimeout){ + config.stationMode = stationMode; + config.stationSSID = String(stationSSID); + config.stationPassword = String(stationPassword); + config.apSSID = String(apSSID); + config.apPassword = String(apPassword); + config.hostname = String(hostname); + config.connectTimeout = connectTimeout; +} + +int WiFiNet::connect(){ + config.fromFile("/config.json"); + WiFi.hostname(config.hostname); + Serial.println("Hostname: " + config.hostname); + if(!connectStation()) { + createAccessPoint(); + } + startDNS(); + return 1; +} + +int WiFiNet::connectStation(){ + if(config.stationMode == 0) return 0; + + int wifiConnectStart = millis(); + WiFi.mode(WIFI_STA); + WiFi.begin(config.stationSSID.c_str(), config.stationPassword.c_str()); + Serial.println("connect to " + config.stationSSID); + + // TODO use tasks + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + if(millis() - wifiConnectStart >= (uint)config.connectTimeout) { + Serial.println("wifi connect timeout"); + return 0; + } + } + Serial.println("IP address: " + WiFi.localIP().toString()); + Serial.println(WiFi.localIP().toString()); + return 1; +} + +int WiFiNet::createAccessPoint(){ + Serial.println("Starting SoftAP: " + String(config.apSSID)); + WiFi.disconnect(); + WiFi.mode(WIFI_AP); + WiFi.softAP(config.apSSID.c_str(), config.apPassword.c_str()); + String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); + Serial.println(softApPrt.c_str()); + return 1; +} + +// TODO make user configurable services +int WiFiNet::startDNS() { + if (!MDNS.begin(config.hostname.c_str())) { + Serial.println("Error setting up MDNS responder!"); + return 0; + } else { + Serial.println("mDNS responder started"); + MDNS.addService("http", "tcp", 80); + } + return 1; +} \ No newline at end of file diff --git a/src/WiFiNet.h b/src/WiFiNet.h new file mode 100644 index 0000000..5d3177c --- /dev/null +++ b/src/WiFiNet.h @@ -0,0 +1,74 @@ +#ifndef __WIFI_NET__ +#define __WIFI_NET__ + +#include +#include + +#ifdef ESP32 +#include +#elif defined(ESP8266) +#include +#endif // ESP32 + +#include "Network.h" + +#include +#include +#include "JsonStruct.h" +using namespace std; +using namespace std::placeholders; + +#define JSON_stationMode "stationMode" +#define JSON_stationSSID "stationSSID" +#define JSON_apPassword "apPassword" +#define JSON_apSSID "apSSID" +#define JSON_stationPassword "stationPassword" +#define JSON_hostname "hostname" +#define JSON_connect_timeout "connectTimeout" + +struct WiFiConfig : public JsonStruct { + int stationMode; + String stationSSID; + String stationPassword; + String apSSID; + String apPassword; + String hostname; + int connectTimeout; + + void mapJsonObject(JsonObject& root) { + root[JSON_stationMode] = stationMode; + root[JSON_stationSSID] = stationSSID; + root[JSON_stationPassword] = stationPassword; + root[JSON_apSSID] = apSSID; + root[JSON_apPassword] = apPassword; + root[JSON_hostname] = hostname; + root[JSON_connect_timeout] = connectTimeout; + } + + // Map a json object to this struct. + void fromJsonObject(JsonObject& json) { + stationMode = getIntAttrFromJson(json, JSON_stationMode, stationMode); + stationSSID = getAttrFromJson(json, JSON_stationSSID, stationSSID); + stationPassword = getAttrFromJson(json, JSON_stationPassword, stationPassword); + apSSID = getAttrFromJson(json, JSON_apSSID, apSSID); + apPassword = getAttrFromJson(json, JSON_apPassword, apPassword); + hostname = getAttrFromJson(json, JSON_hostname, hostname); + connectTimeout = getIntAttrFromJson(json, JSON_connect_timeout, connectTimeout); + }; +}; + +class WiFiNet : public Network { + public: + WiFiConfig config; + WiFiNet(int stationMode, const char* stationSSID, const char* stationPassword, const char* apSSID, const char* apPassword, const char* hostname, int connectTimeout); + int connect(); + int connectStation(); + int createAccessPoint(); + int startDNS(); + void configure(WiFiConfig); + int isConnected(){ + return WiFi.status() == WL_CONNECTED; + } +}; + +#endif \ No newline at end of file diff --git a/src/examples/basic/config.h b/src/examples/basic/config.h index b538d4c..1ce975a 100644 --- a/src/examples/basic/config.h +++ b/src/examples/basic/config.h @@ -1,24 +1,32 @@ -#ifndef __DEVICE_CONFIG__ -#define __DEVICE_CONFIG__ +#ifndef __STANDALONE_CONFIG__ +#define __STANDALONE_CONFIG__ // Scheduler config +#define _TASK_PRIORITY // Support for layered scheduling priority #define _TASK_SLEEP_ON_IDLE_RUN #define _TASK_STD_FUNCTION -#define _TASK_PRIORITY // Chip config -#define SPROCKET_TYPE "SPROCKET" #define SERIAL_BAUD_RATE 115200 -#define STARTUP_DELAY 1000 +#define STARTUP_DELAY 3000 // network config -#define SPROCKET_MODE 1 -#define WIFI_CHANNEL 11 -#define AP_SSID "sprocket" -#define AP_PASSWORD "th3r31sn0sp00n" -#define STATION_SSID "MyAP" +#define SPROCKET_MODE 0 +#define AP_SSID "MyAP" +#define AP_PASSWORD "myApPwd" +#define STATION_SSID "Th1ngs4p" #define STATION_PASSWORD "th3r31sn0sp00n" -#define HOSTNAME "sprocket" +#define HOSTNAME "standalone-node" #define CONNECT_TIMEOUT 10000 +// OTA config +#define OTA_PORT 8266 +#define OTA_PASSWORD "" + +// WebServer +#define WEB_CONTEXT_PATH "/" +#define WEB_DOC_ROOT "/www" +#define WEB_DEFAULT_FILE "index.html" +#define WEB_SERVER_PORT 80 + #endif \ No newline at end of file diff --git a/src/examples/basic/main.cpp b/src/examples/basic/main.cpp index a545da9..51acb6c 100644 --- a/src/examples/basic/main.cpp +++ b/src/examples/basic/main.cpp @@ -1,31 +1,28 @@ #include "config.h" #include "WiFiNet.h" #include "Sprocket.h" -#include "TemplatePlugin.h" -WiFiNet *network; -Sprocket *sprocket; +WiFiNet wifi( + SPROCKET_MODE, + STATION_SSID, + STATION_PASSWORD, + AP_SSID, + AP_PASSWORD, + HOSTNAME, + CONNECT_TIMEOUT); + +Sprocket sprocket( + {STARTUP_DELAY, SERIAL_BAUD_RATE}); void setup() { - sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); - sprocket->addPlugin(new TemplatePlugin({"sprocket"})); - - network = new WiFiNet( - SPROCKET_MODE, - STATION_SSID, - STATION_PASSWORD, - AP_SSID, - AP_PASSWORD, - HOSTNAME, - CONNECT_TIMEOUT); - network->connect(); - - sprocket->activate(); + delay(3000); + wifi.connect(); + sprocket.activate(); } void loop() { - sprocket->loop(); + sprocket.loop(); yield(); } \ No newline at end of file