24. Februar 2018

Arduino-Sketch

#include <LiquidCrystal.h>
#define uint  unsigned int
#define ulong unsigned long
#define PIN_VANE 3
#define MSECS_CALC_WIND_DIR 1000
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#include "Wire.h"
#include "Adafruit_BMP085.h"
#include <Ethernet.h>
#include <SPI.h>
Adafruit_BMP085 bmp;
 int windPin = 3;        
 float windFactor = 1.19381;  
 int measureTime = 1;   
volatile unsigned int windCounter = 0;
float windSpeed = 0.0;
DHT dht(DHTPIN, DHTTYPE);

volatile int numRevsAnemometer = 0;
ulong nextCalcDir;                 
ulong time;                      

#define NUMDIRS 8
float  adcvalue = 1.1;
ulong   adc[NUMDIRS] = {26, 45, 77, 118, 161, 196, 220, 256};
char *strVals[NUMDIRS] = {"NORDOST","NORD","NORDWEST","WEST","SUEDWEST","SUED","SUEDOST","OST"};
byte dirOffset=0;

void countWind() {
   windCounter ++;
}

LiquidCrystal lcd(4,5, 6, 7, 8, 9);

void show_values() {
dht.begin();
bmp.begin();
float h = dht.readHumidity();
float t = dht.readTemperature();
nextCalcDir   = millis() + MSECS_CALC_WIND_DIR;
nextCalcDir = time + MSECS_CALC_WIND_DIR; 
  int val;
   byte x, reading;

   val = analogRead(PIN_VANE);
   val >>=2;                      
   reading = val;

   for (x=0; x<NUMDIRS; x++) {
      if (adc[x] >= reading)
         break;
   }
   x = (x + dirOffset) % 8;
 
lcd.begin(16, 4);             
//lcd.setCursor(0,0);
//lcd.clear();
//lcd.setCursor(0,0);

lcd.print("U:");
lcd.print(windCounter);
lcd.print(" = ");
lcd.print(windSpeed);
lcd.print(" km/h  "); 
lcd.setCursor(0,1);
lcd.print("aus: ");
lcd.print(strVals[x]);
lcd.setCursor(-4,2);
lcd.print(t/12+1,1);
lcd.print(‚\xDF‘);
lcd.print("C");
lcd.print(" / ");
lcd.print(h/1.19,0);
lcd.print("%");  
lcd.setCursor(-4,3);
lcd.print (bmp.readTemperature()-1);
lcd.print(‚\xDF‘);
lcd.print("C");
lcd.print(" ");
lcd.print (bmp.readPressure()/91.64,1 );
lcd.print(" hPa");

}
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };          
byte ip[] = {
  192, 168, 0, 177};                            

Server server = Server(80);

void setup()
{
  nextCalcDir   = millis() + MSECS_CALC_WIND_DIR;
nextCalcDir = time + MSECS_CALC_WIND_DIR; 
  show_values();
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
 
nextCalcDir   = millis() + MSECS_CALC_WIND_DIR;
nextCalcDir = time + MSECS_CALC_WIND_DIR;
  //zaehler auf 0 stellen
windCounter = 0;
time = millis();
  //zaehl-interrupt aktivieren
attachInterrupt(1,countWind,RISING);
  //abwarten des messzeitraums
delay(1000 * measureTime);
  //zaehl-interrupt deaktivieren
detachInterrupt(1);
  //zeit bestimmen
time = (millis() – time) / 1000;
windSpeed = (float)windCounter / (float)measureTime * windFactor;

//show_values();
int val;
   byte x, reading;

   val = analogRead(PIN_VANE);
   val >>=2;                       
   reading = val;

  
   for (x=0; x<NUMDIRS; x++) {
      if (adc[x] >= reading)
         break;
   }

   x = (x + dirOffset) % 8;
float h = dht.readHumidity();
  float t = dht.readTemperature();
  Client client = server.available();            
  if (client) {                                 
 
    server.print("HTTP/1.0 200 OK\r\nServer: arduino\r\nContent-Type: text/html\r\n\r\n");
    server.println("-");
server.println("-");
    server.println(t/12+1,1);
    server.println(h/1.19,0);
    server.println(bmp.readPressure()/91.64,1 );
    server.println("0.0"); //Regen
    server.println(windSpeed);

    server.println(strVals[x]);
    server.println("Zeit");
    server.println("Datum");  
    server.println(windCounter);     
         
         
         
    delay(100);                                  
    client.stop();                               
  }
  lcd.setCursor(0,0);
  show_values();
 
}