-
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.
extract RemoteControl class and fix request type to get
- Loading branch information
1 parent
d299c43
commit 73d225f
Showing
3 changed files
with
135 additions
and
92 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,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(); | ||
} | ||
} |
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 |
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