· 6 years ago · Nov 07, 2019, 09:17 AM
1/*
2 Rui Santos
3 Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files.
7
8 The above copyright notice and this permission notice shall be included in all
9 copies or substantial portions of the Software.
10
11 Inital Code provided by Rui Sanotos et al.
12
13 Code further developed by Vikram Nayak and Jayanth Varma @ Katalyst D&E
14
15 +91 9110457911
16
17 katalystautomotive@gmail.com
18
19 Ver: 1.3
20
21 06/11/2019
22
23 formula: [ ((mean(analog_value)/1000)^(-1.16))*60.374 ]
24
25 */
26
27//#include <WiFiClient.h>
28//#include <WebServer.h>
29//#include <ESPmDNS.h>
30//#include <Update.h>
31//#include "soc/rtc.h"
32
33
34#include <WiFi.h>
35#include <HTTPClient.h>
36#define model 20150
37#include "HX711.h"
38
39#define DOUT 22
40#define CLK 23
41
42HX711 scale;
43
44float calibration_factor = -42000; //-7050 worked for my 440lb max scale setup
45float reading = 0;
46
47const char* host = "esp32";
48const char* ssid = "Katalyst_Wifi";
49const char* password = "cmipl1234";
50
51
52
53
54
55// Replace with your network credentials
56
57const char* serverName = "http://katalystpower.com/post-esp-data.php";
58
59// Keep this API Key value to be compatible with the PHP code provided in the project page.
60// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
61String apiKeyValue = "tPmAT5Ab3j7F9";
62
63//String sensorName = "razmaan";
64//String sensorLocation = "fuckyou";
65
66//#include <SharpIR.h>
67float analog_value1 = 0 ;
68float analog_value2 = 0 ;
69float analog_value3 = 0 ;
70float analog_value4 = 0 ;
71int fail_count = 0 ;
72
73int ledPin = 2 ;
74unsigned long previousMillis = 0;
75const long interval = 5000;
76unsigned long currentMillis = 0 ;
77
78unsigned long previousMillis1 = 0;
79unsigned long currentMillis1 = 0;
80int interval1 = 500;
81
82int counter = 0;
83int ledState = LOW;
84//SharpIR mySensor = SharpIR(36, model);
85
86
87void setup() {
88
89 scale.begin(DOUT, CLK);
90 scale.set_scale();
91 // scale.tare();
92 pinMode(5,OUTPUT);
93 digitalWrite(5,HIGH);
94 pinMode(2,OUTPUT);
95 Serial.begin(115200);
96
97 WiFi.begin(ssid, password);
98 Serial.println("Connecting");
99 while(WiFi.status() != WL_CONNECTED) {
100
101
102
103 Serial.print(".");
104 digitalWrite(ledPin,HIGH);
105 delay(150);
106 digitalWrite(ledPin,LOW);
107
108 }
109
110 Serial.println("");
111 Serial.print("Connected to WiFi network with IP Address: ");
112 Serial.println(WiFi.localIP());
113
114
115 }
116
117
118
119
120
121
122void loop() {
123if(fail_count == 5)
124{
125 Serial.println("Fail count exceeded");
126 delay(2000);
127 ESP.restart();
128}
129
130delay(1);
131if(counter >= 50)
132{
133 counter = 0 ;
134 Serial.println("ESP Restart sequence");
135 delay(2000);
136 ESP.restart();
137}
138currentMillis = millis();
139
140 if (currentMillis - previousMillis >= interval) {
141 // save the last time you blinked the LED
142
143 previousMillis = currentMillis;
144 post();
145 // if the LED is off turn it on and vice-versa:
146
147 }
148
149currentMillis1 = millis();
150
151 if (currentMillis1 - previousMillis1 >= interval1) {
152 // save the last time you blinked the LED
153
154 previousMillis1 = currentMillis1;
155 if (ledState == LOW) {
156 ledState = HIGH;
157 } else {
158 ledState = LOW;
159 }
160 // if the LED is off turn it on and vice-versa:
161
162 }
163
164
165 digitalWrite(ledPin, ledState);
166}
167
168void post()
169{
170 //Check WiFi connection status
171
172
173 if(WiFi.status()== WL_CONNECTED){
174
175
176 analog_value1 = analogRead(34);
177 delay(10);
178 analog_value1 = (830/analog_value1)*100 ;
179 analog_value1 = 100 - analog_value1 ;
180
181
182 analog_value2 = analogRead(35);
183 delay(10);
184 analog_value2 = (600/analog_value2)*100 ;
185 analog_value2 = 100 - analog_value2 ;
186
187 analog_value3 = 0;
188
189 scale.set_scale(calibration_factor);
190 reading = scale.get_units() + 0.48 ;
191 Serial.println(reading);
192
193 for(int i = 0; i <= 25; i++)
194 {
195 analog_value3 = analog_value3 + analogRead(36);
196 // delay(10);
197 }
198
199 analog_value3 = analog_value3 / 25 ;
200 analog_value3 = map(analog_value3,825,3175,0,100);
201 //analog_value3 = (pow((analog_value3/1000),-1.16))*60.374 ;
202 //analog_value3 = 60.374 * pow(analog_value3/1000.0, -1.16);
203
204 //analog_value3 = mySensor.distance();
205 delay(10);
206 //analog_value3 = (1261/analog_value3)*100 ;
207 //analog_value3 = 100 - analog_value3 ;
208
209
210 analog_value4 = analogRead(39);
211 delay(10);
212 analog_value4 = (600/analog_value4)*100 ;
213 analog_value4 = 100 - analog_value4 ;
214
215 long rssi = WiFi.RSSI();
216 Serial.print("RSSI:");
217 Serial.println(rssi);
218 interval1 = 1000 ;
219
220
221 HTTPClient http;
222
223 // Your Domain name with URL path or IP address with path
224 http.begin(serverName);
225
226 // Specify content-type header
227 http.addHeader("Content-Type", "application/x-www-form-urlencoded");
228
229 // Prepare your HTTP POST request data
230 String httpRequestData = "api_key=" + apiKeyValue + "&b1wt=" + reading
231 + "&b1px=" + String(analog_value1) + "&b2wt=" + reading
232 + "&b2px=" + String(analog_value2) + "&b3wt=" + reading
233 + "&b3px=" + String(analog_value3) + "&b4wt=" + reading
234 + "&b4px=" + String(analog_value4) + "&b3wt=" + reading
235 + "";
236 Serial.print("httpRequestData: ");
237 Serial.println(httpRequestData);
238
239
240 // You can comment the httpRequestData variable above
241 // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
242 //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
243
244 // Send HTTP POST request
245 int httpResponseCode = http.POST(httpRequestData);
246 if(httpResponseCode != 200)
247 {
248 interval1 = 0 ;
249 }
250
251 // If you need an HTTP request with a content type: text/plain
252 //http.addHeader("Content-Type", "text/plain");
253 //int httpResponseCode = http.POST("Hello, World!");
254
255 // If you need an HTTP request with a content type: application/json, use the following:
256 //http.addHeader("Content-Type", "application/json");
257 //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
258
259 if (httpResponseCode>0) {
260 Serial.print("HTTP Response code: ");
261 Serial.println(httpResponseCode);
262 }
263 else {
264 Serial.print("Error code: ");
265 Serial.println(httpResponseCode);
266 fail_count = fail_count + 1 ;
267 Serial.println(fail_count);
268
269 }
270 // Free resources
271 http.end();
272 }
273 else {
274 Serial.println("WiFi Disconnected");
275 interval1 = 150 ;
276 WiFi.begin(ssid, password);
277 }
278 counter = counter + 1 ;
279}