mirror of
https://gitlab.com/wirelos/sprocket-network-wifi.git
synced 2025-12-14 04:36:51 +01:00
extract wifi functionality from sprocket-core
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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 = +<*> -<examples/> +<examples/basic/>
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "TemplatePlugin.h"
|
||||
|
||||
TemplatePlugin::TemplatePlugin(TemplateConfig cfg)
|
||||
{
|
||||
templateConfig = cfg;
|
||||
}
|
||||
|
||||
void TemplatePlugin::activate(Scheduler *scheduler)
|
||||
{
|
||||
PRINT_MSG(Serial, "TEMPLATE", "plugin activated");
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef __TEMPLATE_PLUGIN__
|
||||
#define __TEMPLATE_PLUGIN__
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
#include <Plugin.h>
|
||||
#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
|
||||
73
src/WiFiNet.cpp
Normal file
73
src/WiFiNet.cpp
Normal file
@@ -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;
|
||||
}
|
||||
74
src/WiFiNet.h
Normal file
74
src/WiFiNet.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef __WIFI_NET__
|
||||
#define __WIFI_NET__
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif // ESP32
|
||||
|
||||
#include "Network.h"
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <FS.h>
|
||||
#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
|
||||
@@ -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
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user