-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathesp8266-anemometer.ino
138 lines (129 loc) · 3.42 KB
/
esp8266-anemometer.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
#include <ESP8266WiFi.h>
#include "myconfig.h"
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <PubSubClient.h>
unsigned long next_timestamp = 0;
volatile unsigned long i = 0;
float wind = 0;
float last_wind = 0;
int count = 0;
volatile unsigned long last_micros;
long debouncing_time = 5; //in millis
int input_pin = 13;
char charBuffer[32];
WiFiClient espClient;
PubSubClient client(espClient);
void ICACHE_RAM_ATTR Interrupt()
{
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
i++;
last_micros = micros();
}
}
void setup() {
Serial.begin(115200);
delay(10);
pinMode(input_pin, INPUT_PULLUP);//D7
// We start by connecting to a WiFi network
if(debugOutput){
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
}
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int maxWait = 500;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if(debugOutput) Serial.print(".");
if(maxWait <= 0)
ESP.restart();
maxWait--;
}
if(debugOutput){
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
delay(500);
do_update();
client.setServer(mqtt_host, mqtt_port);
reconnect();
attachInterrupt(input_pin,Interrupt,RISING);
}
void loop()
{
if (millis() > next_timestamp )
{
detachInterrupt(input_pin);
count++;
float rps = i/number_reed; //computing rounds per second
if(i == 0)
wind = 0.0;
else
wind = 1.761 / (1 + rps) + 3.013 * rps;// found here: https://www.amazon.de/gp/customer-reviews/R3C68WVOLJ7ZTO/ref=cm_cr_getr_d_rvw_ttl?ie=UTF8&ASIN=B0018LBFG8 (in German)
if(last_wind - wind > 0.8 || last_wind - wind < -0.8 || count >= 10){
if(debugOutput){
Serial.print("Wind: ");
Serial.print(wind);
Serial.println(" km/h");
}
String strBuffer;
strBuffer = String(wind);
strBuffer.toCharArray(charBuffer,10);
if (!client.publish(mqtt_topic_prefix, charBuffer, false))
{
ESP.restart();
delay(100);
}
count = 0;
}
i = 0;
last_wind = wind;
if(WiFi.status() != WL_CONNECTED) {
ESP.restart();
delay(100);
}
next_timestamp = millis()+1000; //intervall is 1s
attachInterrupt(input_pin,Interrupt,RISING);
}
yield();
}
void reconnect() {
int maxWait = 0;
while (!client.connected()) {
if(debugOutput) Serial.print("Attempting MQTT connection...");
if (client.connect(mqtt_id)) {
if(debugOutput) Serial.println("connected");
} else {
if(debugOutput){
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
}
delay(5000);
if(maxWait > 10)
ESP.restart();
maxWait++;
}
}
}
void do_update(){
if(debugOutput) Serial.println("do update");
t_httpUpdate_return ret = ESPhttpUpdate.update(update_server, 80, update_uri, firmware_version);
switch(ret) {
case HTTP_UPDATE_FAILED:
if(debugOutput) Serial.println("[update] Update failed.");
break;
case HTTP_UPDATE_NO_UPDATES:
if(debugOutput )Serial.println("[update] no Update needed");
break;
case HTTP_UPDATE_OK:
if(debugOutput) Serial.println("[update] Update ok."); // may not called we reboot the ESP
break;
}
}