PitClimaWeb PCWE - NodeMcu Lua ESP8266 ESP-12 based.
L'utilizzo del PitClimaDataLogger e il caricamento su web tramite WiFi con NodeMcu Lua ESP8266 ESP-12E.
Il PitClimaWeb aggiunge nuove possibilità e funionalità con NodeMcu Lua ESP8266 ESP-12E la semplificazione è massima.
Attraverso l'utilizzo della scheda NodeMcu Lua ESP8266 ESP-12E il PitClimaWeb, si appoggia al WiFi per registrare i parametri climatici ambientali (temperatura, umidità) e caricarli sul sito WEB.
Si tratta dell'evoluzione del PitClimaWeb il codice sorgente infatti attraverso la compliazione condizionale comprede tutte le precedentei versioni di PitClima e puo' essere caricato sulle varie board semplicemente mettendo o togliendo i commenti alla definizione delle rispettive variabili.
Come nel caso delle ultime versioni di PitClima è stata ulizzata la libreria SimpleDHT-1.0.6 che permette una maggiore compatibilità con le varie schede.
Schema per la compilazione condizionale
Tipo di scheda
- #define ESPertino --> per ESPertino
- #define ESP12 --> per NodeMcu ESP8266 ESP-12
- #define ArduinEhernet --> per Arduino (1R3, Mega o M0Pro) con Ethernet Shield
Tipo di sensore
- #define DHT11 --> per DHT11
- #define DHT22 --> per DHT22
Datalogger su SD card
- #define SDCard --> per scrittura su SD card
Ulteriori controlli di debug
- #define OnDebug --> per stampare sulla seriale ulteriori elementi utili al debug
Cosa serve:
- NodeMcu Lua ESP8266 ESP 12E
- 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
ZappocoS - PitClimaWeb02E 02/02/2018 -- per M0 Pro sostituire Serial. con SerialUSB.
ZappocoS - PitClimaWeb03 11/02/2018 -- aggiunto DHT22 e controllo per tipo di scheda ARDUINO_ARCH_SAMD
ZappocoS - PitClimaWeb03 12/02/2018 -- variabili per controlli #ifdef per compilazione condizionale
ZappocoS - PitClimaWeb04 12/02/2018 -- integrazione di SD
ZappocoS - PitClimaWeb04 13/02/2018 -- compilazione condizionale per tutte le schede
ZappocoS - PitClimaWeb05 18/02/2018 -- aggiunto codice per utilizzo con NODEMCU
PitClimaWeb --> Stazione Climatica - Termo Igrometro del PIT
----------------------------------------------------------------------------------------
DHT ---- Connection
pin 1 VCC-5v
pin 2 dati
pin 3 none
pin 4 GND-GND
----------------------------------------------------------------------------------------
Schema per la compilazione condizionale
define ESPertino
--> ESPertino
Led Pin 23
DHTPin 2
--> Arduino (non ESPertino)
Led Pin 6
DHTPin 7
SDCard 10 --> 4 se su Ethernet
define ArduinEhernet
--> Ehernet Shield present
define DHT11 --> per DHT11
define DHT22 --> per DHT22
define SDCard --> per scrittura su SD card
define OnDebug --> per stampare sulla seriale ulteriori elementi utili al debug
*/
#if defined(ARDUINO_ARCH_SAMD) // for M0 Pro, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
//----------------------------------------------------------------------------------------
// Settaggio flag per stampa su seriale di dati per debug
//----------------------------------------------------------------------------------------
// #define OnDebug // togliere il commento per stampare maggiori dettagli sulla seriale
// ---------------------------------------------------
// impostazione della variabile che serve per selezionare automaticamente l'utilizzo della
// libreria per ESPertino o quella per Arduino --> Ehernet
// commentare la linea che non ricorre
// commentare entrambe per solo seriale
// ---------------------------------------------------
//#define ESPertino
#define ESP12
//#define ArduinEhernet
// ---------------------------------------------------
// impostazione della variabile che serve per selezionare automaticamente l'utilizzo della
// libreria SimpleDHT commentare la linea che non ricorre
// ---------------------------------------------------
#define DHT11 // per DHT11
// #define DHT22 // per DHT22
// ---------------------------------------------------
// impostazione della variabile che serve per selezionare automaticamente l'utilizzo della
// scrittura su scheda SD commentare la linea se non ricorre
// ---------------------------------------------------
// #define SDCard // per SD
//----------------------------------------------------------------------------------------
// librerie sensore di temperatura e umidità DHT22
//----------------------------------------------------------------------------------------
/*
// Libreria per Arduino sostituita da SimpleDHT.h che funziona anche su Espertino e M0Pro
#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
// pin collegamento sensore DHT
#ifdef ESPertino
int pinDHT = 2; // Pin di collegamento 2 per espertino
#endif
#ifdef ArduinEhernet
int pinDHT = 7; // Pin di collegamento 7 per Arduino
#endif
#ifdef ESP12
int pinDHT = 12; // Pin di collegamento 12 per ESP12
#endif
#ifdef DHT11
// per DHT11
SimpleDHT11 DHT; // Oggetto dht11
byte temperature = 0;
byte humidity = 0;
#else
// per DHT22
SimpleDHT22 DHT; // Oggetto dht22
float temperature = 0;
float humidity = 0;
#endif
#ifdef SDCard
//----------------------------------------------------------------------------------------
// librerie SD card
//----------------------------------------------------------------------------------------
#include
#include
File dataFile;
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
#ifdef ArduinEhernet
const int chipSelect = 4;
#else
const int chipSelect = 10;
#endif
#endif
//----------------------------------------------------------------------------------------
// 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
#ifdef ESPertino
//----------------------------------------------------------------------------------------
// librerie WiFi - Espertino
//----------------------------------------------------------------------------------------
#include
#include
WiFiMulti WiFiMulti;
const char* ssid = "miaSSID";
const char* password = "miaPASSWORD";
const char* host = "www.miosito.me";
#endif
#ifdef ArduinEhernet
//----------------------------------------------------------------------------------------
// 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.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;
#endif
#ifdef ESP12
//----------------------------------------------------------------------------------------
// librerie WiFi - ESP12
//----------------------------------------------------------------------------------------
#include
WiFiServer server(80);
const char* ssid = "miaSSID";
const char* password = "miaPASSWORD";
const char* host = "www.miosito.me";
#endif
//----------------------------------------------------------------------------------------
// Variabili di controllo
//----------------------------------------------------------------------------------------
#ifdef ESPertino
int ledPin = 23; // per Espertino
#endif
#ifdef ArduinEhernet
int ledPin = 6; // per Arduino (tutte le versioni)
#endif
#ifdef ESP12
int ledPin = 5; // per ESP12
#endif
int flagSts = 0;
float Temperatura;
float Umidita;
float IndiceCalore;
float TemperaturaF;
float TemperaturaP;
void setup(){
pinMode(ledPin, OUTPUT);
LedLighFlash (ledPin, 5, 100 , 0, 2000);
Serial.begin(9600);
// Serial.println ("ARDUINO_ARCH_SAMD ");
// Serial.println (ARDUINO_ARCH_SAMD);
delay (2000);
Serial.println("");
Serial.println("");
Serial.println("");
Serial.println("PitClimaDataLoggerWeb V5.02 - by Zappoco");
Serial.println("---------------------------");
InitializeSensor ();
LedLighFlash (ledPin, 5, 100 , 0, 2000);
Serial.print(" availableMemory ... ");
Serial.println(availableMemory ());
#ifdef SDCard
//
// Inizializzazione SD card
//
Serial.print(" initializing SD card ...");
pinMode(chipSelect, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(" card failed, or not present");
// don't do anything more:
LedLighFlash (ledPin, 6, 100 , 0, 100);
flagSts = 1;
return;
}
else
{
Serial.println(" card ok");
}
#endif
#if defined(ESPertino)
// #ifdef ESPertino
//
// Inizializzazione WiFi per l'utilizzo con ESPertino
//
WiFiMulti.addAP(ssid, password);
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);
Serial.print(" Wifi collegato, l'IP di ESPertino e' ");
Serial.println(WiFi.localIP());
Serial.println();
#elif defined(ArduinEhernet)
//#ifdef ArduinEhernet
//
// Inizializzazione Scheda di rete per l'utilizzo con ESPertino
//
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(" failed to configure Ethernet using DHCP");
LedLighFlash (ledPin, 6, 100 , 0, 100);
// 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);
delay(1000);
// if you get a connection, report back via serial:
if (client.connect(host, 80)) {
Serial.println(" ok");
String url = "/PHP_logs_write.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 {
// if you didn't get a connection to the server:
Serial.println(" connection failed");
LedLighFlash (ledPin, 12, 100 , 0, 100);
}
#elif defined(ESP12)
//#ifdef ESP12
//
// Inizializzazione Scheda di rete per l'utilizzo con ESP12
//
// Connect to WiFi network
WiFi.mode(WIFI_STA);
// Serial.println();
// Serial.println();
Serial.print(" connecting to ");
Serial.print(ssid);
// LedLighFlash (ledPin, 2, 2000 , 0, 1000);
// LedLighFlash (ledPin, 3, 100 , 0, 100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
//delay(500);
delay(125);
Serial.print(".");
digitalWrite(ledPin, HIGH);
delay(125);
digitalWrite(ledPin, LOW);
}
Serial.println("");
Serial.print(" WiFi connected ... ");
// Start the server
server.begin();
LedLighFlash (ledPin, 4, 2000 , 0, 1000);
LedLighFlash (ledPin, 3, 100 , 0, 100);
Serial.print(" Server started ... ");
// Print the IP address
Serial.println(WiFi.localIP());
LedLighFlash (ledPin, 5, 2000 , 0, 1000);
LedLighFlash (ledPin, 3, 100 , 0, 100);
#endif
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;
#ifdef SDCard
//
// Apertura file e scrittura intestazione
//
dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("----------------------------------------------");
dataFile.println("PitClimaDataLoggerWeb V5.02 - by Zappoco");
dataFile.println("----------------------------------------------");
dataFile.println("-------- Inizializzazione terminata ----------");
dataFile.println("-------- sostituire . con , ----------");
dataFile.println("----------------------------------------------");
dataFile.println("");
dataFile.print("N.");
dataFile.print(" ; ");
dataFile.print("Tempo [ore]");
dataFile.print(" ; ");
dataFile.print("Temperatura [gC]");
dataFile.print(" ; ");
dataFile.print("Umidita [%]");
// dataFile.print(" ; ");
// dataFile.println("Indice di calore [gF]");
dataFile.println("");
dataFile.close();
}
#endif
}
void loop() {
TimeAdesso = millis();
TimeDelta = TimeAdesso - TimeUltima - TimeDelay;
if (TimeDelta >= 0) {
TimeUltima = millis();
Counter = Counter +1;
TemperatureSensorRead ();
PrintClimaData ();
}
}
//--------------------------------------------------------------------------------------------------------------
// Questa funzione stampa i dati dei sensori
//
// --> seriale
// --> WEB
// --> SD card
//--------------------------------------------------------------------------------------------------------------
void PrintClimaData () {
digitalWrite (ledPin, HIGH);
#ifdef OnDebug
Serial.print("PrintSerialData ");
Serial.print("flagSts ");
Serial.println(flagSts);
#endif
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;
#ifdef ESPertino
WiFiClient client;
#endif
#ifdef ESP12
WiFiClient client = server.available();
#endif
#if defined(ESPertino) || defined(ArduinEhernet) || defined(ESP12)
#ifdef OnDebug
Serial.println("Scrive dati su WEB");
#endif
// ----------------------------------------------------------------
// 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
{
LedLighFlash (ledPin, 6, 100 , 0, 100);
Serial.println(" error 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
{
LedLighFlash (ledPin, 6, 100 , 0, 100);
Serial.println(" error not writing on Host 2");
}
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
{
LedLighFlash (ledPin, 6, 100 , 0, 100);
Serial.println(" error not writing on Host 3");
}
// ----------------------------------------------------------------
// 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
{
LedLighFlash (ledPin, 6, 100 , 0, 100);
Serial.println(" error not writing on Host 4");
}
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
{
LedLighFlash (ledPin, 6, 100 , 0, 100);
Serial.println(" error not writing on Host 5");
}
#endif
delay(1000);
digitalWrite (ledPin, LOW);
#ifdef SDCard
//
// Inizializzazione SD card
//
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(Counter);
dataFile.print(" ; ");
dataFile.print(tempo - TimeDelay);
dataFile.print(" ; ");
dataFile.print(Temperatura);
dataFile.print(" ; ");
dataFile.print(Umidita);
// dataFile.print(" ; ");
// dataFile.print(IndiceCalore);
// dataFile.print(" ; ");
// dataFile.print(TemperaturaP);
dataFile.println("");
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
LedLighFlash (ledPin, 8, 100 , 0, 100);
}
#endif
}
//--------------------------------------------------------------------------------------------------------------
// Inizializzazione del sensore
//--------------------------------------------------------------------------------------------------------------
void InitializeSensor (){
Serial.print(" initializing ... ");
// Initialize the sensor
TemperatureSensorRead ();
Serial.println(" ok");
LedLighFlash (ledPin, 3, 300 , 2, 1000);
}
//--------------------------------------------------------------------------------------------------------------
// Questa funzione legge lo stato del sensore
// di temperatura/umidita DHT022 o DHT11
//--------------------------------------------------------------------------------------------------------------
void TemperatureSensorRead (){
tempo =(TimeAdesso-TimeStart);
int err = SimpleDHTErrSuccess;
#ifdef DHT11
// per DHT11
Serial.print ("DHT11read ");
err = DHT.read(pinDHT, &temperature, &humidity, NULL); // Legge il sensore dht11
#else
// per DHT22
Serial.print ("DHT22read2 ");
err = DHT.read2(pinDHT, &temperature, &humidity, NULL); // Legge il sensore dht22
#endif
if (err != SimpleDHTErrSuccess) {
Serial.print(" failed, err = ");
Serial.print(err);
Serial.print(" ");
LedLighFlash (ledPin, 10, 100 , 0, 100);
}
Temperatura =float(temperature);
Umidita =float(humidity);
/*
codice per l'utilizzo della libreria DHT.h ora sostituita da SimpeDHT
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;
}
*/
#ifdef OnDebug
Serial.print(Counter);
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);
Serial.println("");
#endif
}
//--------------------------------------------------------------------------------------------------------------
// Questa funzione fa lampeggiare il led di controllo
//
// iLedPin --> numero del pin cui è collegato il Led
// nLampLed --> numero di lampeggi
// delayLampLed --> durata del lampeggio
// mLampLed --> incremento della durata ad ogni ciclo
// delayLampLedDopo --> pausa dopo la fine del ciclo di lampeggiamento
//--------------------------------------------------------------------------------------------------------------
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, 18 febbraio 2018