Hey,
i'm building a weather station which includes:
-
windspeed (interrupt counting)
-
winddirection (Analog input voltage measurement)
-
temp and humidity (dht11)
All of the data is written to my thingspeak channel to plot it.
The general setup is working fine but i noticed that the winddirection measurements are jumping up and down, so i included smoothing (12 measurements in a minute) for the analog input. To get this to work i had to change all my delays to millis() to get an analog reading every 5 seconds and to update all the other readings every minute to the thingspeak channel. Now the function to count my interrupts is not working anymore. I can't count any interrupts. I tried to get information from several older threads in this forum bit i couldn't work out a solution for my problem and i don't understand why using interrupts and millis seems to be a problem.
Maybe someone can explain what my problem is and how to fix it, thanks in advance.
#include "DHT.h"
#include "ThingSpeak.h"
#include <WiFiNINA.h>
#define WIFI_SSID "XXXXXX"
#define WIFI_PASSWORD "XXXXXX"
WiFiClient client;
unsigned long aufnahmeStartMillis;
unsigned long fahneStartMillis;
unsigned long currentMillis;
const unsigned long aufnahmePeriod = 60000;
const unsigned long fahneAveragePeriod = 5000;
const int numReadings = 12;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int windDirectionVoltage = 0;
int inputPin = A0;
unsigned long seconds = 1000L;
unsigned long minutes = seconds * 60;
unsigned long aufnahmezeit = minutes;
const byte interruptPin = 2;
int InterruptCounter;
float WindSpeed;
char* windDirection;
float humid;
float temp;
char thingSpeakAddress = "api.thingspeak.com";
unsigned long myChannelNumber = "XXXXX";
const char * myWriteAPIKey = "XXXXXXXXX";
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
String myStatus = "";
int number = 0;
int windDegrees = 0;
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
aufnahmeStartMillis = millis();
fahneStartMillis = millis();
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
currentMillis = millis();
messung();
windrichtung();
}
void windrichtung() {
if (currentMillis - fahneStartMillis >= fahneAveragePeriod)
{
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
windDirectionVoltage = total / numReadings;
Serial.print("WindDirectionVoltage: ");
Serial.println(windDirectionVoltage);
fahneStartMillis = currentMillis;
}
}
void messung() {
InterruptCounter = 0;
attachInterrupt(digitalPinToInterrupt(interruptPin), count, RISING);
if (currentMillis - aufnahmeStartMillis >= aufnahmePeriod) {
detachInterrupt(digitalPinToInterrupt(interruptPin));
if (windDirectionVoltage >= 212 && windDirectionVoltage < 273) {
windDirection = "N";
}
else if (windDirectionVoltage >= 577 && windDirectionVoltage < 665) {
windDirection = "NNE";
}
else if (windDirectionVoltage >= 483 && windDirectionVoltage < 577) {
windDirection = "NE";
}
else if (windDirectionVoltage >= 929 && windDirectionVoltage < 943) {
windDirection = "ENE";
}
else if (windDirectionVoltage >= 906 && windDirectionVoltage < 929) {
windDirection = "E";
}
else if (windDirectionVoltage >= 943 && windDirectionVoltage < 1023) {
windDirection = "ESE";
}
else if (windDirectionVoltage >= 795 && windDirectionVoltage < 858) {
windDirection = "SE";
}
else if (windDirectionVoltage >= 858 && windDirectionVoltage < 906) {
windDirection = "SSE";
}
else if (windDirectionVoltage >= 665 && windDirectionVoltage < 748) {
windDirection = "S";
}
else if (windDirectionVoltage >= 748 && windDirectionVoltage < 795) {
windDirection = "SSW";
}
else if (windDirectionVoltage >= 348 && windDirectionVoltage < 399) {
windDirection = "SW";
}
else if (windDirectionVoltage >= 399 && windDirectionVoltage < 483) {
windDirection = "WSW";
}
else if (windDirectionVoltage >= 0 && windDirectionVoltage < 106) {
windDirection = "W";
}
else if (windDirectionVoltage >= 163 && windDirectionVoltage < 212) {
windDirection = "WNW";
}
else if (windDirectionVoltage >= 106 && windDirectionVoltage < 163) {
windDirection = "NW";
}
else if (windDirectionVoltage >= 273 && windDirectionVoltage < 348) {
windDirection = "NNW";
}
if (windDirection == "N") {
windDegrees = 360;
}
else if (windDirection == "NNE") {
windDegrees = 22;
}
else if (windDirection == "NE") {
windDegrees = 45;
}
else if (windDirection == "ENE") {
windDegrees = 68;
}
else if (windDirection == "E") {
windDegrees = 90;
}
else if (windDirection == "ESE") {
windDegrees = 112;
}
else if (windDirection == "SE") {
windDegrees = 135;
}
else if (windDirection == "SSE") {
windDegrees = 158;
}
else if (windDirection == "S") {
windDegrees = 180;
}
else if (windDirection == "SSW") {
windDegrees = 202;
}
else if (windDirection == "SW") {
windDegrees = 225;
}
else if (windDirection == "WSW") {
windDegrees = 248;
}
else if (windDirection == "W") {
windDegrees = 270;
}
else if (windDirection == "WNW") {
windDegrees = 292;
}
else if (windDirection == "NW") {
windDegrees = 315;
}
else if (windDirection == "NNW") {
windDegrees = 338;
}
humid = dht.readHumidity();
temp = dht.readTemperature();
WindSpeed = (float(InterruptCounter) / (float(60)) * 2.4);
Serial.print("Umdrehungen: ");
Serial.println(InterruptCounter);
Serial.print("WindSpeed: ");
Serial.print(WindSpeed);
Serial.print("km/h / ");
Serial.print(WindSpeed / 3.6);
Serial.println("m/s" );
Serial.print("Windrichtung: ");
Serial.println(windDirection);
Serial.print("WindDirectionVoltage: ");
Serial.println(windDirectionVoltage);
Serial.print("Humidity: ");
Serial.println(humid);
Serial.print("Temperature: ");
Serial.println(temp);
// set the fields with the values
ThingSpeak.setField(1, WindSpeed);
ThingSpeak.setField(2, windDirection);
ThingSpeak.setField(3, humid);
ThingSpeak.setField(4, temp);
ThingSpeak.setField(5, windDegrees);
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
aufnahmeStartMillis = currentMillis;
}
}
void count() {
InterruptCounter++;
}