Skip to content

Commit

Permalink
extract RemoteControl class and fix request type to get
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Dec 25, 2023
1 parent d299c43 commit 73d225f
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 92 deletions.
99 changes: 99 additions & 0 deletions controller/tea_poor/lib/RemoteControl/RemoteControl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#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.");
while(true) delay(500);
}

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
102 changes: 10 additions & 92 deletions controller/tea_poor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,90 +1,18 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFiS3.h>
#include <aWOT.h>
#include <WaterPumpController.h>
#include <RemoteControl.h>

// Setting up water pump
WaterPumpController waterPumpController(12, 9, 3);
// Just for safety reasons, we don't want to pour tea for too long
// Their is no reason to make it configurable and add unnecessary complexity
const int WATER_PUMP_SAFE_THRESHOLD = 10 * 1000;

// setting up WiFi
const char *SSID = "MyWiFiNetwork";
const char *PWD = "VerySecurePassword";

// minimalistic webserver
WiFiServer server(80);
Application app;

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 printCurrentNet() {
// print the SSID of the network you're attached to:
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:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

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

void connectToWiFi() {
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// block further activity
while(true) delay(500);
}

// info about your adapter
String firmware_version = WiFi.firmwareVersion();

Serial.print("WiFi Firmware Version: ");
Serial.println(firmware_version);
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);

WiFi.begin(SSID, PWD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.print("Connected. IP: ");
Serial.println(WiFi.localIP());
printCurrentNet();
}
// setting up remote control
RemoteControl remoteControl(
"MyWiFiNetwork", // network name/SSID
"VerySecurePassword" // network password
);

bool isValidIntNumber(const char *str, const int maxValue, const int minValue=0) {
if (strlen(str) <= 0) return false;
Expand Down Expand Up @@ -116,21 +44,11 @@ void pour_tea(Request &req, Response &res) {
void setup() {
Serial.begin(9600);
waterPumpController.setup();

// connect to WiFi
connectToWiFi();

// Set endpoints
app.post("/pour_tea", &pour_tea);
// setup Server
server.begin();
remoteControl.setup([](Application &app) {
app.get("/pour_tea", pour_tea);
});
}

void loop() {
WiFiClient client = server.available();

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

0 comments on commit 73d225f

Please sign in to comment.