-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
a3b7171
commit 090fd4e
Showing
7 changed files
with
232 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
38 changes: 38 additions & 0 deletions
38
controller/tea_poor/lib/WaterPumpController/WaterPumpController.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
controller/tea_poor/lib/WaterPumpController/WaterPumpController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.