This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaphyEspOta.cpp
115 lines (98 loc) · 3.28 KB
/
LeaphyEspOta.cpp
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
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoWebsockets.h>
#include <ESP8266httpUpdate.h>
#include <jsonlib.h>
#include "LeaphyEspOta.h"
using namespace websockets;
// The url is injected just before compilation time
const char* ServerUrl;
//const char* ServerUrl = "wss://6lge1rqji3.execute-api.eu-west-1.amazonaws.com/test/";
WebsocketsClient wsclient;
boolean wsConnected = false;
WiFiClient wificlient;
String robotId;
LeaphyEspOta::LeaphyEspOta(char* serverUrl)
{
ServerUrl = serverUrl;
}
void setupWifi(){
robotId = WiFi.macAddress();
robotId.replace(":", "");
WiFiManager wifiManager;
char* ssid = "Leaphy-";
strcat(ssid, robotId.c_str());
Serial.print("Generated ssid: ");
Serial.println(ssid);
// TODO: Show ssid in screen
wifiManager.autoConnect(ssid);
}
void onMessageCallback(WebsocketsMessage message) {
Serial.print("Got Message: ");
String messageData = message.data();
Serial.println(messageData);
String event = jsonExtract(messageData, "event");
// If pairing code, save and show it on the screen
if(event == "PAIRINGCODE_UPDATED"){
String pairingCode = jsonExtract(messageData, "message");
} else if(event == "BINARY_PUBLISHED"){
String registerMessage = "{ \"action\": \"update-robot\", \"robotId\": \"" + robotId + "\"}";
wsclient.send(registerMessage);
String s3Location = jsonExtract(messageData, "message");
t_httpUpdate_return ret = ESPhttpUpdate.update(wificlient, s3Location);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
}
void onEventsCallback(WebsocketsEvent event, String data);
void setupWS(){
// Setup Callbacks
wsclient.onMessage(onMessageCallback);
wsclient.onEvent(onEventsCallback);
// Connect to server
wsclient.connect(ServerUrl);
}
void onEventsCallback(WebsocketsEvent event, String data) {
if(event == WebsocketsEvent::ConnectionOpened) {
wsConnected = true;
Serial.println("Connnection Opened, registering robot");
// Register the robot with its mac address
String registerMessage = "{ \"action\": \"register-robot\", \"robotId\": \"" + robotId + "\"}";
wsclient.send(registerMessage);
} else if(event == WebsocketsEvent::ConnectionClosed) {
Serial.println("Connnection Closed");
wsConnected = false;
} else if(event == WebsocketsEvent::GotPing) {
Serial.println("Got a Ping!");
} else if(event == WebsocketsEvent::GotPong) {
Serial.println("Got a Pong!");
}
}
void LeaphyEspOta::setupOta(){
Serial.begin(115200);
Serial.println("Starting OTA Setup");
setupWifi();
Serial.println("Connected to WiFi, connecting to WS API");
setupWS();
}
void LeaphyEspOta::handleLoop(){
// Handle the WS stuff
if(wsConnected){
wsclient.poll();
} else {
Serial.println("Reopening Connection");
wsclient = WebsocketsClient();
setupWS();
}
}