forked from oh2mp/esp32_watersensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_watersensor.ino
123 lines (101 loc) · 3.39 KB
/
esp32_watersensor.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
/*
* ESP32 based BLE beacon for liquid sensors
*
* See https://github.com/oh2mp/esp32_watersensor
*
*/
#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEBeacon.h"
#include <SPIFFS.h>
#define LED 2
#define SENS 36
int sensorbuff;
byte sensor;
char datastr[16];
char convtable[255];
BLEAdvertising *advertising;
std::string mfdata = "";
/* ----------------------------------------------------------------------------------
* Set up data packet for advertising
*/
void set_beacon() {
BLEBeacon beacon = BLEBeacon();
BLEAdvertisementData advdata = BLEAdvertisementData();
BLEAdvertisementData scanresponse = BLEAdvertisementData();
advdata.setFlags(0x06); // BR_EDR_NOT_SUPPORTED 0x04 & LE General discoverable 0x02
mfdata = "";
mfdata += (char)0xE5; mfdata += (char)0x02; // Espressif Incorporated Vendor ID = 0x02E5
mfdata += (char)0x48; mfdata += (char)0xE9; // Identifier for this sketch is 0xE948 (Oxygen)
mfdata += (char)(sensor); // Raw (calculated) value from the sensor
mfdata += convtable[sensor]; // Table converted value
mfdata += (char)0xBE; mfdata += (char)0xEF; // Beef is always good nutriment
advdata.setManufacturerData(mfdata);
advertising->setAdvertisementData(advdata);
advertising->setScanResponseData(scanresponse);
}
/* ----------------------------------------------------------------------------------
* Read the conversion table from the file
*/
void readtable() {
memset(convtable, 0, sizeof(convtable));
if (SPIFFS.exists("/table.txt")) {
File file;
file = SPIFFS.open("/table.txt", "r");
char numstr[8];
int inx;
int val;
byte fillflag = 0;
while (file.available()) {
memset(numstr,0,8);
file.readBytesUntil(' ', numstr, 8);
inx = (char)atoi(numstr);
memset(numstr,0,8);
file.readBytesUntil('\n', numstr, 8);
val = (char)atoi(numstr);
convtable[inx] = val;
if (fillflag == 0) {
memset(convtable,val,inx);
fillflag = 1;
}
}
file.close();
}
}
/* ---------------------------------------------------------------------------------- */
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); // LED off
pinMode(SENS, INPUT);
analogSetWidth(12);
analogSetAttenuation(ADC_0db);
analogReadResolution(12);
Serial.begin(115200);
BLEDevice::init("ESP32+watersensor");
advertising = BLEDevice::getAdvertising();
SPIFFS.begin();
readtable();
}
/* ---------------------------------------------------------------------------------- */
void loop() {
// Read sensor and calculate value
for (int i = 0; i < 50; i++) {
sensorbuff += analogRead(SENS);
delay(100);
}
sensor = int((float)sensorbuff/2000+0.5);
sprintf(datastr, "%d = %d l\n",sensor,(int)convtable[sensor]);
Serial.print(datastr);
sensorbuff = 0;
set_beacon();
digitalWrite(LED, HIGH); // LED on during the advertising
advertising->start();
delay(100);
advertising->stop();
digitalWrite(LED, LOW); // LED off
// Reboot once in hour to be sure
if (millis() > 3.6E+6) {
ESP.restart();
}
}
/* ---------------------------------------------------------------------------------- */