Skip to content

Commit 090fd4e

Browse files
Gw dev (#1)
* refactoring * forgotten changes * extract RemoteControl class and fix request type to get * Tested code, debugged infinite While Loop by simply removing it and removed unused dependency. --------- Co-authored-by: GreenWizard <[email protected]>
1 parent a3b7171 commit 090fd4e

File tree

7 files changed

+232
-189
lines changed

7 files changed

+232
-189
lines changed

controller/tea_poor/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "RemoteControl.h"
2+
3+
void printMacAddress(byte mac[]) {
4+
for (int i = 0; i < 6; i++) {
5+
if (i > 0) {
6+
Serial.print(":");
7+
}
8+
if (mac[i] < 16) {
9+
Serial.print("0");
10+
}
11+
Serial.print(mac[i], HEX);
12+
}
13+
Serial.println();
14+
}
15+
16+
void debugNetworkInfo() {
17+
Serial.print("Connected. IP: ");
18+
Serial.println(WiFi.localIP());
19+
20+
Serial.print("SSID: ");
21+
Serial.println(WiFi.SSID());
22+
23+
// print the MAC address of the router you're attached to:
24+
byte bssid[6];
25+
WiFi.BSSID(bssid);
26+
Serial.print("BSSID: ");
27+
printMacAddress(bssid);
28+
29+
// print the received signal strength:
30+
Serial.print("signal strength (RSSI): ");
31+
Serial.println(WiFi.RSSI());
32+
33+
// print the encryption type:
34+
Serial.print("Encryption Type: ");
35+
Serial.println(WiFi.encryptionType(), HEX);
36+
37+
Serial.println("----------------------------------------------");
38+
Serial.println();
39+
}
40+
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() {
51+
if (WiFi.status() == WL_NO_MODULE) {
52+
Serial.println("Communication with WiFi module failed!");
53+
while(true) delay(500);
54+
}
55+
56+
String firmware_version = WiFi.firmwareVersion();
57+
if ( firmware_version < WIFI_FIRMWARE_LATEST_VERSION) {
58+
Serial.print("Latest available version: ");
59+
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
60+
Serial.println("Please upgrade your firmware.");
61+
}
62+
63+
Serial.print("Connecting to ");
64+
Serial.println(_SSID);
65+
66+
int attempts = 0;
67+
while (WL_CONNECTED != WiFi.status()) { // try to connect to the network
68+
attempts++;
69+
Serial.println("Atempt to connect: " + String(attempts));
70+
WiFi.begin(_SSID.c_str(), _SSIDPassword.c_str());
71+
for (int i = 0; i < 50; i++) { // wait for connection
72+
Serial.print(".");
73+
delay(500);
74+
if (WL_CONNECTED == WiFi.status()) break;
75+
}
76+
Serial.println();
77+
Serial.println("Connection status: " + String(WiFi.status()));
78+
}
79+
Serial.println();
80+
81+
debugNetworkInfo();
82+
}
83+
84+
void RemoteControl::setup(RemoteControlRoutesCallback routes) {
85+
_setupNetwork();
86+
routes(_app); // setup routes
87+
_server.begin();
88+
}
89+
90+
void RemoteControl::process() {
91+
// TODO: check if we still have a connection. If not, reconnect.
92+
WiFiClient client = _server.available();
93+
94+
if (client.connected()) {
95+
_app.process(&client);
96+
client.stop();
97+
}
98+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef REMOTECONTROL_H
2+
#define REMOTECONTROL_H
3+
4+
#include <Arduino.h>
5+
#include <WiFiS3.h>
6+
#include <aWOT.h>
7+
8+
// define routes callback function signature
9+
typedef void (*RemoteControlRoutesCallback)(Application &app);
10+
11+
class RemoteControl {
12+
public:
13+
RemoteControl(const char* SSID, const char* SSIDPassword);
14+
~RemoteControl();
15+
void setup(RemoteControlRoutesCallback routes);
16+
void process();
17+
private:
18+
const String _SSID;
19+
const String _SSIDPassword;
20+
WiFiServer _server;
21+
Application _app;
22+
23+
void _setupNetwork();
24+
};
25+
26+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <Arduino.h>
2+
#include "WaterPumpController.h"
3+
4+
WaterPumpController::WaterPumpController(int directionPin, int brakePin, int powerPin) :
5+
_directionPin(directionPin),
6+
_brakePin(brakePin),
7+
_powerPin(powerPin)
8+
{
9+
10+
}
11+
12+
WaterPumpController::~WaterPumpController() {}
13+
14+
void WaterPumpController::setup() {
15+
pinMode(_directionPin, OUTPUT);
16+
pinMode(_brakePin, OUTPUT);
17+
pinMode(_powerPin, OUTPUT);
18+
// TODO: check that its okay to do during setup
19+
stopPump();
20+
}
21+
22+
void WaterPumpController::pour(int milliseconds) {
23+
startPump();
24+
delay(milliseconds);
25+
stopPump();
26+
}
27+
28+
void WaterPumpController::startPump() {
29+
_state = PUMP_ON;
30+
digitalWrite(_brakePin, LOW); // release breaks
31+
analogWrite(_powerPin, 255);
32+
}
33+
34+
void WaterPumpController::stopPump() {
35+
digitalWrite(_brakePin, HIGH); // activate breaks
36+
analogWrite(_powerPin, 0);
37+
_state = PUMP_OFF;
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef WATERPUMPCONTROLLER_H
2+
#define WATERPUMPCONTROLLER_H
3+
4+
class WaterPumpController {
5+
public:
6+
enum EPumpState {
7+
PUMP_OFF,
8+
PUMP_ON
9+
};
10+
private:
11+
const int _directionPin;
12+
const int _brakePin;
13+
const int _powerPin;
14+
const int _maxPower = 255;
15+
EPumpState _state = PUMP_OFF;
16+
public:
17+
WaterPumpController(int directionPin, int brakePin, int powerPin);
18+
~WaterPumpController();
19+
20+
void setup();
21+
void pour(int miliseconds);
22+
void startPump();
23+
void stopPump();
24+
25+
EPumpState state() const { return _state; }
26+
};
27+
28+
#endif // WATERPUMPCONTROLLER_H

controller/tea_poor/platformio.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ platform = renesas-ra
1313
board = uno_r4_wifi
1414
framework = arduino
1515
lib_deps =
16-
bblanchon/ArduinoJson@^6.21.4
1716
lasselukkari/aWOT@^3.5.0

0 commit comments

Comments
 (0)