-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.h
More file actions
173 lines (136 loc) · 4.58 KB
/
setup.h
File metadata and controls
173 lines (136 loc) · 4.58 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************
* Name: setup
*
* Returns: Nothing.
*
* Parameters: None.
*
* Description: Setup for the Arduino.
*
***************************************************/
/* Boot-time OTA */
WebServer server(80);
/* Server Index Page */
const char* serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
int BATT_PULSE = 12;
// const int ledPin = 13; // useful on-board LED
/* Setup outputs */
TaskHandle_t Task0;
void codeForTask0(void * parameter)
{
for(;;){
// code to pulse battery load on Core 0
Serial.print("\n[info] Pulse Core 0 High");
digitalWrite(BATT_PULSE, HIGH);
delay(1400);
Serial.print("\n[info] Pulse Core 0 Low");
digitalWrite(BATT_PULSE, LOW);
delay(5000);
}
}
void setupCores(){
xTaskCreatePinnedToCore(
codeForTask0,
"Task_0",
1000,
NULL,
1,
&Task0,
0);
delay(500);
pinMode (BATT_PULSE, OUTPUT);
}
void setup() {
setupCores();
Wire.begin(); //batt test
/* Initialise serial */
Serial.begin(115200);
/* Setup inputs */
///////////////////////////////////////// rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M); // 80mhz cpu clock
Serial.print("\n[init] Booting..\n[init] Configuring WDT");
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
/* Boot-time connection to WiFi */
esp_wifi_init; // wifi psave
btStop(); // disable bluetooth
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); // psave light
Serial.print("\n[info] First time connect attempt to SSID: ") + String(Serial.print(WIFI_SSID));
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.print("\n[fail] Connection Failed!...");
esp_task_wdt_init(9, true);
Serial.print("\n[fail] No WiFi, setting watchdog of 10 secs. Reset.");
delay(10000);
}
char buf[16]; sprintf(buf, "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3] );
Serial.print("\n[ ok ] WiFi connected. IP address: " + (String)buf + ", RSSI: " + (String)WiFi.RSSI()+"db");
/* return index page which is stored in serverIndex */
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
/* handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware */
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
server.begin();
Serial.print("\nTelegram Command Bot, Version: "+(String)majorVersion+"."+(String)minorVersion)+"\n";
}
int ii = 0; // WDT