Skip to content

Commit

Permalink
#5. Dancebot compiles again with a new robot config and networking ar…
Browse files Browse the repository at this point in the history
…chitecture.
  • Loading branch information
dimembermatt committed Sep 25, 2020
1 parent db641d1 commit e10d492
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 259 deletions.
8 changes: 4 additions & 4 deletions DancebotESP32/src/DancebotESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <Arduino.h>
#include "DemobotAPI.h" // grab abstracted robot API - we fill in this for each robot
#include "WebController.h"

#include "DemobotNetwork.h"

/**
* Example flow for a Demobots sketch with an ESP32:
Expand All @@ -19,14 +19,14 @@
* }
*
* loop() {
* // update robot state
* // look for update from server
* update robot state
* look for update from server
* }
*/

void setup() {
delay(5000); //give time for me to open up serial monitor
setupNetworking();
// setupNetworking();
initializeRobot();
}

Expand Down
2 changes: 1 addition & 1 deletion DancebotESP32/src/DemobotAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void updateState(String state) {
bot->enableDanceRoutine(true);
break;
default:
// Serial.println("Invalid state.");
Serial.println("Invalid state.");
}
}

Expand Down
105 changes: 62 additions & 43 deletions DancebotESP32/src/DemobotNetwork.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
/**
* File: DemobotNetwork.cpp
* Author: Matthew Yu
* Last Modified: 09/20/20
* Last Modified: 09/24/20
* Project: Demobots General
* Organization: UT IEEE RAS
* Description: Implements definitions for the DemobotNetwork class, which allows
* users to identify setup and connect a robot to the network given some credentials.
*/
#include "DemobotNetwork.h"

DemobotNetwork::DemobotNetwork(char* demobotName) {
// redirect any traffic with an unknown address to here
IPAddress gateway(192,168,1,1);
// 255-245-1 = 9 allowed IP addresses on the subnet
IPAddress subnet(255,255,0,0);

DemobotNetwork::DemobotNetwork(String demobotName) {
credentialsLog = new Credential[numCredentials] {
Credential{"Demobot", "Demobots1234"},
Credential{"DemobotsNetwork", "Dem0b0tsRu1e!"},
Credential{"", ""},
Credential{"", ""}
};

http = new HTTPClient();
_demobotName = demobotName;
// set server ip based on robot type
switch(hash(_demobotName)) {
case static_hash("Dancebot1"):
case static_hash("Dancebot2"):
case static_hash("Dancebot3"):
case static_hash("Dancebot4"):
case static_hash("Dancebot5"):
case static_hash("Mothership"):
char* charArray = new char[_demobotName.length() + 1];
_demobotName.toCharArray(charArray, _demobotName.length());
switch(hash(charArray)) {
case 40742: // Dancebot1
case 40998: // Dancebot2
case 41254: // Dancebot3
case 41510: // Dancebot4
case 41766: // Dancebot5
case 111755: // Mothership
_ipaddress = IPAddress(192,168,1,1);
break;
case static_hash("Polargraph"):
case 108262: // Polargraph
_ipaddress = IPAddress(192,168,1,2);
break;
case static_hash("Marquee"):
case 13199: // Marquee
_ipaddress = IPAddress(192,168,1,3);
break;
case static_hash("TowerOfPower"):
case 448950: // TowerOfPower
_ipaddress = IPAddress(192,168,1,4);
break;
default:
_ipaddress = IPAddress(192,168,1,0);
break;
}
// set credentials
Expand All @@ -42,8 +58,8 @@ void DemobotNetwork::reconfigureNetworks() {
if (!_startup) {
// take the first entry (assuming that it's filled) of the
// credentials log
_SSID = credentialsLog[0].SSID;
_PASSWORD = credentialsLog[0].PASSWORD;
_SSID = const_cast<char*>(credentialsLog[0].SSID);
_PASSWORD = const_cast<char*>(credentialsLog[0].PASSWORD);
}

_connected = false;
Expand All @@ -53,7 +69,7 @@ bool DemobotNetwork::connectNetwork() {
// if we never found a network with credentials, don't attempt to connect
if (_SSID == nullptr || _PASSWORD == nullptr) { return false; }

int retry;
int retry = 0;
while (true) {
WiFi.begin(_SSID, _PASSWORD);
// poll until we get connected or get a connection failure
Expand Down Expand Up @@ -85,11 +101,11 @@ bool DemobotNetwork::pingServer() {

// send a root level GET request to see if the page exists
String queryPath = "http://" + IpAddress2String(_ipaddress) + "/";
http.begin(queryPath.c_str());
return http.GET();
http->begin(queryPath.c_str());
return http->GET();
}

int DemobotNetwork::sendGETRequest(String endpoint, String keys[], String vals[], int argSize, char* response) {
int DemobotNetwork::sendGETRequest(String endpoint, String keys[], String vals[], int argSize, String *response) {
// if not connected to a network or doesn't have an ip address TODO: don't
// know if !_ipaddress will work
if (!_connected || !_ipaddress) return -1;
Expand All @@ -106,10 +122,11 @@ int DemobotNetwork::sendGETRequest(String endpoint, String keys[], String vals[]
}
}

http.begin(queryPath.c_str());
int responseCode = http.GET();
http->begin(queryPath.c_str());
int responseCode = http->GET();
if (responseCode > 0) {
response = http.getString();
String str = String(http->getString());
response = &(str);
}
return responseCode;
}
Expand All @@ -120,8 +137,8 @@ int DemobotNetwork::sendPOSTRequest(String endpoint, String keys[], String vals[
if (!_connected || !_ipaddress) return -1;

String queryPath = "http://" + IpAddress2String(_ipaddress) + endpoint;
http.begin(queryPath);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http->begin(queryPath);
http->addHeader("Content-Type", "application/x-www-form-urlencoded");
String data = "";
// append keys and values to the path
for (int i = 0; i < argSize; i++) {
Expand All @@ -133,7 +150,7 @@ int DemobotNetwork::sendPOSTRequest(String endpoint, String keys[], String vals[
}
}

return http.POST(data);
return http->POST(data);
}

char* DemobotNetwork::getNetworkSSID() {
Expand All @@ -144,10 +161,24 @@ char* DemobotNetwork::getNetworkPassword() {
return _PASSWORD;
}

void DemobotNetwork::shutdownNetwork() {
delete[] credentialsLog;
delete http;
}

IPAddress DemobotNetwork::getIPAddress() {
return _ipaddress;
}

String DemobotNetwork::IpAddress2String(const IPAddress ipAddress) {
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}




// Private methods

Expand All @@ -156,43 +187,31 @@ int DemobotNetwork::hash(char* valptr) {
int count = 0;
while(*valptr != '\0') {
// calculate the multiplying power for the digit
int power = 1; // 10E0
int power = 1; // 2^0
for (int i = 0; i < count; i++) {
power *= 10; // 10Ecount
power *= 2; // 2^count
}
tot += *valptr * power);
tot += *valptr * power;
count++;
valptr++;
}
return tot;
}

bool DemobotNetwork::getNetwork(char* ssid, char* password) {
bool DemobotNetwork::getNetwork(char ssid[], char password[]) {
// 1. scan networks
int networks = WiFi.scanNetworks();
if (networks == 0) { return false; }
// 2. assign the relevant network to the robot
for (int i = 0; (i < numCredentials); i++) {
for (int j = 0; j < networks; j++) {
if(WiFi.SSID(j).equals(String(credentialsLog[i].SSID))) {
ssid = credentialsLog[i].SSID;
password = credentialsLog[i].PASSWORD;
ssid = const_cast<char*>(credentialsLog[i].SSID);
password = const_cast<char*>(credentialsLog[i].PASSWORD);
return true;
}
}
}
// 2b. if we didn't find a network, set it to default
return false;
}

/**
* IpAddress2String converts an IPAddress object into a String object.
* Returns a String.
* @author: apicquot from https://forum.arduino.cc/index.php?topic=228884.0
*/
String IpAddress2String(const IPAddress& ipAddress) {
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
}
Loading

0 comments on commit e10d492

Please sign in to comment.