Skip to content

Commit

Permalink
static IP, reconnect, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Jan 13, 2024
1 parent 3901ce4 commit 96a68cd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 41 deletions.
56 changes: 30 additions & 26 deletions controller/tea_poor/lib/Arduino/RemoteControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,7 @@ void debugNetworkInfo() {
Serial.println();
}

RemoteControl::RemoteControl(const char* SSID, const char* SSIDPassword) :
_SSID(SSID), _SSIDPassword(SSIDPassword),
_server(80), _app()
{
}

RemoteControl::~RemoteControl() {
}

void RemoteControl::_setupNetwork() {
void verifyNetwork() {
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while(true) delay(500);
Expand All @@ -59,15 +50,25 @@ void RemoteControl::_setupNetwork() {
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
Serial.println("Please upgrade your firmware.");
}
}

RemoteControl::RemoteControl(const NetworkConnectCallback &onConnect) :
_onConnect(onConnect)
{
}

RemoteControl::~RemoteControl() {
}

void RemoteControl::connectTo(const char* ssid, const char* password) {
Serial.print("Connecting to ");
Serial.println(_SSID);
Serial.println(ssid);

int attempts = 0;
while (WL_CONNECTED != WiFi.status()) { // try to connect to the network
attempts++;
Serial.println("Atempt to connect: " + String(attempts));
WiFi.begin(_SSID.c_str(), _SSIDPassword.c_str());
Serial.println("Attempt to connect: " + String(attempts));
WiFi.begin(ssid, password);
for (int i = 0; i < 50; i++) { // wait for connection
Serial.print(".");
delay(500);
Expand All @@ -77,30 +78,33 @@ void RemoteControl::_setupNetwork() {
Serial.println("Connection status: " + String(WiFi.status()));
}
Serial.println();

// successfully connected
debugNetworkInfo();
}

void RemoteControl::setup(RemoteControlRoutesCallback routes) {
_setupNetwork();
routes(_app); // setup routes
void RemoteControl::setup() { reconnect(); }

void RemoteControl::reconnect() {
// reset everything
WiFi.disconnect();
verifyNetwork();
_app = Application(); // reset routes
_server = WiFiServer(80); // reset server
// reconnect
_onConnect(*this, _app);
_server.begin();
}

void RemoteControl::process() {
// TODO: check if we still have a connection. If not, reconnect.
if(WL_CONNECTED != WiFi.status()) {
reconnect();
return; // wait for next tick, just to be sure that all is ok
}
///////////////////////////
WiFiClient client = _server.available();

if (client.connected()) {
_app.process(&client);
client.stop();
}
}

String RemoteControl::asJSONString() const {
String result = "{";
result += "\"SSID\": \"" + _SSID + "\",";
result += "\"signal strength\": " + String(WiFi.RSSI());
result += "}";
return result;
}
19 changes: 12 additions & 7 deletions controller/tea_poor/lib/Arduino/RemoteControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
#include <Arduino.h>
#include <WiFiS3.h>
#include <aWOT.h>
#include <functional>

// define routes callback function signature
typedef void (*RemoteControlRoutesCallback)(Application &app);
// forward declaration
class RemoteControl;

// define callback for (re)connecting to WiFi, use std::function
typedef std::function<void(RemoteControl&, Application&)> NetworkConnectCallback;

class RemoteControl {
public:
RemoteControl(const char* SSID, const char* SSIDPassword);
RemoteControl(const NetworkConnectCallback &onConnect);
~RemoteControl();
void setup(RemoteControlRoutesCallback routes);
void setup();
void process();
String asJSONString() const;
void reconnect();
///////////////////
void connectTo(const char* ssid, const char* password);
private:
const String _SSID;
const String _SSIDPassword;
NetworkConnectCallback _onConnect;
WiFiServer _server;
Application _app;

Expand Down
26 changes: 18 additions & 8 deletions controller/tea_poor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ auto waterPump = std::make_shared<WaterPumpScheduler>(
)
);

// setting up remote control
RemoteControl remoteControl(WIFI_SSID, WIFI_PASSWORD);

// build command processor
CommandProcessor commandProcessor(
WATER_PUMP_SAFE_THRESHOLD,
Expand All @@ -35,10 +32,17 @@ void withExtraHeaders(Response &res) {
res.set("Content-Type", "application/json");
}

void setup() {
Serial.begin(9600);
waterPump->setup();
remoteControl.setup([](Application &app) {
RemoteControl remoteControl(
// lambda function to setup network
[](RemoteControl &remoteControl, Application &app) {
// connect to WiFi
// set static IP address, if defined in configs
#ifdef WIFI_IP_ADDRESS
WiFi.config(WIFI_IP_ADDRESS);
#endif

remoteControl.connectTo(WIFI_SSID, WIFI_PASSWORD);
// setup routes
app.get("/pour_tea", [](Request &req, Response &res) {
char milliseconds[64];
req.query("milliseconds", milliseconds, 64);
Expand All @@ -59,7 +63,13 @@ void setup() {
withExtraHeaders(res);
res.print(response.c_str());
});
});
}
);

void setup() {
Serial.begin(9600);
waterPump->setup();
remoteControl.setup();
}

void loop() {
Expand Down
3 changes: 3 additions & 0 deletions controller/tea_poor/src/secrets.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ const int WATER_PUMP_POWER_PIN = 3;
// Their is no reason to make it configurable and add unnecessary complexity
const int WATER_PUMP_SAFE_THRESHOLD = 10 * 1000;

// Static IP address. If not defined, dynamic IP address will be used
// #define WIFI_IP_ADDRESS IPAddress(192, 168, 1, 123)

#endif // SECRETS_H

0 comments on commit 96a68cd

Please sign in to comment.