@@ -2,12 +2,13 @@ import React from 'react';
2
2
import Popup from 'react-popup' ;
3
3
import './App.css' ;
4
4
5
- var myESP32 = 'e804b643 -6ce7-4e81-9f8a-ce0f699085eb'
5
+ var myESP32 = 'd804b643 -6ce7-4e81-9f8a-ce0f699085eb'
6
6
7
- var otaServiceUuid = 'b8659210 -af91-4ad3-a995-a58d6fd26145'
8
- var versionCharacteristicUuid = 'b8659212 -af91-4ad3-a995-a58d6fd26145'
9
- var fileCharacteristicUuid = 'b8659211 -af91-4ad3-a995-a58d6fd26145'
7
+ var otaServiceUuid = 'c8659210 -af91-4ad3-a995-a58d6fd26145'
8
+ var versionCharacteristicUuid = 'c8659212 -af91-4ad3-a995-a58d6fd26145'
9
+ var fileCharacteristicUuid = 'c8659211 -af91-4ad3-a995-a58d6fd26145'
10
10
11
+ let esp32Device = null ;
11
12
let esp32Service = null ;
12
13
let readyFlagCharacteristic = null ;
13
14
let dataToSend = null ;
@@ -26,8 +27,6 @@ const characteristicSize = 512;
26
27
27
28
/* BTConnect
28
29
* Brings up the bluetooth connection window and filters for the esp32
29
- * TODO:
30
- * 10-Add handler for disconnect so page doesn't crash
31
30
*/
32
31
function BTConnect ( ) {
33
32
navigator . bluetooth . requestDevice ( {
@@ -36,7 +35,11 @@ function BTConnect(){
36
35
} ] ,
37
36
optionalServices : [ otaServiceUuid ]
38
37
} )
39
- . then ( device => device . gatt . connect ( ) )
38
+ . then ( device => {
39
+ esp32Device = device ;
40
+ esp32Device . addEventListener ( 'gattserverdisconnected' , onDisconnected ) ;
41
+ return esp32Device . gatt . connect ( )
42
+ } )
40
43
. then ( server => server . getPrimaryService ( otaServiceUuid ) )
41
44
. then ( service => {
42
45
esp32Service = service ;
@@ -50,6 +53,29 @@ function BTConnect(){
50
53
. catch ( error => { console . log ( error ) ; } ) ;
51
54
}
52
55
56
+ /* onDisconnected(event)
57
+ * If the device becomes disconnected, prompt the user to reconnect.
58
+ */
59
+ function onDisconnected ( event ) {
60
+ Popup . create ( {
61
+ content : esp32Device . name + ' is disconnected, would you like to reconnect?' ,
62
+ buttons : {
63
+ left : [ {
64
+ text : 'Yes' ,
65
+ action : function ( ) {
66
+ Popup . close ( ) ;
67
+ BTConnect ( ) ;
68
+ }
69
+ } ] ,
70
+ right : [ {
71
+ text : 'No' ,
72
+ action : function ( ) {
73
+ Popup . close ( ) ;
74
+ }
75
+ } ]
76
+ }
77
+ } )
78
+ }
53
79
54
80
/* CheckVersion()
55
81
* Grab most current version from Github and Server, if they don't match, prompt the user for firmware update
@@ -66,7 +92,7 @@ function CheckVersion(){
66
92
softwareVersion = 'v' + value . getUint8 ( 2 ) + '.' + value . getUint8 ( 3 ) + '.' + value . getUint8 ( 4 ) ;
67
93
} )
68
94
//Grab our version numbers from Github
69
- . then ( _ => fetch ( 'https://raw.githubusercontent.com/sparkfun/ESP32_OTA_BLE_React_WebApp_Demo/master/version.json' ) )
95
+ . then ( _ => fetch ( 'https://raw.githubusercontent.com/sparkfun/ESP32_OTA_BLE_React_WebApp_Demo/master/GithubRepo/ version.json' ) )
70
96
. then ( function ( response ) {
71
97
// The API call was successful!
72
98
return response . json ( ) ;
@@ -83,35 +109,31 @@ function CheckVersion(){
83
109
var softwareVersionCount = 0 ;
84
110
latestCompatibleSoftware = data . firmware [ softwareVersionCount ] [ 'software' ] ;
85
111
versionFindLoop:
86
- while ( latestCompatibleSoftware !== undefined ) {
112
+ while ( latestCompatibleSoftware !== ' undefined' ) {
87
113
var compatibleHardwareVersion = "N/A"
88
114
var hardwareVersionCount = 0 ;
89
- while ( compatibleHardwareVersion !== undefined ) {
115
+ while ( compatibleHardwareVersion !== ' undefined' ) {
90
116
compatibleHardwareVersion = data . firmware [ softwareVersionCount ] [ 'hardware' ] [ hardwareVersionCount ++ ] ;
91
117
if ( compatibleHardwareVersion === currentHardwareVersion )
92
118
{
93
119
latestCompatibleSoftware = data . firmware [ softwareVersionCount ] [ 'software' ] ;
94
- console . log ( latestCompatibleSoftware ) ;
95
- PromptUserForUpdate ( ) ;
120
+ if ( latestCompatibleSoftware !== softwareVersion )
121
+ {
122
+ console . log ( latestCompatibleSoftware ) ;
123
+ PromptUserForUpdate ( ) ;
124
+ }
96
125
break versionFindLoop;
97
126
}
98
127
}
99
128
softwareVersionCount ++ ;
100
129
}
101
- console . log ( "Uh oh, no compatible software found" ) ;
102
130
}
103
131
} )
104
132
. catch ( error => { console . log ( error ) ; } ) ;
105
133
}
106
134
107
135
/* PromptUserForUpdate()
108
- * Downloads the firmware based on the hardware version and latest software version
109
- * TODO:
110
- * 7-Make separate public repo for firmware and patchnotes
111
- * 5-Make github folder structure for different hardware versions
112
- * 6-Look at the specific release that matches software we want to switch to (Have html adapt to changing HW and SW versions)
113
- * 4-Get patchnotes from github release page(???) and display them with update prompt
114
- * 6-Use CSS to turn Popup into actual popup
136
+ * Asks the user if they want to update, if yes downloads the firmware based on the hardware version and latest software version and begins sending
115
137
*/
116
138
function PromptUserForUpdate ( ) {
117
139
Popup . create ( {
@@ -120,7 +142,7 @@ function PromptUserForUpdate(){
120
142
left : [ {
121
143
text : 'Yes' ,
122
144
action : function ( ) {
123
- fetch ( 'https://raw.githubusercontent.com/AndyEngland521/Tet/master/Firmware/main/compiled/v1_3 .bin' )
145
+ fetch ( 'https://raw.githubusercontent.com/sparkfun/ESP32_OTA_BLE_React_WebApp_Demo/' + latestCompatibleSoftware + '/GithubRepo/' + currentHardwareVersion + ' .bin')
124
146
. then ( function ( response ) {
125
147
return response . arrayBuffer ( ) ;
126
148
} )
@@ -136,7 +158,6 @@ function PromptUserForUpdate(){
136
158
right : [ {
137
159
text : 'No' ,
138
160
action : function ( ) {
139
- /** Close this popup. Close will always close the current visible one, if one is visible */
140
161
Popup . close ( ) ;
141
162
}
142
163
} ]
@@ -147,9 +168,6 @@ function PromptUserForUpdate(){
147
168
/* SendFileOverBluetooth(data)
148
169
* Figures out how large our update binary is, attaches an eventListener to our dataCharacteristic so the Server can tell us when it has finished writing the data to memory
149
170
* Calls SendBufferedData(), which begins a loop of write, wait for ready flag, write, wait for ready flag...
150
- * TODO:
151
- * 8-Disconnect the eventListener once the update is complete
152
- * 7-Call SendBufferedData from promise returned by addEventListener or startNotifications (???Slight reliability improvement???)
153
171
*/
154
172
function SendFileOverBluetooth ( ) {
155
173
if ( ! esp32Service )
@@ -162,7 +180,6 @@ function SendFileOverBluetooth() {
162
180
remaining = totalSize ;
163
181
amountToWrite = 0 ;
164
182
currentPosition = 0 ;
165
-
166
183
esp32Service . getCharacteristic ( fileCharacteristicUuid )
167
184
. then ( characteristic => {
168
185
readyFlagCharacteristic = characteristic ;
@@ -195,6 +212,15 @@ function SendBufferedData() {
195
212
console . log ( "remaining: " + remaining ) ;
196
213
esp32Service . getCharacteristic ( fileCharacteristicUuid )
197
214
. then ( characteristic => RecursiveSend ( characteristic , dataToSend ) )
215
+ . then ( _ => {
216
+ if ( remaining === 0 )
217
+ {
218
+ esp32Device . gatt . disconnect ( )
219
+ }
220
+ } )
221
+ . then ( _ => {
222
+ return document . getElementById ( 'completion' ) . innerHTML = ( 100 * ( currentPosition / totalSize ) ) . toPrecision ( 3 ) + '%' ;
223
+ } )
198
224
. catch ( error => {
199
225
console . log ( error ) ;
200
226
} ) ;
@@ -223,6 +249,7 @@ return (
223
249
< Popup />
224
250
< p id = "hw_version" > Hardware: Not Connected</ p >
225
251
< p id = "sw_version" > Software: Not Connected</ p >
252
+ < p id = "completion" > </ p >
226
253
< button id = "connect"
227
254
onClick = { BTConnect }
228
255
>
0 commit comments