MeteoPi

Ensoleillement

Le Matériel

Pour donner une idée de l'ensoleillement j'utilise 2 capteurs:

  1. Capteur de lumière TSL2591
  2. Capteur UV Grove GUVA-S12D
Le Montage
Le Script
GetLux.ino
/* TSL2591 Digital Light Sensor */
/* Dynamic Range: 600M:1 */
/* Maximum Lux: 88K */
 
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
 
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); 
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Timers
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
unsigned long CurrentMillis  = 0;     // Millisseconde courante
unsigned long NextMillis  = 0;        // Millisseconde courante
unsigned long UVMillis  = 0;          // Millisseconde courante
unsigned long NextUVMillis  = 0;      // Millisseconde courante
 
 
#define INTERVAL 30000 ;		// 30 secondes
#define UVTime 500 ;			// 0.5 secondes
#define NOM "GetLux"
#define Version "1.009"
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
int BAUD = 9600;
char modele[] = "RedBoard";
char buffer[80];
const byte SENSOR_UV = A0;
unsigned int SumValue = 0;
int count = 0;
/**************************************************************************/
/*
    Program entry point for the Arduino sketch
*/
/**************************************************************************/
void setup(void)
{
  Serial.begin(BAUD);
  //Serial.print(F("#! Starting Adafruit TSL2591\t"));
  sprintf (buffer, "#! %s Version: %s\t\t", NOM, Version);
  Serial.print(buffer);
  if (tsl.begin())
  {
    Serial.println(F("| Found a TSL2591 sensor"));
  }
  else
  {
    Serial.println(F("| No sensor found ... check your wiring?"));
    while (1);
  }
  /* Configure the sensor */
  tsl.setGain(TSL2591_GAIN_LOW);    // 1x gain (bright light)
  tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS);  // shortest integration time (bright light)
  SumValue = 0;
  count = 0;
  NextMillis = millis();
  NextUVMillis = millis();
}
/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void)
{
  CurrentMillis = millis();
  if (CurrentMillis > NextUVMillis)
  {
    SumValue += analogRead(SENSOR_UV);
    count ++;
    NextUVMillis = CurrentMillis + UVTime ;
  }
 
  if (CurrentMillis > NextMillis)
  {
    float sensorValue;
    int analogValue = SumValue / count;
    SumValue = 0;
    count = 0;
    if (analogValue < 20)
    {
      sensorValue = 0;
    }
    else
    {
      sensorValue = 0.05 * analogValue - 1;
      if ( sensorValue > 11)
      {
        sensorValue = 11.00;
      }
    }
    sensors_event_t event;
    tsl.getEvent(&event);
    if ((event.light > 4294966000.0) | (event.light < -4294966000.0))
    {
      /* If event.light = 0 lux the sensor is probably saturated */
      /* and no reliable data could be generated! */
      /* if event.light is +/- 4294967040 there was a float over/underflow */
      Serial.println("#! Invalid data (adjust gain or timing)");
    }
    else
    {
      Serial.print("Lux = ");
      uint32_t Lux = event.light;
      Serial.print(Lux);
      Serial.print(" | UV = ");
      Serial.print(sensorValue);
      Serial.print(" | IR = ");
      Serial.print(tsl.getLuminosity(TSL2591_INFRARED));
      Serial.print(" | Rayonnement = ");
      Serial.print(tsl.getLuminosity(TSL2591_FULLSPECTRUM));
      Serial.print(" | Visible = ");
      Serial.print(tsl.getLuminosity(TSL2591_VISIBLE));
      Serial.println(" | ");
    }
    NextMillis = CurrentMillis + INTERVAL ;
  }
}
meteoduino/luxmetre.txt · Dernière modification: 14/07/2017 18:37 par michel
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5