Skip to content

Commit

Permalink
Gw dev (#1)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
psmgeelen and GreenWizard2015 authored Dec 27, 2023
1 parent a3b7171 commit 090fd4e
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 189 deletions.
5 changes: 5 additions & 0 deletions controller/tea_poor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
98 changes: 98 additions & 0 deletions controller/tea_poor/lib/RemoteControl/RemoteControl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "RemoteControl.h"

void printMacAddress(byte mac[]) {
for (int i = 0; i < 6; i++) {
if (i > 0) {
Serial.print(":");
}
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
}
Serial.println();
}

void debugNetworkInfo() {
Serial.print("Connected. IP: ");
Serial.println(WiFi.localIP());

Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);

// print the received signal strength:
Serial.print("signal strength (RSSI): ");
Serial.println(WiFi.RSSI());

// print the encryption type:
Serial.print("Encryption Type: ");
Serial.println(WiFi.encryptionType(), HEX);

Serial.println("----------------------------------------------");
Serial.println();
}

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

RemoteControl::~RemoteControl() {
}

void RemoteControl::_setupNetwork() {
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while(true) delay(500);
}

String firmware_version = WiFi.firmwareVersion();
if ( firmware_version < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.print("Latest available version: ");
Serial.println(WIFI_FIRMWARE_LATEST_VERSION);
Serial.println("Please upgrade your firmware.");
}

Serial.print("Connecting to ");
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());
for (int i = 0; i < 50; i++) { // wait for connection
Serial.print(".");
delay(500);
if (WL_CONNECTED == WiFi.status()) break;
}
Serial.println();
Serial.println("Connection status: " + String(WiFi.status()));
}
Serial.println();

debugNetworkInfo();
}

void RemoteControl::setup(RemoteControlRoutesCallback routes) {
_setupNetwork();
routes(_app); // setup routes
_server.begin();
}

void RemoteControl::process() {
// TODO: check if we still have a connection. If not, reconnect.
WiFiClient client = _server.available();

if (client.connected()) {
_app.process(&client);
client.stop();
}
}
26 changes: 26 additions & 0 deletions controller/tea_poor/lib/RemoteControl/RemoteControl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef REMOTECONTROL_H
#define REMOTECONTROL_H

#include <Arduino.h>
#include <WiFiS3.h>
#include <aWOT.h>

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

class RemoteControl {
public:
RemoteControl(const char* SSID, const char* SSIDPassword);
~RemoteControl();
void setup(RemoteControlRoutesCallback routes);
void process();
private:
const String _SSID;
const String _SSIDPassword;
WiFiServer _server;
Application _app;

void _setupNetwork();
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <Arduino.h>
#include "WaterPumpController.h"

WaterPumpController::WaterPumpController(int directionPin, int brakePin, int powerPin) :
_directionPin(directionPin),
_brakePin(brakePin),
_powerPin(powerPin)
{

}

WaterPumpController::~WaterPumpController() {}

void WaterPumpController::setup() {
pinMode(_directionPin, OUTPUT);
pinMode(_brakePin, OUTPUT);
pinMode(_powerPin, OUTPUT);
// TODO: check that its okay to do during setup
stopPump();
}

void WaterPumpController::pour(int milliseconds) {
startPump();
delay(milliseconds);
stopPump();
}

void WaterPumpController::startPump() {
_state = PUMP_ON;
digitalWrite(_brakePin, LOW); // release breaks
analogWrite(_powerPin, 255);
}

void WaterPumpController::stopPump() {
digitalWrite(_brakePin, HIGH); // activate breaks
analogWrite(_powerPin, 0);
_state = PUMP_OFF;
}
28 changes: 28 additions & 0 deletions controller/tea_poor/lib/WaterPumpController/WaterPumpController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef WATERPUMPCONTROLLER_H
#define WATERPUMPCONTROLLER_H

class WaterPumpController {
public:
enum EPumpState {
PUMP_OFF,
PUMP_ON
};
private:
const int _directionPin;
const int _brakePin;
const int _powerPin;
const int _maxPower = 255;
EPumpState _state = PUMP_OFF;
public:
WaterPumpController(int directionPin, int brakePin, int powerPin);
~WaterPumpController();

void setup();
void pour(int miliseconds);
void startPump();
void stopPump();

EPumpState state() const { return _state; }
};

#endif // WATERPUMPCONTROLLER_H
1 change: 0 additions & 1 deletion controller/tea_poor/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ platform = renesas-ra
board = uno_r4_wifi
framework = arduino
lib_deps =
bblanchon/ArduinoJson@^6.21.4
lasselukkari/aWOT@^3.5.0
Loading

0 comments on commit 090fd4e

Please sign in to comment.