-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
168 lines (160 loc) · 4.92 KB
/
main.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
#include <rgb_lcd.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ElegantOTA.h>
// Variables
const char* ssid = "WIFI_SSID";
const char* password = "WIFI_PASSWORD";
const int MaxHeight = 1700; //height of water tank in mm
const int Offset = 100; //distance between sensor and top of water tank in mm
const int MaxVol = 20000; //max vol of both water tanks in L
int CurVol = 0;
int CurPercentage = 0;
// Sensor variables
int pinRX = 16;
int pinTX = 17;
unsigned char data_buffer[4] = { 0 };
float distance = 0;
unsigned char CS;
// Object to represent software serial port A02YYUW
SoftwareSerial MySerial(pinRX, pinTX);
// Object to represent 16x2 LCD
rgb_lcd lcd;
// Webserver and OTA server
WiFiServer webserver(80); //Json server
WebServer server(81); //OTA server
// Progress Bar characters
byte zero[] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000 };
byte one[] = { B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000 };
byte two[] = { B11000, B11000, B11000, B11000, B11000, B11000, B11000, B11000 };
byte three[] = { B11100, B11100, B11100, B11100, B11100, B11100, B11100, B11100 };
byte four[] = { B11110, B11110, B11110, B11110, B11110, B11110, B11110, B11110 };
byte five[] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };
// Function to start Wifi server
void setupWiFi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(F("WiFi connected."));
Serial.print(F("Please connect to http://"));
Serial.println(WiFi.localIP());
lcd.clear();
lcd.print(WiFi.localIP());
// Start JSON webserver
webserver.begin();
Serial.println("Webserver started (:80)");
// Start ElegantOTA
ElegantOTA.begin(&server);
server.begin();
Serial.println("OTA server started (:81)");
}
// Function to setup LCD screen
void setupLcd() {
lcd.begin(16, 2);
lcd.createChar(0, zero);
lcd.createChar(1, one);
lcd.createChar(2, two);
lcd.createChar(3, three);
lcd.createChar(4, four);
lcd.createChar(5, five);
lcd.clear();
}
// Function to update LCD screen with readings
void updateLCD() {
static unsigned long timer = 0;
const unsigned long interval = 1000UL * 5; // testing, show every x seconds
const int lineToPrintOn = 1;
if (millis() - timer >= interval) {
timer = millis();
MySerial.flush();
if (MySerial.available() > 0) {
delay(5); // to allow signal to stabilize
if (MySerial.read() == 0xff) {
data_buffer[0] = 0xff;
for (int i = 1; i < 4; i++) {
data_buffer[i] = MySerial.read();
}
CS = data_buffer[0] + data_buffer[1] + data_buffer[2];
if (data_buffer[3] == CS) {
distance = (data_buffer[1] << 8) + data_buffer[2];
// Print to serial monitor
Serial.print("Distance : ");
Serial.print(distance);
Serial.println(" mm");
}
}
// Calculate percentage & volume
CurPercentage = (100 - (((distance - Offset) / MaxHeight) * 100));
CurVol = MaxVol / 100 * CurPercentage;
// Show progress bar on LCD
float factor = 100.0 / 80.0; // 16 characters x 5
int percent = (CurPercentage + 1) / factor;
int number = percent / 5;
int remainder = percent % 5;
// show current volume and percentage on LCD
if (number > 0) {
for (int j = 0; j < number; j++) {
lcd.setCursor(j, lineToPrintOn);
lcd.write(5);
}
}
lcd.setCursor(number, 1);
lcd.write(remainder);
if (number < 16) {
for (int j = number + 1; j <= 16; j++) {
lcd.setCursor(j, lineToPrintOn);
lcd.write((byte)0x00);
}
}
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(1, 0);
lcd.print(CurVol);
lcd.print(" L - ");
lcd.print(CurPercentage);
lcd.print(" %");
}
}
}
void doWiFi() {
WiFiClient client = webserver.available();
if (!client)
return;
Serial.println(F("New client"));
while (client.available()) client.read();
StaticJsonDocument<100> doc;
doc["data"] = CurPercentage;
doc["distance"] = distance;
// Write response headers
client.println(F("HTTP/1.0 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(measureJsonPretty(doc));
client.println();
serializeJsonPretty(doc, client);
delay(5000); //wait for client to download information
client.stop();
}
// ========== MAIN FUNCTIONS: SETUP & LOOP ==========
void setup() {
Serial.begin(115200);
MySerial.begin(9600); //Software serial port
setupLcd();
lcd.print("Starting...");
delay(5000);
setupWiFi();
delay(5000);
lcd.clear();
}
void loop() {
updateLCD();
doWiFi();
server.handleClient(); //OTA server
}