-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apt water meter.ino
157 lines (130 loc) · 4.55 KB
/
Apt water meter.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
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String apiKey = "IWSLQ5PSCU0C83CX"; // Enter your Write API key from ThingSpeak
const char *ssid = "iPhone"; // replace with your wifi ssid and wpa2 key
const char *pass = "0987654321";
const char* server = "api.thingspeak.com";
#define LED_BUILTIN 16
#define SENSOR 2
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
WiFiClient client;
void setup()
{
WiFi.begin("iPhone", "0987654321");
while(WiFi.status() != WL_CONNECTED)
{
Serial.print("..");
delay(200);
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
pulse1Sec = pulseCount;
pulseCount = 0;
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
display.clearDisplay();
display.setCursor(10,0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Water Flow Meter");
display.setCursor(0,20); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("R:");
display.print(float(flowRate));
display.setCursor(100,28); //oled display
display.setTextSize(1);
display.print("L/M");
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalLitres);
Serial.println("L");
display.setCursor(0,45); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("V:");
display.print(totalLitres);
display.setCursor(100,53); //oled display
display.setTextSize(1);
display.print("L");
display.display();
}
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(float(flowRate));
postStr += "&field2=";
postStr += String(totalLitres);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}