-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_mqtt_pulsecounter.ino
230 lines (187 loc) · 6.45 KB
/
simple_mqtt_pulsecounter.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
/*****************************************************************************************
* Simple pulse counter which sends data over MQTT.
* This coutner uses interrupts on 2 pins - I have two water meters attached to ESP8266
* via simplest LM393 comparator circuit
* It is "talk-only" client.
* It sends out data every 60 seconds by default.
* It keeps adding pulses until succesful connection.
*
* MQTT Message consists of
* P1, P2 - number of pulses counted since last succesful connection to server
* Sec - period during which we collected P1 and P2, seconds.
* Per - number of base periods we collected data for. >1 if we failed to connect to server last time
* Up - ESP8266 uptime
*
* Copyright (C) 2016 Anton Viktorov <[email protected]>
*
* This library is free software. You may use/redistribute it under The MIT License terms.
*
*****************************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
extern "C" {
#include "user_interface.h"
}
const char CompileDate[] = __DATE__ " " __TIME__;
#define WIFI_LED_PIN 5 // LED on when connected to Wifi
#define PULSE_PIN1 12 // P1
#define PULSE_PIN2 13 // P2
#define SAMPLE_MINUTES 1
#define SENSOR_FAMILY "ESP8266" // final sensor name for MQTT will be SENSOR_FAMILY-SENSOR_NAME-SENSOR_ID
#define SENSOR_NAME "water" // default values give: "ESP8266-water-1"
#define SENSOR_ID "1"
//#define MAX_MQTT_FAILURES_BEFORE_REBOOT 10 //not used
#define DEBOUNCE_MS 20 //debouncing for interrupts
const char *Ssid = "YOURSSID"; // cannot be longer than 32 characters! there also might be an issue with connection if SSID is less than 8 chars
const char *Pass = "PASSWORD"; //
char MqttServer[] = "192.168.1.1"; // address of your MQTT Server
unsigned int MqttPort = 1883; // MQTT port number. default is 1883
char MqttTopic[] = "sensors/" SENSOR_NAME;
//char MqttTopicIn[] = "sensors-control/" SENSOR_NAME; // mqtt callback not implemented
volatile unsigned int Pulses1 = 0;
volatile unsigned int PulsesLast1 = 0;
volatile unsigned int PulsesKept1 = 0;
volatile unsigned int Pulses2 = 0;
volatile unsigned int PulsesLast2 = 0;
volatile unsigned int PulsesKept2 = 0;
volatile unsigned int PulsesPeriods = 0;
unsigned int MqttFailures = 0;
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.println("mqtt callback invoked. not implemented.");
}
WiFiClient WifiClient;
PubSubClient MqttClient(MqttServer, MqttPort, mqttCallback, WifiClient);
volatile unsigned long LastMicros1;
void pulseHandler1() {
if((long)(micros() - LastMicros1) >= DEBOUNCE_MS * 1000) {
Pulses1 = Pulses1 + 1;
LastMicros1 = micros();
}
}
volatile unsigned long LastMicros2;
void pulseHandler2() {
if((long)(micros() - LastMicros2) >= DEBOUNCE_MS * 1000) {
Pulses2 = Pulses2 + 1;
LastMicros2 = micros();
}
}
// TIMER
os_timer_t myTimer;
bool tickOccured;
void timerCallback(void *pArg) {
PulsesLast1 = Pulses1;
PulsesKept1 += Pulses1;
Pulses1 = 0;
PulsesLast2 = Pulses2;
PulsesKept2 += Pulses2;
Pulses2 = 0;
PulsesPeriods++;
tickOccured = true;
}
void timerInit(void) {
os_timer_setfn(&myTimer, timerCallback, NULL);
os_timer_arm(&myTimer, 1000 * 60 * SAMPLE_MINUTES, true);
}
void setup() {
Serial.begin(74880);
delay(10);
Serial.println();
Serial.print("Simple MQTT Pulse counter (C) Anton Viktorov, [email protected], "); Serial.println(CompileDate);
Serial.print("Server: ");Serial.print(MqttServer);Serial.print(" Topic: "); Serial.println(MqttTopic);
WiFi.mode(WIFI_STA);
// pinMode(4, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
// pinMode(12, INPUT_PULLUP);
// pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(16, INPUT_PULLDOWN_16);
pinMode(WIFI_LED_PIN, OUTPUT);
digitalWrite(WIFI_LED_PIN, LOW);
pinMode(PULSE_PIN1, INPUT);
attachInterrupt(PULSE_PIN1, pulseHandler1, RISING);
pinMode(PULSE_PIN2, INPUT);
attachInterrupt(PULSE_PIN2, pulseHandler2, RISING);
tickOccured = false;
timerInit();
}
//
//void delay500() {
// for(int i = 0; i < 20; i++) {
// delay(10);
// yield();
// }
//}
void loop() {
if (tickOccured == true) {
Serial.print("Tick occured. Pulses kept so far: ");
String payload = "{\"d\":{\"N\":\"" SENSOR_NAME "-" SENSOR_ID "\"";
payload += ",\"P1\":";
payload += PulsesKept1;
payload += ",\"P2\":";
payload += PulsesKept2;
payload += ",\"Sec\":";
payload += PulsesPeriods * 60 * SAMPLE_MINUTES;
payload += ",\"Per\":";
payload += PulsesPeriods;
payload += ",\"Up\":";
payload += ((unsigned long)millis()/1000);
payload += "}}";
Serial.println(payload);
if (MqttClient.publish(MqttTopic, (char*) payload.c_str())) {
Serial.println("mqtt publish ok");
PulsesPeriods = 0;
PulsesKept1 = 0;
PulsesKept2 = 0;
} else {
Serial.println("mqtt publish fail");
// MqttFailures++; // not sure why I wanted to do this :) so I commented this out later
// if (MqttFailures >= MAX_MQTT_FAILURES_BEFORE_REBOOT) {
// Serial.println("too many mqtt failures, rebooting");
// ESP.reset();
// }
if (MqttClient.connected()) {
Serial.println("mqtt connection still up");
} else {
Serial.println("mqtt connection down");
if (MqttClient.connect("ESP8266-" SENSOR_NAME "-" SENSOR_ID)) {
yield();
}
}
}
tickOccured = false;
}
digitalWrite(WIFI_LED_PIN, 0);
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(Ssid);
Serial.println("...");
// digitalWrite(WIFI_LED_PIN, 1);
// delay500();
// digitalWrite(WIFI_LED_PIN, 0);
// delay500();
WiFi.begin(Ssid, Pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.printDiag(Serial);
return;
}
Serial.println("WiFi connected");
} else {
digitalWrite(WIFI_LED_PIN, 1);
}
if (WiFi.status() == WL_CONNECTED) {
if (!MqttClient.connected()) {
yield();
if (MqttClient.connect(SENSOR_FAMILY "-" SENSOR_NAME "-" SENSOR_ID)) {
yield();
MqttClient.publish("welcome",SENSOR_FAMILY "-" SENSOR_NAME "-" SENSOR_ID);
Serial.println("MQTT connected");
// MqttClient.subscribe(MqttTopicIn);
}
}
if (MqttClient.connected())
MqttClient.loop();
}
yield();
}