PitClimaWeb PCWE - ESPertino based.

L'utilizzo del PitClimaDataLogger e il caricamento su web tramite WiFi con ESPertino.

 

 

Il PitClimaWeb si evolve e con espertino si svincola dalla rete wired.

Attraverso l'utilizzo della nuova scheda ESPertino prodotta da Elettronica Open Source il PitClimaWeb, si appoggia al WiFi per registrare i parametri climatici ambientali (temperatura, umidità, luminosità e pressione) e caricarli sul sito WEB.

Si tratta della evoluzione del PitClimaWeb che a sua volta prendeva lo spunto 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.

Diversamente dal prededente si utilizza solo il sensore di umidità e temperatura DHT11 per il quale è stata ulizzata la libreria SimpleDHT-1.0.6 che consente il controllo e l'utilizzo dei dati del sensore la libreria DHT utilizzata con arduino non funziona.

Cosa serve:

 

  • ESPertino
  • sensore temperatura umidità
  • led
  • resistenze
  • condensatore da 100nF
  • 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         20/01/2018 -- preimpostazione per utilizzo WEB
ZappocoS          - PitClimaWeb02         27/01/2018 -- caricamento dati WEB 
ZappocoS          - PitClimaWeb02E        27/01/2018 -- modifiche per utilizzo Espertino

PitClimaWeb --> Stazione Climatica - Termo Igrometro del PIT 

//----------------------------------------------------------------------------------------
 
 DHT22 ---- Connection
 DHT11 ---- Connection
 pin 1 VCC-5v
 pin 2 dati --> digital pin 7 
 pin 3 none
 pin 4 GND-GND

*/

//----------------------------------------------------------------------------------------
// librerie sensore di temperatura e umidità DHT22
//----------------------------------------------------------------------------------------
/* 
// Libreria per Arduino
#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 DHT22   // DHT11 DHT22

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

// Libreria per espertino
#include 
int pinDHT11 = 2;  // Pin di collegamento 7 per Arduino
SimpleDHT11 dht11; // Oggetto dht11

//----------------------------------------------------------------------------------------
// 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 WiFi - Espertino
//----------------------------------------------------------------------------------------

#include 
#include 

WiFiMulti WiFiMulti;

#define SOTTOCAMPIONAMENTO 6000
//----------Variabili------
int digitale,k;
// long somma;
// float volt,temperatura,media;

const char* ssid     = "miaSSID";
const char* password = "miaPASSWORD";
const char* host = "www.miosito.me";



/*
//----------------------------------------------------------------------------------------
// librerie Ehernet - Arduino
//----------------------------------------------------------------------------------------

#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.fdt.altervista.org";

// 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 = 5; // 6 per Arduino 5 per Espertino

int flagSts = 0;
int stsDelay = 1000;

byte temperature = 0;
byte humidity = 0;

float Temperatura;
float Umidita;
float IndiceCalore;
float TemperaturaF;
float TemperaturaP;

//----------------------------------------------------------------------------------------
//  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("PitClimaWeb02_TempUmid_1E");
      Serial.println("---------------------------");

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

      WiFiMulti.addAP("miaSSID", "miaPASSWORD");
  
      Serial.println();
      Serial.println();
      Serial.print("Wait for WiFi... ");
  
      while(WiFiMulti.run() != WL_CONNECTED) {
          Serial.print(".");
          delay(500);
      }

      //-----Gestione iniziale WiFi
      // WiFi.begin(ssid, password);
      Serial.println();
      Serial.println("Aspetto 10 secondi per Wifi...");
      //---Lampeggio Led 10 secondi per attesa WiFi-----
      LedLighFlash (ledPin, 100, 100 , 0, 1000);
      
      /*
      for(k=1;k<=100;k++) {
         digitalWrite(5,LOW);  // Accende il Led (e' al contrario) 
         delay(100);
         digitalWrite(5,HIGH);  // Spegne il Led (e' al contrario) 
         delay(100);
      } */
      Serial.print("Wifi collegato, l'IP di ESPertino e':");
      Serial.println(WiFi.localIP());
      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.fdt.altervista.org");
        //client.println("Connection: close");
        //client.println();

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

      Serial.println("---------------------------");
 
      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("Indice Calore [gF]");
      // Serial.print(" ; ");
      // Serial.print("T Percepita [gC]");
      Serial.println("");

      TimeStart  = millis();
      TimeAdesso = TimeStart;
      TimeUltima = TimeStart;
     
}


void loop() {

      TimeAdesso = millis();
      TimeDelta = TimeAdesso - TimeUltima - TimeDelay;
      
      
      if (TimeDelta >= 0) {  
      
         TimeUltima = millis();
         Counter = Counter +1;
         TemperatureSensorRead ();
         PrintSerialData ();

    
      }
  
}

void PrintSerialData () {

      digitalWrite (ledPin, HIGH);

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

      Serial.print(Counter);
      Serial.print(" ; ");
      Serial.print(tempo);
      Serial.print(" ; ");
      Serial.print(Temperatura);
      Serial.print(" ; ");
      Serial.print(Umidita);
      // Serial.print(" ; ");
      // Serial.print(IndiceCalore);
      // Serial.print(" ; ");
      // Serial.print(TemperaturaP);
      Serial.println("");

      String url;
      WiFiClient client;
      // ----------------------------------------------------------------
      // TEMPERATURA - UMIDITA - file logs
      // ----------------------------------------------------------------
      if (client.connect(host, 80)) {
          url = "/PHP_logs_write.php?temperatura=" + String(Temperatura) + "," + String(Umidita) + "," + String(IndiceCalore) + "," + String(TemperaturaP);
          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
      // ----------------------------------------------------------------
      if (client.connect(host, 80)) {
          url = "/PHP_temp_write.php?temperatura=" + String(Temperatura);
          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");
      }
      
      // ----------------------------------------------------------------
      // UMIDITA
      // ----------------------------------------------------------------
      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");
      }
      
      delay(1000);
      digitalWrite (ledPin, LOW);

}


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

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

  // Initialize the sensor
  TemperatureSensorRead ();
  // Solo per Arduino
  // if (flagSts != 2)
  //     Serial.print("DHT");Serial.print(DHTTYPE); Serial.println("  init success");

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

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

//--------------------------------------------------------------------------------------------------------------
// Questa funzione legge lo stato del sensore
// di temperatura/umidita DHT022 o DHT11
//--------------------------------------------------------------------------------------------------------------
void TemperatureSensorRead (){

  tempo =(TimeAdesso-TimeStart);

  /*
  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;
  }

  // L'indice di Calore o Heat Index (HI)
  // Compute heat index
  // Must send in temp in Fahrenheit!
  // 
  // http://www.dst.unina.it/met_cal.htm
  // Il computo del valore dell'indice ha significato solo per 
  // temperature dell'aria più grandi di 80 °F ( 27 °C ),
  // temperature del punto di rugiada (dew point) più grandi di 65 °F (12 °C),
  // e umidità relativa (RH) più grande del 40%.
  //
  if (Temperatura >= 27 && Umidita >= 40 ){
    IndiceCalore = dht.computeHeatIndex(TemperaturaF, Umidita);
    TemperaturaP = (IndiceCalore -32)/1.8;
  }
  else
  {
    IndiceCalore = TemperaturaF;
    TemperaturaP = Temperatura;
  }

  */
  
  // Utilizzo Espertino
  dht11.read(pinDHT11, &temperature, &humidity, NULL); // Legge il sensore
  Temperatura =float(temperature);
  Umidita =float(humidity);

  
  if(OnDebug == true) {    

    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 fa lampeggiare il led di controllo 
//--------------------------------------------------------------------------------------------------------------

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, 2 febbraio 2018