Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WIFISerialCommunication #106

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions firmware/lucidgloves-firmware/AdvancedConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//Comm defines, no touchy
#define COMM_SERIAL 0
#define COMM_BTSERIAL 1
#define COMM_WIFISERIAL 2

//Bluetooth and WIFI advanced settings
#define ECHO_WIRELESS_DATA false//Should the bluetooth or wifi data be echoed over serial for debugging
REXXER301 marked this conversation as resolved.
Show resolved Hide resolved

//Encoding
#define ENCODING 1
Expand Down
65 changes: 65 additions & 0 deletions firmware/lucidgloves-firmware/WIFISerialCommunication.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//only compiles if WIFISerial is set because it won't compile for a non-compatible board
#if COMMUNICATION == COMM_WIFISERIAL
#include <WiFi.h>
#include <HTTPClient.h>

class WIFISerialCommunication : public ICommunication {

private:
bool m_isOpen;
WiFiClient client;

void connect() {
while (!client.connect(host, port)) //blocks thread until connected
delay(500);
Serial.print("Connected to Server at" + String(client.localIP()) + ":" + String(client.localPort()));
}

public:
WIFISerialCommunication() {
m_isOpen = false;
}

bool isOpen() {
return m_isOpen;
}

void start() {
Serial.begin(SERIAL_BAUD_RATE);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
m_isOpen = true;
Serial.println("Connected to WiFi");

connect();

}
void output(char* data) {
if (!client.connected()) {
Serial.println("Client not connected, attempting reconnect");
connect();
}
client.println(data);
#if ECHO_WIRELESS_DATA
Serial.print(data);
Serial.flush();
#endif
}

bool readData(char* input) {
if (!client.connected()) {
Serial.println("Client not connected, attempting reconnect");
connect();
}
String message = client.readStringUntil('\n');
// String message = client.readStringUntil('\n', input, 100);
//input[size] = NULL;
strcpy(input, message.c_str());

return input != NULL && strlen(input) > 0;
}
};
#endif
3 changes: 2 additions & 1 deletion firmware/lucidgloves-firmware/_main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ void setup() {
comm = new SerialCommunication();
#elif COMMUNICATION == COMM_BTSERIAL
comm = new BTSerialCommunication();
#endif
#elif COMMUNICATION == COMM_WIFISERIAL
comm = new WIFISerialCommunication();
comm->start();

setupInputs();
Expand Down
13 changes: 12 additions & 1 deletion firmware/lucidgloves-firmware/lucidgloves-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@

//This is the configuration file, main structure in _main.ino
//CONFIGURATION SETTINGS:
#define COMMUNICATION COMM_SERIAL //Which communication protocol to use
#define COMMUNICATION COMM_SERIAL //Which communication protocol to use.
//Options are:COMM_SERIAL (usb), COMM_BTSERIAL (bluetooth), COMM_WIFISERIAL (wifi)


REXXER301 marked this conversation as resolved.
Show resolved Hide resolved
//serial over USB
#define SERIAL_BAUD_RATE 115200

//serial over Bluetooth
#define BTSERIAL_DEVICE_NAME "lucidgloves-left"

//serial over WIFI
#if COMMUNICATION == COMM_WIFISERIAL
const char* ssid = "Your_Wifi_SSID";
const char* password = "Your_Wifi_Password";
const char* host = "Your_host_ip";
const int port = 65432; //port of the tcp server
#endif

//ANALOG INPUT CONFIG
#define FLIP_POTS false //Flip values from potentiometers (for fingers!) if they are backwards

Expand Down