extract wifi networking

This commit is contained in:
2018-11-16 19:10:34 +01:00
parent 9e8104da70
commit 85c8e681f7
6 changed files with 2 additions and 219 deletions

View File

@@ -21,6 +21,7 @@ lib_deps =
Hash
TaskScheduler
SPIFFS
ArduinoJson
;[env:build]
;src_filter = +<*> -<examples/>
@@ -41,14 +42,4 @@ board = ${common.board}
upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud}
framework = ${common.framework}
lib_deps = ${common.lib_deps}
[env:wifi]
src_filter = +<*> -<examples/> +<examples/wifi/>
platform = ${common.platform}
board = ${common.board}
upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud}
framework = ${common.framework}
lib_deps = ${common.lib_deps}
ESP8266mDNS
lib_deps = ${common.lib_deps}

View File

@@ -6,7 +6,6 @@
#define _TASK_PRIORITY
#include <TaskSchedulerDeclarations.h>
//#include <TaskScheduler.h>
#include <Arduino.h>
#include <vector>
#include "FS.h"

View File

@@ -1,73 +0,0 @@
#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;
}

View File

@@ -1,74 +0,0 @@
#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

View File

@@ -1,32 +0,0 @@
#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
// Chip config
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000
// network config
#define SPROCKET_MODE 0
#define AP_SSID "MyAP"
#define AP_PASSWORD "myApPwd"
#define STATION_SSID "Th1ngs4p"
#define STATION_PASSWORD "th3r31sn0sp00n"
#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

View File

@@ -1,28 +0,0 @@
#include "config.h"
#include "WiFiNet.h"
#include "Sprocket.h"
WiFiNet wifi(
SPROCKET_MODE,
STATION_SSID,
STATION_PASSWORD,
AP_SSID,
AP_PASSWORD,
HOSTNAME,
CONNECT_TIMEOUT);
Sprocket sprocket(
{STARTUP_DELAY, SERIAL_BAUD_RATE});
void setup()
{
delay(3000);
wifi.connect();
sprocket.activate();
}
void loop()
{
sprocket.loop();
yield();
}