Minimum/maximum (Læst 3448x)

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 64
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Minimum/maximum
« Dato: August 23, 2016, 08:22:02 »
Hej Forum
Så har jeg igen et problem med min vejrstation jeg kan bare ikke se fejlen i koden
for minimum temperatur når maximum temperatur virker det er i koden linje ca. 100
er der nogen her der kan hjælpe mig  ;)

Mvh Elfix
Kode:
#include <SPI.h>           // used to communicated via the spi bus
#include <Ethernet.h>      // used to communicate with the ethernet controller

#include "TimerOne.h"          // Timer Interrupt set to 2 second for read sensors
#include <math.h>

#include <Wire.h>
#include <Adafruit_BMP085.h>

#define Bucket_Size 0.2794   // bucket size to trigger tip count
#define Bucket_Pin 3         // digital pin Bucket connected to
#define TX_Pin 8           // used to indicate web data tx
 
#define WindSensor_Pin (2)      // The pin location of the anemometer sensor
#define WindVane_Pin (A2)       // The pin the wind vane sensor is connected to
#define VaneOffset 70           // define the anemometer offset from magnetic north

Adafruit_BMP085 bmp;

volatile unsigned long tipCount;    // bucket tip counter used in interrupt routine
volatile unsigned long contactTime; // Timer to manage any contact bounce in interrupt routine

volatile unsigned int  timerCount;    // used to determine 2.5sec timer count
volatile unsigned long rotations;     // cup rotation counter used in interrupt routine
volatile unsigned long contactBounceTime;  // Timer to avoid contact bounce in interrupt routine

long lastTipcount;            // keeps track of bucket tips
float totalRainfall;       // total amount of rainfall detected

volatile float windSpeed;
int vaneValue;       // raw analog value from wind vane
int vaneDirection;   // translated 0 - 360 direction
int calDirection;    // converted value with offset applied
int lastDirValue;    // last recorded direction value

float minTemp;   // keeps track of the minimum recorded temp value
float maxTemp;   // keeps track of the maximum recorded temp value
float maxWind;   // keeps track of the maximum recorded wind value
   

// arayas we use for averaging sensor data
float temps[12];    // array of 12 temps to create a 2 minute average temp
float wspeeds[24];  // array of 24 wind speeds for 2 minute average wind speed
float wdirect[24];  // array of 24 wind directions for 2 minute average


// Here we setup the webserver. We need to supply a mac address. Some ethernet boards have a label attached
// with a mac address you can use.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,178);      // IP address, may need to change depending on network
EthernetServer server(80);           // create a server at port 80

void setup() {

  // setup rain sensor values
  lastTipcount = 0;
  tipCount = 0;
  totalRainfall = 0;

  // setup anemometer values
  lastDirValue = 0;
  rotations = 0;

  // setup timer values
  timerCount = 0;
 

  // disable the SD card by switching pin 4 high
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
 
  Serial.begin(9600);
if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  while (1) {}
  } 
 

  pinMode(TX_Pin,OUTPUT);
  pinMode(Bucket_Pin, INPUT);
  digitalWrite(Bucket_Pin, HIGH);
  pinMode(WindSensor_Pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(Bucket_Pin), isr_rg, FALLING);
  attachInterrupt(digitalPinToInterrupt(WindSensor_Pin), isr_rotation, FALLING);

  // Setup the timer intterupt for 0.5 second
  Timer1.initialize(500000);
  Timer1.attachInterrupt(isr_timer);
 
  sei();// Enable Interrupts
}

void loop() {
 
//  bmp.readSensor();

  // set temp min and max values
  if(bmp.readTemperature() < minTemp) {
    minTemp = bmp.readTemperature();
  } 
  if(bmp.readTemperature() > maxTemp) {
    maxTemp = bmp.readTemperature();
  }

  // set wind max values
  if (windSpeed > maxWind) {
    maxWind = windSpeed;
  }
 

  // update rainfall total if required
  if(tipCount != lastTipcount) {
    cli();  // disable intterupts
    lastTipcount = tipCount;
    totalRainfall = tipCount * Bucket_Size;
    sei();  // enable interrupts
  }

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
       if (client.available()) {
          char c = client.read();
          Serial.write(c);
          // if you've gotten to the end of the line (received a newline
          // character) and the line is blank, the http request has ended,
          // so you can send a reply
          if (c == '\n' && currentLineIsBlank) {
             // send a standard http response header
             digitalWrite(TX_Pin,HIGH);
             client.println("HTTP/1.1 200 OK");
             client.println("Content-Type: text/html");
             client.println("Connection: close");  // the connection will be closed after completion of the response
             client.println("Refresh: 10");  // refresh the page automatically every 5 sec
             client.println();
             client.println("<body bgcolor=\"black\">");//nye
             client.print("<font color=\"white\">");//nye
             client.println("<!DOCTYPE HTML>");
             client.println("<html><body><h1>&nbsp;&nbspGammeldam Weather Control<br>&nbsp;&nbsp vers. 1.0.1</h1>");
             
             digitalWrite(TX_Pin,HIGH);     // Turn the TX LED on
             client.print("<span style=\"font-size: 30px\";><br>&nbsp;&nbsp;Temperature is ");   
             client.print(bmp.readTemperature());
             client.println(" &deg;C<br />");
             client.print("&nbsp;&nbsp;Minim ");   
             client.print(minTemp);
             client.println(" &deg;C");
             client.print("&nbsp;&nbsp;Maxim ");   
             client.print(maxTemp);
             client.println(" &deg;C<br />");
//             client.print("<br>&nbsp;&nbsp;Humidity is ");   
//             client.print(bme.getHumidity());
//             client.println(" %<br />");
             client.print("<br>&nbsp;&nbsp;Pressure is ");   
             client.print(bmp.readPressure()/100);
             client.println(" mb<br />");
             client.print("<br>&nbsp;&nbsp;Wind Speed is ");   
             client.print(windSpeed);
             client.println(" ms<br />");
             client.print("&nbsp;&nbsp;");
             if(calDirection < 0.2)
             client.print("Quit wind");
             else if (windSpeed < 1.5)
             client.print("Almost calm Wind");
             else if (windSpeed < 3.3)
             client.print("Weak wind");
             else if (windSpeed < 5.4)
             client.print("Light wind");
             else if (windSpeed <7.9)
             client.print("Moderate breeze");
             else if (windSpeed < 10.7)
             client.print("Fresh wind");
             else if (windSpeed < 13.8)
             client.print("Strong wind");
             else if (windSpeed < 17.1)
             client.print("High wind");
             else if (windSpeed < 20.7)
             client.print("Moderate gale");
             else if (windSpeed < 24.4)
             client.print("Fresh gale");
             else if (windSpeed < 28.4)
             client.print("Storm");
             else if (windSpeed < 32.6)
             client.print("Strong storm");
             else
             client.print("Hurricane");
             client.println("<br />");
             client.print("<br>&nbsp;&nbsp;Wind Max Speed is ");   
             client.print(maxWind);
             client.println(" ms<br />");
             client.print("&nbsp;&nbsp;");
             if(calDirection < 0.2)
             client.print("Quit wind");
             else if (maxWind < 1.5)
             client.print("Almost calm Wind");
             else if (maxWind < 3.3)
             client.print("Weak wind");
             else if (maxWind < 5.4)
             client.print("Light wind");
             else if (maxWind <7.9)
             client.print("Moderate breeze");
             else if (maxWind < 10.7)
             client.print("Fresh wind");
             else if (maxWind < 13.8)
             client.print("Strong wind");
             else if (maxWind < 17.1)
             client.print("High wind");
             else if (maxWind < 20.7)
             client.print("Moderate gale");
             else if (maxWind < 24.4)
             client.print("Fresh gale");
             else if (maxWind < 28.4)
             client.print("Storm");
             else if (maxWind < 32.6)
             client.print("Strong storm");
             else
             client.print("Hurricane");
             client.println("<br />");
             getWindDirection();
             client.print("<br>&nbsp;&nbsp;Direction is ");   
             client.print(calDirection);
             client.println(" &deg;");
             if(calDirection < 22)
             client.print(" = North");
             else if (calDirection < 67)
             client.print(" = NorthEast");
             else if (calDirection < 112)
             client.print(" = East");
             else if (calDirection < 157)
             client.print(" = SouthEast");
             else if (calDirection < 212)
             client.print(" = South");
             else if (calDirection < 247)
             client.print(" = SouthWest");
             else if (calDirection < 292)
             client.print(" = West");
             else if (calDirection < 337)
             client.print(" = NorthWest");
             else
             client.print(" = N");
             client.println(" <br />");
             client.print("<br>&nbsp;&nbsp;Rainfall is ");   
             client.print(totalRainfall);
             client.println(" mm<br />");
             //client.print("&nbsp;&nbsp;Mini ");   
             //client.print(minTemp);
             //client.println(" &deg;C<br />");
             //client.print("&nbsp;&nbsp;Maxi ");   
             //client.print(maxTemp);
             //client.println(" &deg;C<br />");
             client.println("</html>");
             digitalWrite(TX_Pin,LOW);      // Turn the TX LED off
             break;
           }
           if (c == '\n') {
             // you're starting a new line
             currentLineIsBlank = true;
           } else if (c != '\r') {
             // you've gotten a character on the current line
             currentLineIsBlank = false;
           }
           
         }
       }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    // Serial.println("client disconnected");
    Ethernet.maintain();

}

// isr routine for timer interrupt
void isr_timer() {
 
  timerCount++;
 
  if(timerCount == 5)
  {
    // convert to ms using the formula 2.4 km/t = (2.4 km/t)*(1000 m/km)*(1/3600 t/s)
    // = 2/3 m/s = 0.667 m/s
    windSpeed = rotations * 0.667;
    rotations = 0;
    timerCount = 0;
  }
}

// Interrrupt handler routine that is triggered when the rg-11 detects rain
void isr_rg() {

  if((millis() - contactTime) > 15 ) { // debounce of sensor signal
    tipCount++;
    contactTime = millis();
  }
}
// end of rg-11 rain detection interrupt handler

// interrupt handler to increment the rotation count for wind speed
void isr_rotation ()   {

  if ((millis() - contactBounceTime) > 15 ) {  // debounce the switch contact.
    rotations++;
    contactBounceTime = millis();
  }
}

// Get Wind Direction
void getWindDirection() {
 
   vaneValue = analogRead(WindVane_Pin);
   vaneDirection = map(vaneValue, 0, 1023, 0, 360);
   calDirection = vaneDirection + VaneOffset;
   
   if(calDirection > 360)
     calDirection = calDirection - 360;
     
   if(calDirection < 0)
     calDirection = calDirection + 360;
 
}
« Senest Redigeret: August 23, 2016, 14:21:58 af Elfix »
Alle opgaver løses kun umuligheder tager lidt længere tid

 

Offline gerd

  • Administrator
  • µProcessoren
  • *****
  • Indlæg: 915
  • Antal brugbare Indlæg: 97
    • Vis profil
    • Hjemmeside med nogle af mine projekter
Sv: Minimum/maximum
« Svar #1 Dato: August 23, 2016, 17:24:03 »
Hej Jonny,

Du skriver:
float minTemp;   

der er:
float minTemp = 0.0;
eller
minTemp = 0°C.

Jeg tror, det var ikke koldere end 0°C i dag. Så 0°C er korrekt.

Men hvis du skrive
float minTemp = 999.9;
er den næste værdi omkring 21.0°C ... og det virker.

Du kan også skrive
minTemp = bmp.readTemperature();
på enden af setup(); {..}

Apropos vejrstation ...

I tyskland har hver højtryksområde et navn. Og i denne uge heder det "gerd"


gerd



 

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 64
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Sv: Minimum/maximum
« Svar #2 Dato: August 23, 2016, 19:32:00 »
Hej Gerd
Tak for hjælpen!! også denne gang  :)
Nu skal jeg nok tænke på dig når der er solskin de næste dage   8)
Mvh Jonny
Alle opgaver løses kun umuligheder tager lidt længere tid

 

Offline Elfix

  • Spændingsdeler
  • ****
  • Indlæg: 64
  • Antal brugbare Indlæg: 1
  • Gammeldam
    • Vis profil
Sv: Minimum/maximum
« Svar #3 Dato: August 23, 2016, 23:38:34 »
Hej Forum
Så mangler jeg bare at konverter det hele over til esp8266 med eller uden arduino også
google chart så tror jeg det køre  8)

Mvh Elfix
Alle opgaver løses kun umuligheder tager lidt længere tid