· 6 years ago · Nov 07, 2019, 09:08 AM
1/*
2 Rui Santos
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files.
6
7 The above copyright notice and this permission notice shall be included in all
8 copies or substantial portions of the Software.
9
10 Inital Code provided by Rui Sanotos et al.
11
12 Code further developed by Vikram Nayak and Jayanth Varma @ Katalyst D&E
13
14 +91 9110457911
15
16 katalystautomotive@gmail.com
17
18 Ver: 1.3
19
20 06/11/2019
21
22 formula: [ ((mean(analog_value)/1000)^(-1.16))*60.374 ]
23
24 */
25
26//#include <WiFiClient.h>
27//#include <WebServer.h>
28//#include <ESPmDNS.h>
29//#include <Update.h>
30#include <WiFi.h>
31#include <HTTPClient.h>
32
33#define model 20150
34#include "HX711.h"
35
36#define DOUT1 22
37#define CLK1 23
38/*
39#define DOUT2 22
40#define CLK2 23
41
42#define DOUT3 22
43#define CLK3 23
44
45#define DOUT4 22
46#define CLK4 23
47*/
48HX711 scale1;
49/*
50HX711 scale2;
51HX711 scale3;
52HX711 scale4;
53*/
54float calibration_factor = -42000;
55const char* host = "esp32";
56const char* ssid = "Katalyst_Wifi";
57const char* password = "cmipl1234";
58
59//WebServer server(80);
60
61//Login Page
62/* const char* loginIndex =
63 "<form name='loginForm'>"
64 "<table width='20%' bgcolor='E7D62A' align='center'>"
65 "<tr>"
66 "<td colspan=2>"
67 "<center><font size=4><b>FLip UPDATER</b></font></center>"
68 "<br>"
69 "</td>"
70 "<br>"
71 "<br>"
72 "</tr>"
73 "<td>Username:</td>"
74 "<td><input type='text' size=25 name='userid'><br></td>"
75 "</tr>"
76 "<br>"
77 "<br>"
78 "<tr>"
79 "<td>Password:</td>"
80 "<td><input type='Password' size=25 name='pwd'><br></td>"
81 "<br>"
82 "<br>"
83 "</tr>"
84 "<tr>"
85 "<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
86 "</tr>"
87 "</table>"
88"</form>"
89"<script>"
90 "function check(form)"
91 "{"
92 "if(form.userid.value=='katalyst' && form.pwd.value=='cmipl1234')"
93 "{"
94 "window.open('/serverIndex')"
95 "}"
96 "else"
97 "{"
98 " alert('Error Password or Username')"
99 "}"
100 "}"
101"</script>";
102
103//Server Index page
104const char* serverIndex =
105"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
106"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
107 "<input type='file' name='update'>"
108 "<input type='submit' value='Update'>"
109 "</form>"
110 "<div id='prg'>progress: 0%</div>"
111 "<script>"
112 "$('form').submit(function(e){"
113 "e.preventDefault();"
114 "var form = $('#upload_form')[0];"
115 "var data = new FormData(form);"
116 " $.ajax({"
117 "url: '/update',"
118 "type: 'POST',"
119 "data: data,"
120 "contentType: false,"
121 "processData:false,"
122 "xhr: function() {"
123 "var xhr = new window.XMLHttpRequest();"
124 "xhr.upload.addEventListener('progress', function(evt) {"
125 "if (evt.lengthComputable) {"
126 "var per = evt.loaded / evt.total;"
127 "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
128 "}"
129 "}, false);"
130 "return xhr;"
131 "},"
132 "success:function(d, s) {"
133 "console.log('success!')"
134 "},"
135 "error: function (a, b, c) {"
136 "}"
137 "});"
138 "});"
139 "</script>"; */
140
141const char* serverName = "http://katalystpower.com/post-esp-data.php";
142
143// Keep this API Key value to be compatible with the PHP code provided in the project page.
144// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
145
146String apiKeyValue = "tPmAT5Ab3j7F9";
147float analog_value1 = 0 ;
148float analog_value2 = 0 ;
149float analog_value3 = 0 ;
150float analog_value4 = 0 ;
151int fail_count = 0 ;
152
153float reading1 = 0;
154// float reading2 = 0;
155// float reading3 = 0;
156// float reading4 = 0;
157
158int ledPin = 2 ;
159unsigned long previousMillis = 0;
160const long interval = 5000;
161unsigned long currentMillis = 0 ;
162
163unsigned long previousMillis1 = 0;
164unsigned long currentMillis1 = 0;
165int interval1 = 500;
166int counter = 0;
167int ledState = LOW;
168
169void setup()
170{
171 Serial.begin(115200);
172 WiFi.begin(ssid, password);
173 Serial.println("Connecting");
174
175 while(WiFi.status() != WL_CONNECTED) {
176 Serial.print(".");
177 digitalWrite(ledPin,HIGH);
178 delay(150);
179 digitalWrite(ledPin,LOW);
180}
181 Serial.println("");
182 Serial.print("Connected to WiFi network with IP Address: ");
183 Serial.println(WiFi.localIP());
184
185/* if (!MDNS.begin(host)) { //http://esp32.local
186 Serial.println("Error setting up MDNS responder!");
187 while (1) {
188 delay(1000);
189 }
190 }
191 Serial.println("mDNS responder started");
192*/
193
194 pinMode(5,OUTPUT);
195 digitalWrite(5,HIGH);
196 pinMode(2,OUTPUT);
197
198 scale1.begin(DOUT1, CLK1);
199 scale1.set_scale();
200
201/*
202 scale2.begin(DOUT2, CLK2);
203 scale2.set_scale();
204
205 scale3.begin(DOUT3, CLK3);
206 scale3.set_scale();
207
208 scale4.begin(DOUT4, CLK4);
209 scale4.set_scale();
210*/
211}
212 /*return index page which is stored in serverIndex */
213/* server.on("/", HTTP_GET, []() {
214 server.sendHeader("Connection", "close");
215 server.send(200, "text/html", loginIndex);
216 });
217 server.on("/serverIndex", HTTP_GET, []() {
218 server.sendHeader("Connection", "close");
219 server.send(200, "text/html", serverIndex);
220 });
221 /*handling uploading firmware file //
222 server.on("/update", HTTP_POST, []() {
223 server.sendHeader("Connection", "close");
224 server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
225 ESP.restart();
226 }, []() {
227 HTTPUpload& upload = server.upload();
228 if (upload.status == UPLOAD_FILE_START) {
229 Serial.printf("Update: %s\n", upload.filename.c_str());
230 if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
231 Update.printError(Serial);
232 }
233 } else if (upload.status == UPLOAD_FILE_WRITE) {
234 /* flashing firmware to ESP /
235 if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
236 Update.printError(Serial);
237 }
238 } else if (upload.status == UPLOAD_FILE_END) {
239 if (Update.end(true)) { //true to set the size to the current progress
240 Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
241 } else {
242 Update.printError(Serial);
243 }
244 }
245 });
246 server.begin();
247} */
248
249void loop() {
250if(fail_count == 5)
251{
252 Serial.println("Fail count exceeded");
253 delay(2000);
254 ESP.restart();
255}
256//server.handleClient();
257delay(1);
258if(counter >= 50)
259{
260 counter = 0 ;
261 Serial.println("ESP Restart sequence");
262 delay(2000);
263 ESP.restart();
264}
265currentMillis = millis();
266if (currentMillis - previousMillis >= interval) {
267 // save the last time you blinked the LED
268 previousMillis = currentMillis;
269 post();
270 }
271
272currentMillis1 = millis();
273
274if (currentMillis1 - previousMillis1 >= interval1) {
275 // save the last time you blinked the LED
276 previousMillis1 = currentMillis1;
277 if (ledState == LOW) {
278 ledState = HIGH;
279 } else {
280 ledState = LOW;
281 }
282 }
283 digitalWrite(ledPin, ledState);
284}
285
286void post()
287{
288 //Check WiFi connection status
289if(WiFi.status()== WL_CONNECTED){
290
291//Proximity Sensor 1
292analog_value1 = 0;
293for(int i = 0; i <= 25; i++){
294analog_value1 = analog_value1 + analogRead(34);
295}
296analog_value1 = analog_value1 / 25 ;
297analog_value1 = map(analog_value1,825,3175,0,100);
298delay(10);
299
300//Proximity Sensor 2
301analog_value2 = 0;
302for(int i = 0; i <= 25; i++){
303analog_value2 = analog_value2 + analogRead(35);
304}
305analog_value2 = analog_value2 / 25 ;
306analog_value2 = map(analog_value2,825,3175,0,100);
307delay(10);
308
309//Proximity Sensor 3
310analog_value3 = 0;
311for(int i = 0; i <= 25; i++){
312analog_value3 = analog_value3 + analogRead(36);
313}
314analog_value3 = analog_value3 / 25 ;
315analog_value3 = map(analog_value3,825,3175,0,100);
316delay(10);
317
318//Proximity Sesnor 4
319/*analog_value4 = 0;
320for(int i = 0; i <= 25; i++){
321analog_value4 = analog_value4 + analogRead(39);
322}
323analog_value4 = analog_value4 / 25 ;
324analog_value4 = map(analog_value4,825,3175,0,100);
325delay(10);
326*/
327
328//Load Cell 1
329scale1.set_scale(calibration_factor);
330reading1 = scale1.get_units() + 0.48 ;
331Serial.println(reading1);
332/*
333//Load Cell 2
334scale2.set_scale(calibration_factor);
335reading2 = scale2.get_units() + 0.48 ;
336Serial.println(reading2);
337
338//Load Cell 3
339scale3.set_scale(calibration_factor);
340reading3 = scale3.get_units() + 0.48 ;
341Serial.println(reading3);
342
343//Load Cell 4
344scale4.set_scale(calibration_factor);
345reading4 = scale4.get_units() + 0.48 ;
346Serial.println(reading4);
347*/
348long rssi = WiFi.RSSI();
349Serial.print("RSSI:");
350Serial.println(rssi);
351interval1 = 1000 ;
352
353HTTPClient http;
354
355//Domain name with URL path or IP address with path
356http.begin(serverName);
357// Specify content-type header
358http.addHeader("Content-Type", "application/x-www-form-urlencoded");
359
360String httpRequestData = "api_key=" + apiKeyValue + "&b1wt=" + reading1
361 + "&b1px=" + String(analog_value1) + "&b2wt=" + reading1
362 + "&b2px=" + String(analog_value2) + "&b3wt=" + reading1
363 + "&b3px=" + String(analog_value3) + "&b4wt=" + reading1
364 + "&b4px=" + String(analog_value4) + "";
365Serial.print("httpRequestData: ");
366Serial.println(httpRequestData);
367// Send HTTP POST request
368int httpResponseCode = http.POST(httpRequestData);
369if(httpResponseCode != 200){
370 interval1 = 0 ;
371}
372// If you need an HTTP request with a content type: application/json, use the following:
373//http.addHeader("Content-Type", "application/json");
374//int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
375if (httpResponseCode>0) {
376 Serial.print("HTTP Response code: ");
377 Serial.println(httpResponseCode);
378}
379else {
380 Serial.print("Error code: ");
381 Serial.println(httpResponseCode);
382 fail_count = fail_count + 1 ;
383 Serial.println(fail_count);
384 }
385// Free resources
386 http.end();
387 }
388else {
389 Serial.println("WiFi Disconnected");
390 interval1 = 150 ;
391 WiFi.begin(ssid, password);
392}
393 counter = counter + 1 ;
394}