-
Notifications
You must be signed in to change notification settings - Fork 1
/
commutr-server.ino
133 lines (109 loc) · 3.33 KB
/
commutr-server.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
}
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include "./DNSServer.h" // Patched lib
#include "./Shared.h"
#include "index.h"
using std::vector;
#define NAME "Commutr"
const char * ssid = "eduroam";
const char * username = "az2lou@uwaterloo.ca";
const char * password = "";
WiFiClient wifi;
HTTPClient http;
const byte DNS_PORT = 53; // Capture DNS requests on port 53
const byte ACTIVITY_LED = 2;
const byte ACTIVITY_REVERSE = 1; // turn off when active, not on.. needed for me
IPAddress APIP(10, 10, 10, 1); // Private network for server
// state:
vector<Message> messages;
vector<String> usernames = {"John Doe"};
// standard api servers
DNSServer dnsServer;
ESP8266WebServer webServer(80);
String index() {
return home_page;
}
void setup() {
Serial.begin(115200);
// Onboard Blue Light
pinMode(0, OUTPUT);
digitalWrite(0, LOW);
// Connection Status
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
// Push Button
pinMode(5, INPUT);
// Both Station and Access Point
WiFi.mode(WIFI_AP_STA);
// Connect to WiFi
Serial.println("Connecting to: ");
Serial.println(ssid);
wifi_station_disconnect();
struct station_config wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, ssid);
strcpy((char*)wifi_config.password, password);
wifi_station_set_config(&wifi_config);
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();
wifi_station_set_wpa2_enterprise_auth(1);
wifi_station_set_enterprise_identity((uint8*)username, strlen(username));
wifi_station_set_enterprise_username((uint8*)username, strlen(username));
wifi_station_set_enterprise_password((uint8*)password, strlen(password));
wifi_station_connect();
while(WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.println(WiFi.localIP());
}
Serial.println();
digitalWrite(4, HIGH);
// Local access point
WiFi.softAPConfig(APIP, APIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(NAME);
dnsServer.start(DNS_PORT, "commutr.com", APIP);
webServer.on("/", []() {
webServer.send(200, "text/html", index());
});
webServer.on("/messages", HTTP_POST, []() {
Message m = parseMessage(webServer.arg("plain"));
messages.push_back(m);
webServer.send(200);
});
webServer.on("/messages", HTTP_GET, []() {
webServer.send(200, "application/json", toJSON(messages));
});
webServer.onNotFound([]() {
webServer.send(404);
});
webServer.begin();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
if(digitalRead(5) == HIGH) {
Serial.println("Backing up to the server!");
http.addHeader("Content-Type", "application/json");
const int capacity = JSON_OBJECT_SIZE(10);
Serial.println(messages.size());
for(int i = 0; i < messages.size(); ++i) {
http.begin(wifi, "http://100.64.219.109:5000/");
Message m = messages[i];
StaticJsonDocument<capacity> document;
document["username"] = m.username;
document["text"] = m.text;
document["id"] = m.id;
char message[400];
serializeJson(document, message, sizeof(message));
Serial.println(message);
int httpCode = http.POST(message);
http.end();
}
delay(2000);
}
}