Data Logger

 Data logger Temp / Vocht en 2 analoge ingangen 0-25V



Datalogger met mogelijkheid om elke seconde/minuut of uur te loggen 

Data log file voor SD-kaart is zo opgesteld dat je het eenvoudig kan importeren in excel ( data gescheiden met komma's)



de code 

---------------------------------------------------------------------------------------------------------------------------

/*

Pin Digital 2 -> Pin CE - RST modulo RTC

Pin Digital 3 -> Pin I/O - DAT modulo RTC

Pin Digital 4 -> Pin CLCK - CLK modulo RTC

Pin Analogic A4 -> Pin SDA display

Pin Analogic A5 -> Pin SCL display


*/


#include <DS1302.h> // real time module

#include <Wire.h> //i2c

#include <LiquidCrystal_I2C.h> //lcd display

#include <SPI.h> // voor sd kaart

#include <SD.h> // voor sd kaart


#include "DHT.h" // temp sensor

#define DHTPIN 9 // aansluiting voor temp sensor

#define DHTTYPE DHT22 // temp sensor

DHT dht(DHTPIN, DHTTYPE); // Temp vocht sensor bepalen


int CS_PIN = 10; // cs pin van sd kaart, enige aansluiting die veranderd kan worden


int elkeseconde = 5; // ingangen voor selectie van data logging

int elkeminuut = 6;

int elkuur = 7;


//merker definieren

int merkerelkeseconde = 0;

int merkerelkeminuut = 0;

int merkerelkuur = 0;


const int chipSelect = 4; //aansturing sd kaart


// aansluitingen rtc

DS1302 rtc(2, 3, 4); // io voor RTC module


// adres van display

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


//analoge spannings ingang 1

const int voltageSensorPin = A3; // sensor pin

float vIn; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)

float vOut;

float voltageSensorVal; // value on pin A3 (0 - 1023)

const float factor = 5.00; // reduction factor of the Voltage Sensor shield

const float vCC = 4.67; // Arduino input voltage (measurable by voltmeter)


//ingang 2

const int voltageSensorPin2 = A2; // sensor pin

float vIn2; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)

float vOut2;

float voltageSensorVal2; // value on pin A2 (0 - 1023)

const float factor2 = 5.000; // reduction factor of the Voltage Sensor shield

const float vCC2 = 4.67; // Arduino input voltage (measurable by voltmeter)


Time t; // real time klok definieren in t


void setup()

{

rtc.halt(false);

rtc.writeProtect(false);


Serial.begin(9600);


// indien klok moet ingesteld worden onderste lijnen juist instellen

// rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY

// rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)

// rtc.setDate(6, 8, 2010); // Set the date to August 6th, 2010


//Pin definities

pinMode(CS_PIN, OUTPUT);

pinMode(elkeseconde, INPUT);

pinMode (elkeminuut, INPUT);

pinMode (elkuur, INPUT);


// while (!Serial) {

// ; // wait for serial port to connect. Needed for native USB port only

// }

Serial.print("Initializing SD card...");


// see if the card is present and can be initialized:

if (!SD.begin(chipSelect)) {

Serial.println("Card failed, or not present");


lcd.begin(16, 2); // initialize the lcd

lcd.home (); // go home

lcd.print("Geen SD kaart ");

delay(1000);


// don't do anything more:

while (1);

}

Serial.println("card initialized.");


lcd.begin(16, 2); // initialize the lcd

lcd.clear();

// lcd.home (); // go home

// lcd.setCursor ( 0, 0 ); // go to the next line

lcd.print("SD kaart ");

// lcd.setCursor ( 0, 1 ); // go to the next line

lcd.print("herkend ");

delay(1000);



lcd.begin(16, 2); // initialize the lcd

// lcd.home (); // go home

lcd.setCursor ( 0, 0 ); // go to the next line

lcd.print("Data logger");

lcd.setCursor ( 0, 1 ); // go to the next line

lcd.print (" C2020 Spike ");

delay(1000);

lcd.clear();


dht.begin(); // initialize the sensor

}


void loop()

{

// read humidity

float humi = dht.readHumidity(); // read temperature as Celsius

float tempC = dht.readTemperature(); //float tempF = dht.readTemperature(true);


// check if any reads failed

if (isnan(humi) || isnan(tempC) ) {

Serial.println("Geen data van Temp sensor");

} else {

// Serial.print("Humidity: ");

Serial.print(humi);

Serial.print("%");

Serial.print(" , ");

// Serial.print("Temperature: ");

Serial.print(tempC);

Serial.println("�C ");

}


// Get data from the DS1302

t = rtc.getTime();

// Serial.print(rtc.getDOWStr()); // Dag

//Serial.print(" ");

Serial.print(rtc.getDateStr()); //Datum

Serial.print(" -- ");

Serial.println(rtc.getTimeStr()); //tijd


//Serial.println(t.hour, DEC);

//Serial.println(t.min, DEC);

//Serial.print(t.sec, DEC);


merkerelkeseconde = digitalRead (elkeseconde);

merkerelkeminuut = digitalRead (elkeminuut);

merkerelkuur = digitalRead (elkuur);


//ingang 1

voltageSensorVal = analogRead(voltageSensorPin); // read the current sensor value (0 - 1023)

vOut = (voltageSensorVal / 1024) * vCC; // convert the value to the real voltage on the analog pin

vIn = vOut * factor; // convert the voltage on the source by multiplying with the factor


Serial.print(" U1 = ");

Serial.print(vIn);

// Serial.print("V");


//ingang2

voltageSensorVal2 = analogRead(voltageSensorPin2); // read the current sensor value (0 - 1023)

vOut2 = (voltageSensorVal2 / 1024) * vCC2; // convert the value to the real voltage on the analog pin

vIn2 = vOut2 * factor2; // convert the voltage on the source by multiplying with the factor


Serial.print(" U2 = ");

Serial.println(vIn2);

// Serial.println("V");


lcd.setCursor(0, 0);


lcd.print(humi);

lcd.print(" % ");

lcd.print(tempC);

lcd.print("C ");


lcd.setCursor(0, 1);

lcd.print("V1=");

lcd.print(vIn);

lcd.println(" V");


lcd.setCursor(8, 1);

lcd.print("V2=");

lcd.print(vIn2);

lcd.println(" V");






if (merkerelkeseconde == HIGH){

Serial.println("Data logging elke seconde actief");

lcd.setCursor ( 15, 0 ); // go to the next line

lcd.print("S");

}


if (merkerelkeminuut == HIGH){

Serial.println("Data logging elke minuut actief");

lcd.setCursor ( 15, 0 );

lcd.print("M");

}


if (merkerelkuur == HIGH){

Serial.println("Data logging elk uur actief");

lcd.setCursor ( 15, 0); // go to the next line

lcd.print("H");

}


delay (800);



//logging elke seconde

if (merkerelkeseconde == HIGH) {

dataopslaan(); // Data routine om data op sd kaart op te slaan oproepen

}


//logging elke minuut

if ((merkerelkeminuut == HIGH) and (t.sec==0)) {

dataopslaan(); // Data routine om data op sd kaart op te slaan oproepen

}


//logging elk uur

if ((merkerelkeminuut == HIGH) and (t.min==0)) {

dataopslaan(); // Data routine om data op sd kaart op te slaan oproepen

}


}


void dataopslaan() {


//sd kaart

// open the file. note that only one file can be open at a time,

// so you have to close this one before opening another.

File dataFile = SD.open("datalog.txt", FILE_WRITE);


// if the file is available, write to it:

if (dataFile) {

// dataFile.println(dataString);



dataFile.print(rtc.getDateStr());

dataFile.print(", "); // comma's om cellen later te delen in excel

// dataFile.print(rtc.getDOWStr());

// dataFile.print(", ");

dataFile.print(rtc.getTimeStr());

dataFile.print(", ");

dataFile.print(",ingang 1 =, "); //analoge ingangen

dataFile.print(vIn);

dataFile.print(", ");

dataFile.print(",ingang 2 =, ");

dataFile.print(vIn2);

dataFile.println(", ");



// read humidity

float humi = dht.readHumidity();

float tempC = dht.readTemperature();


dataFile.print(humi);

dataFile.print(",%,");

dataFile.print(tempC);

dataFile.print(",�C,");

dataFile.close();

}


lcd.setCursor(0, 0);

lcd.println("Data Logging ");





/*DISPLAY

float humi = dht.readHumidity();

// read temperature as Celsius

float tempC = dht.readTemperature();

lcd.setCursor(0, 0);


lcd.print(humi);

lcd.print("% ");

lcd.print(tempC);

lcd.print("C");


lcd.setCursor(0, 1);

lcd.print("V1=");

lcd.print(vIn);

lcd.println(" V");


lcd.setCursor(8, 1);

lcd.print("V2=");

lcd.print(vIn2);

lcd.println(" V");

*/

delay (200);

}

-----------------------------------------------------------------------------------------------------------------------------
















Geen opmerkingen:

Een reactie posten

pzem module op Wemos D1 ESP easy

pzem module voorzien van R 1K tussen rx en 5v RX van pzem op D8 TX van pzem op D7 Wemos Config esp easy RX: GPIO-13 (D7) TX: GPIO-15 (D8) ht...