-
Notifications
You must be signed in to change notification settings - Fork 1
/
software.ino
476 lines (399 loc) · 15 KB
/
software.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/********************
* Here's my Code for Water Quality Device Monitoring
* This Arduino code will collect ph and temp data from MKR1000
* and send them over Samsung ARTIK Cloud API
*
* Author: Jeff Paredes
* Version 1.0
*
********************/
#include <FlashStorage.h>
#include <Thread.h>
#include <WiFi101.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Thread where reading sensors run
Thread myThread = Thread();
// Thread where to count send interval
Thread timeCounter = Thread();
/*
* Temperature Sensor Initialization
*/
#define ONE_WIRE_BUS 1 // Data wire is plugged into digital port 1 of Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire
//devices (not just Maxim/Dallas temperature ICs)
DallasTemperature tempSensor(&oneWire); // Pass our oneWire reference to Dallas Temperature.
/**
* ARTIK Cloud REST Initialization
**/
char server[] = "api.artik.cloud"; // Samsung ARTIK Cloud API Host
int port = 443; // 443 for HTTPS
char buf[200]; // body data to store the JSON to be sent to the ARTIK cloud
String deviceID = "fb3c4b37d94d4951b156281cbc6358d1"; // put your device id here created from tutorial
String deviceToken = "ecbc0df476734c43b86fc17cee9b3e2c"; // put your device token here created from tutorial
int sendInterval = 60; // send time interval in seconds
int sendIntervalCounter=0; // count if we have to send data
/**
* pH meter initialization
**/
#define SensorPin A1 // pH meter Analog output to Arduino Analog Input 1
#define Offset 0.00 // deviation compensate
#define samplingInterval 500
#define ArrayLenth 40 // times of collection
int pHArray[ArrayLenth]; // Store the average value of the sensor feedback
int pHArrayIndex=0;
//store ph calibration data here
FlashStorage(storedSlopeValue, int);
FlashStorage(storedInterceptValue, int);
// we'll save readings here
float pHValue, voltage, celsius;
// status of wifi connection
int status = -1;
// check if this is debug mode
// debug mode shows the status of sensors
bool debugMode = true;
/**
* Wifi Setting
**/
#define WIFI_AP "paredes"
#define WIFI_PWD "paredes2017"
WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, server, port);
/***************************************************
The fllowing code uses software solution to calibration the ph meter, not the potentiometer. So it is more easy to use and calibrate.
I revised it to accomodate MKR1000 settings and my custom formulas
This is for SEN0161 and SEN0169.
Visit https://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161) for more details.
Created 2016-8-11
By youyou from DFrobot <youyou.yu@dfrobot.com>
****************************************************/
#define ReceivedBufferLength 20
char receivedBuffer[ReceivedBufferLength+1]; // store the serial command
byte receivedBufferIndex = 0;
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; //store the sample voltage
int analogBufferIndex = 0;
float slopeValue = 3.5, interceptValue = 0, averageVoltage;
boolean enterCalibrationFlag = 0;
#define VREF 3300 //for arduino MKR1000, the ADC reference is the power(AVCC), that is 3300mV
/*************** END ***********************/
void setup(void) {
pinMode(13, OUTPUT); // set pin 13 (LED) to output
Serial.begin(9600);
// set up reading sensor thread
myThread.onRun(getReadings);
myThread.setInterval(500);
// set up time counter thread
timeCounter.onRun(checkTime);
timeCounter.setInterval(1000);
startWifi(); //start connecting to wifi
readCharacteristicValues(); //read the slope and intercept of the ph probe
}
void loop(void) {
// checks if thread should run
if(myThread.shouldRun())
myThread.run();
// checks if thread should run
if(timeCounter.shouldRun())
timeCounter.run();
// check if we need to send
if(sendIntervalCounter == sendInterval)
sendToAtik();
// check status of wifi connection
status = WiFi.status();
if(status != WL_CONNECTED)
startWifi();
}
/*
* Send Sensor values to api
*/
void sendToAtik(){
Serial.println("===========================================");
Serial.println("We will send these json data");
//print to json format
Serial.println("data: { ");
Serial.print("ph: ");
Serial.print(pHValue);
Serial.print(" , temp: ");
Serial.print(celsius);
Serial.println("} ");
Serial.println("Start sending data");
String contentType = "application/json";
String AuthorizationData = "Bearer " + deviceToken; //Device Token
int len = loadBuffer(celsius,pHValue);
Serial.println("Sending temp: "+String(celsius) +" and ph: "+String(pHValue) );
Serial.println("Send POST to ARTIK Cloud API");
client.beginRequest();
client.post("/v1.1/messages"); //, contentType, buf
client.sendHeader("Authorization", AuthorizationData);
client.sendHeader("Content-Type", "application/json");
client.sendHeader("Content-Length", len);
client.endRequest();
client.print(buf);
// print response from api
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.println("");
if(statusCode==200){
digitalWrite(13, HIGH); // turn on LED
delay(500); // Make delay fro blink
digitalWrite(13, LOW); // turn on LED
Serial.print("Successfully sent data.");
}else{
Serial.print("Failed to send data.");
}
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
/*
* Here we we check time
*/
void checkTime(){
sendIntervalCounter++;
if(sendIntervalCounter > sendInterval){
sendIntervalCounter = 0;
}
}
/*
* Here we read the sensors
*/
void getReadings(){
// Aquiring current temperature
tempSensor.requestTemperatures(); // Send the command to get temperatures
celsius = tempSensor.getTempCByIndex(0);
/***************************************************
The fllowing formula uses software solution to calibration the ph meter, not the potentiometer. So it is more easy to use and calibrate.
I revised it to accomodate MKR1000 settings and my custom formulas
This is for SEN0161 and SEN0169.
Visit https://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161) for more details.
Created 2016-8-11
By youyou from DFrobot <youyou.yu@dfrobot.com>
****************************************************/
if(serialDataAvailable() > 0)
{
byte modeIndex = uartParse();
phCalibration(modeIndex); // If the correct calibration command is received, the calibration function should be called.
readCharacteristicValues(); // After calibration, the new slope and intercept should be read ,to update current value.
}
analogBuffer[analogBufferIndex] = analogRead(SensorPin)/1024.0*VREF; //read the voltage and store into the buffer,every 40ms
analogBufferIndex++;
if(analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
averageVoltage = getMedianNum(analogBuffer,SCOUNT); // read the stable value by the median filtering algorithm
pHValue = averageVoltage/1000.0*slopeValue+interceptValue;
if(enterCalibrationFlag) // in calibration mode, print the voltage to user, to watch the stability of voltage
{
Serial.print("Voltage:");
Serial.print(averageVoltage);
Serial.println("mV");
}else if(debugMode){
Serial.print("pH:"); // in normal mode, print the sensors value to user
Serial.print(pHValue);
Serial.print(" Temperature: ");
Serial.println(celsius);
}
/******************** END **************/
}
/*
* Connect to wifi settings
*/
void startWifi(){
digitalWrite(13, HIGH); // turn on LED while connecting
Serial.println("Connecting MKR1000 to network...");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED ) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
WiFi.begin(WIFI_AP, WIFI_PWD);
// wait 10 seconds for connection:
delay(10000);
status = WiFi.status();
}
Serial.println("Connected!");
digitalWrite(13, LOW); // turn off LED when connected
}
/*
* Buffer to send on API
*/
int loadBuffer(float temp, float ph ) {
StaticJsonBuffer<200> jsonBuffer; // reserve spot in memory
JsonObject& root = jsonBuffer.createObject(); // create root objects
root["sdid"] = deviceID;
root["type"] = "message";
JsonObject& dataPair = root.createNestedObject("data"); // create nested objects
dataPair["temp"] = temp;
dataPair["ph"] = ph;
root.printTo(buf, sizeof(buf)); // JSON-print to buffer
return (root.measureLength()); // also return length
}
/***************************************************
The fllowing formula uses software solution to calibration the ph meter, not the potentiometer. So it is more easy to use and calibrate.
I revised it to accomodate MKR1000 settings and my custom formulas
This is for SEN0161 and SEN0169.
Visit https://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161) for more details.
Created 2016-8-11
By youyou from DFrobot <youyou.yu@dfrobot.com>
****************************************************/
boolean serialDataAvailable(void)
{
char receivedChar;
static unsigned long receivedTimeOut = millis();
while (Serial.available()>0)
{
if (millis() - receivedTimeOut > 1000U)
{
receivedBufferIndex = 0;
memset(receivedBuffer,0,(ReceivedBufferLength+1));
}
receivedTimeOut = millis();
receivedChar = Serial.read();
if (receivedChar == '\n' || receivedBufferIndex==ReceivedBufferLength){
receivedBufferIndex = 0;
strupr(receivedBuffer);
return true;
}
else{
receivedBuffer[receivedBufferIndex] = receivedChar;
receivedBufferIndex++;
}
}
return false;
}
byte uartParse()
{
byte modeIndex = 0;
if(strstr(receivedBuffer, "CALIBRATION") != NULL)
modeIndex = 1;
else if(strstr(receivedBuffer, "EXIT") != NULL)
modeIndex = 4;
else if(strstr(receivedBuffer, "ACID:") != NULL)
modeIndex = 2;
else if(strstr(receivedBuffer, "ALKALI:") != NULL)
modeIndex = 3;
return modeIndex;
}
void phCalibration(byte mode)
{
char *receivedBufferPtr;
static byte acidCalibrationFinish = 0, alkaliCalibrationFinish = 0;
static float acidValue,alkaliValue;
static float acidVoltage,alkaliVoltage;
float acidValueTemp,alkaliValueTemp,newSlopeValue,newInterceptValue;
switch(mode)
{
case 0:
if(enterCalibrationFlag)
Serial.println(F("Command Error"));
break;
case 1:
receivedBufferPtr=strstr(receivedBuffer, "CALIBRATION");
enterCalibrationFlag = 1;
acidCalibrationFinish = 0;
alkaliCalibrationFinish = 0;
Serial.println(F("Enter Calibration Mode"));
break;
case 2:
if(enterCalibrationFlag)
{
receivedBufferPtr=strstr(receivedBuffer, "ACID:");
receivedBufferPtr+=strlen("ACID:");
acidValueTemp = strtod(receivedBufferPtr,NULL);
if((acidValueTemp>3)&&(acidValueTemp<5)) //typical ph value of acid standand buffer solution should be 4.00
{
acidValue = acidValueTemp;
acidVoltage = averageVoltage/1000.0; // mV -> V
acidCalibrationFinish = 1;
Serial.println(F("Acid Calibration Successful"));
}else {
acidCalibrationFinish = 0;
Serial.println(F("Acid Value Error"));
}
}
break;
case 3:
if(enterCalibrationFlag)
{
receivedBufferPtr=strstr(receivedBuffer, "ALKALI:");
receivedBufferPtr+=strlen("ALKALI:");
alkaliValueTemp = strtod(receivedBufferPtr,NULL);
if((alkaliValueTemp>8)&&(alkaliValueTemp<11)) //typical ph value of alkali standand buffer solution should be 9.18 or 10.01
{
alkaliValue = alkaliValueTemp;
alkaliVoltage = averageVoltage/1000.0;
alkaliCalibrationFinish = 1;
Serial.println(F("Alkali Calibration Successful"));
}else{
alkaliCalibrationFinish = 0;
Serial.println(F("Alkali Value Error"));
}
}
break;
case 4:
if(enterCalibrationFlag)
{
if(acidCalibrationFinish && alkaliCalibrationFinish)
{
newSlopeValue = (acidValue-alkaliValue)/(acidVoltage - alkaliVoltage);
newInterceptValue = acidValue - (slopeValue*acidVoltage);
storedSlopeValue.write(newSlopeValue);
storedInterceptValue.write(newInterceptValue);
Serial.print(F("Calibration Successful"));
}
else Serial.print(F("Calibration Failed"));
Serial.println(F(",Exit Calibration Mode"));
acidCalibrationFinish = 0;
alkaliCalibrationFinish = 0;
enterCalibrationFlag = 0;
}
break;
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i<iFilterLen; i++)
{
bTab[i] = bArray[i];
}
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
void readCharacteristicValues()
{
// If the EEPROM is empty then isValid() is false
interceptValue = storedInterceptValue.read();
slopeValue = storedSlopeValue.read();
if(slopeValue==0){
slopeValue = 3.5; // If the EEPROM is new, the recommendatory slope is 3.5.
storedSlopeValue.write(slopeValue);
}
if(debugMode){
Serial.print("Stored: ph sensor slope:");
Serial.print(slopeValue);
Serial.print(" intercept value:");
Serial.println(interceptValue);
}
}
/********************** END ***********************/