-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyEsp32_Pressure.ino
310 lines (235 loc) · 7.66 KB
/
PyEsp32_Pressure.ino
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ includes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "config.h"
#include <Wire.h>
#include "Adafruit_ADS1X15.h"
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ definitions ~~~~~~~~~~~~~~~~~~~~~~~~~~~
#define AnalogPin 36 // ADC pin
#define WifiSwitch 22 // Wifi select pin
#define Verbose false // Verbose
#define WifiSleep 5000 // 3 seconds sleep between wifi pings
#define AcqBuffer 10000 // 10 seconds with no serial communication
#define WifiLED 33 // pin for the wifi status led
#define ledblink 100 // blink period of the led
#define CorrectLin 0.5328 //
#define CorrectAng 0.0036 //
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ variable ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool acquisition = false;
bool WifiEnable = false;
bool WifiInit = false;
int count = 0;
int request = 0;
int measured = 0;
double DataTask1 = 0;
double DataTask2 = 0;
double CorLinCoef = 0;
double CorAngCoef = 1;
unsigned long time_acquisition = 0;
// AdafruitIO_Feed *counter = io.feed("pressure");
TaskHandle_t Task1;
TaskHandle_t Task2;
Adafruit_ADS1115 ads1115_01;
Adafruit_ADS1115 ads1115_02;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Core1StartMessage(bool Verb)
{
if (Verb)
{
Serial.println("");
Serial.println("~~~~~~~~~~~ starting the core 0 test ~~~~~~~~~~~");
Serial.print("Running on core: ");
Serial.println(xPortGetCoreID());
Serial.print("Connecting to IO: ");
}
}
void Core1StopMessage(bool Verb, double value)
{
if (Verb)
{
Serial.println(". Connection was sucessfull.");
Serial.print("sending data -> ");
Serial.println(value);
Serial.println("~~~~~~~~~~~ finishing the core 0 test ~~~~~~~~~~~");
}
}
int Core1Sampling()
{
if (!acquisition)
{
return analogRead(AnalogPin);
}
else
return DataTask1;
}
void Core0Message(bool Verb, double value)
{
if (Verb)
{
Serial.print("I'm doing something at core ");
Serial.println(xPortGetCoreID());
Serial.print(value);
Serial.println(" adc channels.");
}
else
{
Serial.print(int(value));
Serial.print("\n");
}
}
void acquisition_status()
{
unsigned long int current_time = millis();
if (time_acquisition - current_time > AcqBuffer)
{
acquisition = false;
}
}
double calibrate(int value)
{
return value * CorrectAng + CorrectLin;
}
void check_connection()
{
// int counter = 0;
// while(io.status() < AIO_CONNECTED)
// {
// Serial.print(".");
// digitalWrite(WifiLED, LOW);
// delay(250);
// digitalWrite(WifiLED, HIGH);
// delay(250);
// counter++;
// if (counter>30)
// {
// Serial.print("Timeout! Restarting the board...");
// ESP.restart();
// }
// }
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ Task1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void Task1code(void * parameters)
{
for(;;)
{
while(!Serial.available())
{
delay(10);
}
request = Serial.readString().toInt();
int16_t adc;
switch (request)
{
case 0:
// adc = ads1115_01.readADC_SingleEnded(0);
adc=ads1115_01.readADC_Differential_0_1( );
Serial.print(ads1115_01.computeVolts(adc), 4);
break;
case 1:
adc = ads1115_01.readADC_SingleEnded(1);
Serial.print(ads1115_01.computeVolts(adc), 4);
break;
case 2:
adc = ads1115_01.readADC_SingleEnded(2);
Serial.print(ads1115_01.computeVolts(adc), 4);
break;
case 3:
adc = ads1115_01.readADC_SingleEnded(3);
Serial.print(ads1115_01.computeVolts(adc), 4);
break;
case 4:
adc = ads1115_02.readADC_SingleEnded(0);
Serial.print(ads1115_02.computeVolts(adc), 4);
break;
case 5:
adc = ads1115_02.readADC_SingleEnded(1);
Serial.print(ads1115_02.computeVolts(adc), 4);
break;
case 6:
adc = ads1115_02.readADC_SingleEnded(2);
Serial.print(ads1115_02.computeVolts(adc), 4);
break;
case 7:
adc = ads1115_02.readADC_SingleEnded(3);
Serial.print(ads1115_02.computeVolts(adc), 4);
break;
default:
adc = 666;
Serial.print("Error in the request: ");
Serial.print(request);
break;
}
Serial.print("\n");
delay(10);
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void setup()
{
pinMode(WifiLED, OUTPUT);
digitalWrite(WifiLED, LOW);
pinMode(WifiSwitch, INPUT);
delay(10);
WifiEnable = false;// digitalRead(WifiSwitch);
Serial.begin(115200);
Serial.setTimeout(1);
ads1115_01.begin(0x49);
ads1115_02.begin(0x48);
Serial.println("");
Serial.println("~~~~~~~~~~~ starting the setup ~~~~~~~~~~~");
Serial.print("Running on core: ");
Serial.println(xPortGetCoreID());
if(WifiEnable)
{
// WifiInit = true;
// Serial.print("Connecting to Adafruit IO... ");
// io.connect();
// check_connection();
// Serial.println("done.");
// Serial.println(io.statusText());
}
else
Serial.println("Wifi data logging is off");
Serial.println("~~~~~~~~~~~ finishing the setup ~~~~~~~~~~~");
Serial.print("Creating the core 0 task: ");
xTaskCreatePinnedToCore(Task1code,"Task1",10000,NULL,100,&Task1,0);
Serial.println("done.");
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~ LOOP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void loop()
{
WifiEnable = false;//digitalRead(WifiSwitch);
if (WifiInit == true && WifiEnable == false)
{
// Serial.println("Disconnecting from Adafruit IO.");
// WifiInit == false;
// digitalWrite(WifiLED,LOW);
// Serial.println("done.");
}
if (WifiEnable)
{
// if (!WifiInit)
// {
// Serial.print("Connecting to Adafruit IO... ");
// io.connect();
// }
acquisition_status();
check_connection();
DataTask2 = Core1Sampling();
DataTask2 = calibrate(DataTask2);
DataTask2 = pow(10, (DataTask2/0.6)-12);
Core1StartMessage(Verbose);
// io.run(1,true);
// counter->save(DataTask2);
digitalWrite(WifiLED, LOW);
delay(ledblink);
digitalWrite(WifiLED, HIGH);
Core1StopMessage(Verbose, DataTask2);
}
delay(WifiSleep);
}