Skip to content

Commit 96a68cd

Browse files
static IP, reconnect, etc.
1 parent 3901ce4 commit 96a68cd

File tree

4 files changed

+63
-41
lines changed

4 files changed

+63
-41
lines changed

controller/tea_poor/lib/Arduino/RemoteControl.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ void debugNetworkInfo() {
3838
Serial.println();
3939
}
4040

41-
RemoteControl::RemoteControl(const char* SSID, const char* SSIDPassword) :
42-
_SSID(SSID), _SSIDPassword(SSIDPassword),
43-
_server(80), _app()
44-
{
45-
}
46-
47-
RemoteControl::~RemoteControl() {
48-
}
49-
50-
void RemoteControl::_setupNetwork() {
41+
void verifyNetwork() {
5142
if (WiFi.status() == WL_NO_MODULE) {
5243
Serial.println("Communication with WiFi module failed!");
5344
while(true) delay(500);
@@ -59,15 +50,25 @@ void RemoteControl::_setupNetwork() {
5950
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
6051
Serial.println("Please upgrade your firmware.");
6152
}
53+
}
54+
55+
RemoteControl::RemoteControl(const NetworkConnectCallback &onConnect) :
56+
_onConnect(onConnect)
57+
{
58+
}
6259

60+
RemoteControl::~RemoteControl() {
61+
}
62+
63+
void RemoteControl::connectTo(const char* ssid, const char* password) {
6364
Serial.print("Connecting to ");
64-
Serial.println(_SSID);
65+
Serial.println(ssid);
6566

6667
int attempts = 0;
6768
while (WL_CONNECTED != WiFi.status()) { // try to connect to the network
6869
attempts++;
69-
Serial.println("Atempt to connect: " + String(attempts));
70-
WiFi.begin(_SSID.c_str(), _SSIDPassword.c_str());
70+
Serial.println("Attempt to connect: " + String(attempts));
71+
WiFi.begin(ssid, password);
7172
for (int i = 0; i < 50; i++) { // wait for connection
7273
Serial.print(".");
7374
delay(500);
@@ -77,30 +78,33 @@ void RemoteControl::_setupNetwork() {
7778
Serial.println("Connection status: " + String(WiFi.status()));
7879
}
7980
Serial.println();
80-
81+
// successfully connected
8182
debugNetworkInfo();
8283
}
8384

84-
void RemoteControl::setup(RemoteControlRoutesCallback routes) {
85-
_setupNetwork();
86-
routes(_app); // setup routes
85+
void RemoteControl::setup() { reconnect(); }
86+
87+
void RemoteControl::reconnect() {
88+
// reset everything
89+
WiFi.disconnect();
90+
verifyNetwork();
91+
_app = Application(); // reset routes
92+
_server = WiFiServer(80); // reset server
93+
// reconnect
94+
_onConnect(*this, _app);
8795
_server.begin();
8896
}
8997

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

94106
if (client.connected()) {
95107
_app.process(&client);
96108
client.stop();
97109
}
98-
}
99-
100-
String RemoteControl::asJSONString() const {
101-
String result = "{";
102-
result += "\"SSID\": \"" + _SSID + "\",";
103-
result += "\"signal strength\": " + String(WiFi.RSSI());
104-
result += "}";
105-
return result;
106110
}

controller/tea_poor/lib/Arduino/RemoteControl.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
#include <Arduino.h>
55
#include <WiFiS3.h>
66
#include <aWOT.h>
7+
#include <functional>
78

8-
// define routes callback function signature
9-
typedef void (*RemoteControlRoutesCallback)(Application &app);
9+
// forward declaration
10+
class RemoteControl;
11+
12+
// define callback for (re)connecting to WiFi, use std::function
13+
typedef std::function<void(RemoteControl&, Application&)> NetworkConnectCallback;
1014

1115
class RemoteControl {
1216
public:
13-
RemoteControl(const char* SSID, const char* SSIDPassword);
17+
RemoteControl(const NetworkConnectCallback &onConnect);
1418
~RemoteControl();
15-
void setup(RemoteControlRoutesCallback routes);
19+
void setup();
1620
void process();
17-
String asJSONString() const;
21+
void reconnect();
22+
///////////////////
23+
void connectTo(const char* ssid, const char* password);
1824
private:
19-
const String _SSID;
20-
const String _SSIDPassword;
25+
NetworkConnectCallback _onConnect;
2126
WiFiServer _server;
2227
Application _app;
2328

controller/tea_poor/src/main.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ auto waterPump = std::make_shared<WaterPumpScheduler>(
1818
)
1919
);
2020

21-
// setting up remote control
22-
RemoteControl remoteControl(WIFI_SSID, WIFI_PASSWORD);
23-
2421
// build command processor
2522
CommandProcessor commandProcessor(
2623
WATER_PUMP_SAFE_THRESHOLD,
@@ -35,10 +32,17 @@ void withExtraHeaders(Response &res) {
3532
res.set("Content-Type", "application/json");
3633
}
3734

38-
void setup() {
39-
Serial.begin(9600);
40-
waterPump->setup();
41-
remoteControl.setup([](Application &app) {
35+
RemoteControl remoteControl(
36+
// lambda function to setup network
37+
[](RemoteControl &remoteControl, Application &app) {
38+
// connect to WiFi
39+
// set static IP address, if defined in configs
40+
#ifdef WIFI_IP_ADDRESS
41+
WiFi.config(WIFI_IP_ADDRESS);
42+
#endif
43+
44+
remoteControl.connectTo(WIFI_SSID, WIFI_PASSWORD);
45+
// setup routes
4246
app.get("/pour_tea", [](Request &req, Response &res) {
4347
char milliseconds[64];
4448
req.query("milliseconds", milliseconds, 64);
@@ -59,7 +63,13 @@ void setup() {
5963
withExtraHeaders(res);
6064
res.print(response.c_str());
6165
});
62-
});
66+
}
67+
);
68+
69+
void setup() {
70+
Serial.begin(9600);
71+
waterPump->setup();
72+
remoteControl.setup();
6373
}
6474

6575
void loop() {

controller/tea_poor/src/secrets.h.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ const int WATER_PUMP_POWER_PIN = 3;
1515
// Their is no reason to make it configurable and add unnecessary complexity
1616
const int WATER_PUMP_SAFE_THRESHOLD = 10 * 1000;
1717

18+
// Static IP address. If not defined, dynamic IP address will be used
19+
// #define WIFI_IP_ADDRESS IPAddress(192, 168, 1, 123)
20+
1821
#endif // SECRETS_H

0 commit comments

Comments
 (0)