PitClimaWeb PCW - datalogger con caricamento su sito internet dei dati climatici.

L'utilizzo del PitClimaDataLogger e il caricamento su web dei dati climatici all'interno di un sito INTERNET.

 

 

Il PitClimaDataLogger diventa PitClimaWeb

PitClimaWeb registra i parametri climatici ambientali (temperatura, umidità, luminosità e pressione) e questi sono resi visibili sul sito WEB e memorizzati in un datalogger sempre sul WEB dal quale possono essere scaricati per le verifiche o le elaborazioni necessarie.

Lo spunto nasce dall'articolo di Giovanni di Maria "Monitoraggio della temperatura con ESPertino in WiFi con consultazione su Internet" pubblicato su numero 37 di Elettronica Open Source.

Il prossimo passo infatti sarà quello di caricate tutto su ESPertino in modo da passare alla rete WIFI.

E' stato utilizzato Arduino Mega in quanto su Arduino uno lo spazio occupato dalle variabili non è sufficiente e occorre fare una ottimizzazione del software o delle funzioni......

Cosa serve:

 

  • Arduino Mega
  • Sensore temperatura umidità
  • Sensore luminosità
  • Sensore pressionee temperatura
  • Scheda di rete SHIELD da montare su Arduino
  • led
  • resistenze
  • cavetti

Codice sorgente:



/*

ZappocoJ - ZappocoS - PitClimaDataLogger01  26/01/2015 -- Temperatura e umidità
ZappocoJ - ZappocoS - PitClimaDataLogger02  28/02/2015 -- aggiunta luminosità
ZappocoS          - PitClimaDataLogger03  11/04/2015 -- aggiunta sensore pressione e passaggio a Arduino Mega
ZappocoS          - PitClimaWeb01  23/01/2018 -- preimpostazione per utilizzo WEB
ZappocoS          - PitClimaWeb01  23/01/2018 -- preimpostazione per utilizzo WEB 
ZappocoS          - PitClimaWeb02  27/01/2018 -- caricamento dati WEB 

PitClimaWeb --> Stazione Climatica - Termo Igrometro del PIT 

//----------------------------------------------------------------------------------------

Settaggi:

Set TimeDelay --> 1 (3) 5 10 15 20 30 60 minuti () default
Set Start Log --> fa partire la registrazione
Set Stop Log  --> ferma la registrazione
	 
Status:
1) Error   --> no card present
2) running --> registrazione in corso (permette stop)
3) stop    --> permette settaggio time e start

 
//----------------------------------------------------------------------------------------
 
 DHT22 ---- Connection
 pin 1 VCC-5v
 pin 2 dati --> digital pin 7 
 pin 3 none
 pin 4 GND-GND
 
 
 BH1750 ---- Connection
 VCC-5v
 GND-GND
 SCL-SCL(analog pin 5)
 SDA-SDA(analog pin 4)
 ADD-NC or GND
 
*/

//----------------------------------------------------------------------------------------
// librerie sensore di temperatura e umidità DHT22
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// librerie sensore di temperatura e umidità DHT22
//----------------------------------------------------------------------------------------
#include 

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHTPIN 7        // Numero del pin connesso
#define DHTTYPE DHT11   // DHT11 DHT22

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

//----------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------
//  libreria sensore di pressione BMP180
//----------------------------------------------------------------------------------------
#include 
#include 

SFE_BMP180 pressure;
#define ALTITUDE 205.0 // Altitude of Rovereto Italy

//----------------------------------------------------------------------------------------
//  libreria sensore di luminosità BH1750
//----------------------------------------------------------------------------------------
#include 
BH1750 lightMeter;

//----------------------------------------------------------------------------------------
// Variabili temporali
//----------------------------------------------------------------------------------------


unsigned long TimeDelay = 10000;        // setta a 3 minuti (180000 millisecondi) il tempo di delay tra le letture
unsigned long TimeStart = 0;             // tempo di partenza 
unsigned long TimeUltima = 0;            // tempo ultima lettura
unsigned long TimeAdesso = 0;            // tempo della lettura
long TimeDelta  = 0;                     // differenza
unsigned long tempo;

unsigned long Counter = 0;               // contatore delle letture


//----------------------------------------------------------------------------------------
// librerie Ehernet
//----------------------------------------------------------------------------------------

#include 
#include 

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // mac address

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
char host[] = "www.miosito.me";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,188,51);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;


//----------------------------------------------------------------------------------------
// Variabili per errori
//----------------------------------------------------------------------------------------

int ledPin = 6;

int flagSts = 0;
int stsDelay = 1000;

int flagDHT22 = true;

float Temperatura;
float Umidita;
float IndiceCalore;
float TemperaturaF;
float TemperaturaP;
float Luminosita;
float Temperatura2;
float Pressione;


//----------------------------------------------------------------------------------------
//  Settaggio flag per stampa su seriale di dati per debug
//----------------------------------------------------------------------------------------

boolean OnDebug = false;    


void setup(){


      pinMode(ledPin, OUTPUT);

      LedLighFlash (ledPin, 5, 100 , 0, 2000);

      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
      Serial.println("PitClimaDataLoggerXP v02");
      
      InitializeSensor ();

      LedLighFlash (ledPin, 5, 100 , 0, 2000);
      
      Serial.print("availableMemory : ");
      Serial.println(availableMemory ());
      Serial.println("---------------------------");

      //----------------------------------------------------------------------------------------
      // start the Ethernet connection:
      //----------------------------------------------------------------------------------------
      if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip);
      }
      // give the Ethernet shield a second to initialize:
      delay(1000);
      Serial.print("connecting to Host: ... ");
      // Serial.print(host);
      Serial.print(" ... ");

      delay(1000);
      // if you get a connection, report back via serial:
      if (client.connect(host, 80)) {
        Serial.println(" connected");
        // Make a HTTP request:

        //chiamata su più righe
        //client.println("GET /registra.php?temperatura=99.99 HTTP/1.1");
        //client.println("Host: www.miosito.me");
        //client.println("Connection: close");
        //client.println();

        // crea la stringa per la chiamata su una sola linea
        String url = "/PHP_registra.php?temperatura=PARTENZA";
        client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
        client.stop();
      }
      else {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
      

      Serial.println("");
      Serial.print("N.");
      Serial.print(" ; ");
      Serial.print("Tempo [ore]");
      Serial.print(" ; ");
      Serial.print("Temperatura [gC]");
      Serial.print(" ; ");
      Serial.print("Umidita' [%]");
      Serial.print(" ; ");
      Serial.print("Luminosita' [lx]");
      Serial.print(" ; ");
      Serial.print("Pressione [mb]");
      Serial.print(" ; ");
      Serial.println("Temperatura2 [gC]");
}


void loop() {

    //Serial.print("flagSts ");
    //Serial.println(flagSts);
    // flagSts = 2;
    switch(flagSts)

    {

    // Errore inizializzazione SD CARD
    case 1:
       Serial.println(" Error ");
       digitalWrite(ledPin, HIGH);
       delay(200);               
       digitalWrite(ledPin, LOW); 
       delay(200);               
  
       break; 

    // Inizializzazione parametri
    case 0:   
    
      TimeStart  = millis();
      TimeAdesso = TimeStart;
      TimeUltima = TimeStart;
      if(OnDebug == true) {       
        Serial.println("---------------------------");
        Serial.println("Tempi iniziali");
        Serial.println("---------------------------");
      
        Serial.print("TimeDelay ");
        Serial.println(TimeDelay);
          
        Serial.print("TimeStart ");
        Serial.println(TimeStart);
          
        Serial.print("TimeUltima ");
        Serial.println(TimeUltima);
      
        Serial.print("TimeAdesso ");
        Serial.println(TimeAdesso);
      
        Serial.print("TimeDelta ");
        Serial.println(TimeDelta);
    
        Serial.println("---------------------------");
        Serial.println("Fine Tempi iniziali");
        Serial.println("---------------------------");
   
        Serial.println("---------------------------");
        Serial.println("Apertura file datalog.txt per scrittura titoli colonne");
        Serial.println("---------------------------");
      }      

      flagSts = 2;
      break; 
    // Ciclo di lettura normale
    case 2:   
      
        if (TimeDelta >= 0) {  

        
           tempo =(TimeAdesso-TimeStart);
           TimeUltima = millis();
           Counter = Counter +1;
           TemperatureSensorRead ();
           LightSensorRead ();
           PressureSensorRead ();
           PrintSerialData ();
           digitalWrite(ledPin, HIGH);
           delay(1000);               
           digitalWrite(ledPin, LOW); 
           
        }

    default: 
       break;    

  }// End Case

  TimeAdesso = millis();
  TimeDelta = TimeAdesso - TimeUltima - TimeDelay;
}

void PrintSerialData () {

    if(OnDebug == true) {  
      Serial.print("PrintSerialData ");
      Serial.print("flagSts ");
      Serial.println(flagSts);
      Serial.print("flagDHT22 ");
      Serial.println(flagDHT22);
    }
                   
    if (flagSts == 2){
      if (flagDHT22) {

         Serial.print(Counter);
         Serial.print(" ; ");
         Serial.print(tempo);
         Serial.print(" ; ");
         Serial.print(Temperatura);
         Serial.print(" ; ");
         Serial.print(Umidita);
         Serial.print(" ; ");
         Serial.print(Luminosita);
         Serial.print(" ; ");
         Serial.print(Pressione);
         Serial.print(" ; ");
         Serial.println(Temperatura2);

          // if (client.available()) {
          String url;
    
          if (client.connect(host, 80)) {
              url = "/PHP_logs_write.php?temperatura=" + String(Temperatura) + "," + String(Umidita) + "," + String(Luminosita) + "," + String(Pressione)+ "," + String(Temperatura2);
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
          }
          else
          {
              Serial.println(" not writing on Host 1");
          }
          
  
          // ----------------------------------------------------------------
          // TEMPERATURA
          // ----------------------------------------------------------------
          delay (200);
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_temp_write.php?temperatura=" + String(int(Temperatura));
              // client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close ");
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
          }
          else
          {
              Serial.println(" not writing on Host 3");
          }
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_temp_merge.php";
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
    
          }
          else
          {
              Serial.println(" not writing on Host 4");
          }
          
          // ----------------------------------------------------------------
          // PRESSIONE
          // ----------------------------------------------------------------
          delay (200);
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_pres_write.php?temperatura=" + String(int(Pressione));
              // client.print(String("GET ") + url + " HTTP/1.1 " + "Host: " + host + " " + "Connection: close ");
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
          }
          else
          {
              Serial.println(" not writing on Host 3");
          }
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_pres_merge.php";
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
    
          }
          else
          {
              Serial.println(" not writing on Host 4");
          }
          
          // ----------------------------------------------------------------
          // UMIDITA
          // ----------------------------------------------------------------
          delay (200);
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_umid_write.php?temperatura=" + String(int(Umidita));
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
          }
          else
          {
              Serial.println(" not writing on Host 3");
          }
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_umid_merge.php";
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
    
          }
          else
          {
              Serial.println(" not writing on Host 4");
          }
          
    
          // ----------------------------------------------------------------
          // LUMINOSITA
          // ----------------------------------------------------------------
          delay (200);
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_lumi_write.php?temperatura=" + String(int(Luminosita));
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
          }
          else
          {
              Serial.println(" not writing on Host 3");
          }
          
          if (client.connect(host, 80)) {
    
              url = "/PHP_lumi_merge.php";
              client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
              client.stop();
    
          }
          else
          {
              Serial.println(" not writing on Host 4");
          }
          
  
         
      }
    }
}


//--------------------------------------------------------------------------------------------------------------
// Inizializzazione
//--------------------------------------------------------------------------------------------------------------
void InitializeSensor (){

  Serial.println("---------------------------");
  Serial.println("initializing");
  Serial.println("---------------------------");

  // Initialize the sensor
  lightMeter.begin();
  Serial.println("BH1750 init success");

  LedLighFlash (ledPin, 1, 200 , 1, 1000);
  
  // Initialize the sensor (it is important to get calibration values stored on the device).
  if (pressure.begin()){
      Serial.println("BMP180 init success");
      LedLighFlash (ledPin, 2, 200 , 1, 1000);
  }
  else {
      
      // Oops, something went wrong, this is usually a connection problem,
      // see the comments at the top of this sketch for the proper connections.
  
      Serial.println("BMP180 init fail\n\n");
      
      
      while(1); // Pause forever.
  }
  // Initialize the sensor
  Serial.print("DHT");Serial.print(DHTTYPE); Serial.println("  init success");

  TemperatureSensorRead ();

  LedLighFlash (ledPin, 3, 200 , 1, 2000);

  Serial.println("---------------------------");
}


//--------------------------------------------------------------------------------------------------------------
// Questa funzione legge lo stato del sensore
// di luminosità BH1750
//--------------------------------------------------------------------------------------------------------------
void LightSensorRead (){

    //digitalWrite (ledLightPin, HIGH);
    uint16_t lux = lightMeter.readLightLevel();
    if(OnDebug == true) {    
      Serial.println("---------------------------");
      Serial.println("response from BH1750");
      Serial.println("---------------------------");
      Serial.print("Light: ");
      Serial.print(lux);
      Serial.println(" lx");
      
    }
    Luminosita = lux;

}

//--------------------------------------------------------------------------------------------------------------
// Questa funzione legge lo stato del sensore
// di temperatura/umidita DHT022 o DHT11
//--------------------------------------------------------------------------------------------------------------
void TemperatureSensorRead (){
  
  Umidita = dht.readHumidity();
  // Read temperature as Celsius
  Temperatura = dht.readTemperature();
  // Read temperature as Fahrenheit
  TemperaturaF = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(Umidita) || isnan(Temperatura) || isnan(TemperaturaF)) {
    Serial.println("Failed to read from DHT sensor!");
    flagSts = 2;
    return;
  }
  tempo =(TimeAdesso-TimeStart);

  // L'indice di Calore o Heat Index (HI)
  // Compute heat index
  // Must send in temp in Fahrenheit!
  IndiceCalore = dht.computeHeatIndex(TemperaturaF, Umidita);
  TemperaturaP = (IndiceCalore -32)/1.8;

  if(OnDebug == true) {    

    Serial.println("---------------------------");
    Serial.println("response from DHT");
    Serial.println("---------------------------");
  
  
    Serial.print(Counter -1);
    Serial.print(" ; ");
    Serial.print(tempo - TimeDelay);
    Serial.print(" ; ");
    Serial.print(Temperatura);
    Serial.print(" ; ");
    Serial.print(Umidita);
    Serial.print(" ; ");
    Serial.print(IndiceCalore);
    Serial.print(" ; ");
    Serial.println(TemperaturaP);
    
  }

}



//--------------------------------------------------------------------------------------------------------------
// Questa funzione legge lo stato del sensore
// di pressione BMP180
//--------------------------------------------------------------------------------------------------------------
void PressureSensorRead (){

    char status;
    double T,P,p0,a;

    // Loop here getting pressure readings every 10 seconds.
  
    // If you want sea-level-compensated pressure, as used in weather reports,
    // you will need to know the altitude at which your measurements are taken.
    // We're using a constant called ALTITUDE in this sketch:

    if(OnDebug == true) {    

      Serial.println("---------------------------");
      Serial.println("response from BMP180");
      Serial.println("---------------------------");
      Serial.print("provided altitude: ");
      Serial.print(ALTITUDE,0);
      Serial.print(" meters, ");
      Serial.print(ALTITUDE*3.28084,0);
      Serial.println(" feet");
    }
    
    // If you want to measure altitude, and not pressure, you will instead need
    // to provide a known baseline pressure. This is shown at the end of the sketch.
  
    // You must first get a temperature measurement to perform a pressure reading.
    
    // Start a temperature measurement:
    // If request is successful, the number of ms to wait is returned.
    // If request is unsuccessful, 0 is returned.
  
    status = pressure.startTemperature();
    if (status != 0)
    {
      // Wait for the measurement to complete:
      delay(status);
  
      // Retrieve the completed temperature measurement:
      // Note that the measurement is stored in the variable T.
      // Function returns 1 if successful, 0 if failure.
  
      status = pressure.getTemperature(T);
      if (status != 0)
      {
        
        if(OnDebug == true) {    
          // Print out the measurement:
          Serial.print("temperature: ");
          Serial.print(T,2);
          Serial.print(" deg C, ");
          Serial.print((9.0/5.0)*T+32.0,2);
          Serial.println(" deg F");
        }
        Temperatura2 = T;
        
        // Start a pressure measurement:
        // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
        // If request is successful, the number of ms to wait is returned.
        // If request is unsuccessful, 0 is returned.
  
        status = pressure.startPressure(3);
        if (status != 0)
        {
          // Wait for the measurement to complete:
          delay(status);
  
          // Retrieve the completed pressure measurement:
          // Note that the measurement is stored in the variable P.
          // Note also that the function requires the previous temperature measurement (T).
          // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
          // Function returns 1 if successful, 0 if failure.
  
          status = pressure.getPressure(P,T);
          if (status != 0)
          {
            if(OnDebug == true) {                
              // Print out the measurement:
              Serial.print("absolute pressure: ");
              Serial.print(P,2);
              Serial.print(" mb, ");
              Serial.print(P*0.0295333727,2);
              Serial.println(" inHg");
            }
            Pressione = P;
  
            // The pressure sensor returns abolute pressure, which varies with altitude.
            // To remove the effects of altitude, use the sealevel function and your current altitude.
            // This number is commonly used in weather reports.
            // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
            // Result: p0 = sea-level compensated pressure in mb
  
            if(OnDebug == true) {    

              p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
              Serial.print("relative (sea-level) pressure: ");
              Serial.print(p0,2);
              Serial.print(" mb, ");
              Serial.print(p0*0.0295333727,2);
              Serial.println(" inHg");
            }
  
            // On the other hand, if you want to determine your altitude from the pressure reading,
            // use the altitude function along with a baseline pressure (sea-level or other).
            // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
            // Result: a = altitude in m.
  
            if(OnDebug == true) {    

              a = pressure.altitude(P,p0);
              Serial.print("computed altitude: ");
              Serial.print(a,0);
              Serial.print(" meters, ");
              Serial.print(a*3.28084,0);
              Serial.println(" feet");
            }
          }
          else Serial.println("error retrieving pressure measurement\n");
        }
        else Serial.println("error starting pressure measurement\n");
      }
      else Serial.println("error retrieving temperature measurement\n");
    }
    else Serial.println("error starting temperature measurement\n");
  
    //delay(5000);  // Pause for 5 seconds.
}

void LedLighFlash (int iLedPin, int nLampLed, int delayLampLed, int mLampLed, int delayLampLedDopo){
  for (int iLampLed=0; iLampLed< nLampLed; iLampLed++)
      {
        digitalWrite(iLedPin, HIGH);
        delay(delayLampLed *(iLampLed * mLampLed +1));               
        digitalWrite(iLedPin, LOW); 
        delay(delayLampLed *(iLampLed * mLampLed +1));               
      }
  delay (delayLampLedDopo);
  }

// this function will return the number of bytes currently free in RAM
// written by David A. Mellis
// based on code by Rob Faludi http://www.faludi.com
int availableMemory() {
  int size = 1024; // Use 2048 with ATmega328
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}

 

 

 

Scarica tutto quello che serve:

 

 

 

ZappocoS, 27 gennaio 2018