lunedì 24 agosto 2015

DATA_LOG



#include <SPI.h>
#include <SD.h>
#include <dht11.h>
#include <Wire.h>
#include <DS3231.h>

#define DHT11_PIN 2
#define chipSelect_pin 4

DS3231 clock;
RTCDateTime dt;
dht11 DHT;

String SD_data = "";

void setup() {
  Serial.begin(9600);
  clock.begin();
  Serial.print("Preparazione SD card...");
  if (!SD.begin(chipSelect_pin)) {
    Serial.println("Preparazione fallita o SD non presente");
    return;
  }
  Serial.println("SD pronta.");
 SD_data +="Data \t Ora \t \t Temperatura in C \t \t Umidita' in %";
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
   if (dataFile) {
    dataFile.println(SD_data);
    dataFile.close();
    // print to the serial port too:
     Serial.println(SD_data);

     SD_data="";
  }

}

void loop() {
  // preparo la stringa da scrivere nel SD
 
  dt = clock.getDateTime();

  SD_data +=dt.day;    SD_data +="-";
  SD_data +=dt.month;  SD_data +="-";
  SD_data +=dt.year;   SD_data +="\t";
  SD_data +=dt.hour;   SD_data +=":";
  SD_data +=dt.minute; SD_data +="\t";

/*  SD_data += "RTC Temperatura: ";
  SD_data += clock.readTemperature();
  SD_data += " C \t";*/
 
  DHT.read(DHT11_PIN);
  SD_data += "Temperatura in C: \t";
  SD_data += (DHT.temperature);
  SD_data += "\t Umidita' in %: \t";
  SD_data += (DHT.humidity);

  // 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(SD_data);
    dataFile.close();
    // print to the serial port too:
     Serial.println(SD_data);

     SD_data="";
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("File datalog.txt non accessibile");
  }
  delay(60000);
}










giovedì 20 agosto 2015

test funzionamento DS3231 RTC no library


#include "Wire.h"
#define DS1307_ADDRESS 0x68

void setup(){
  Wire.begin();
  Serial.begin(9600);
}

void loop(){
  printDate();
  delay(1000);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(weekDay);
  Serial.print(" ");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

mercoledì 19 agosto 2015

Arduino DTH11

#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 4

void setup(){
  Serial.begin(9600);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop(){
  int chk;
  Serial.print("DHT11, \t");
  chk = DHT.read(DHT11_PIN);    // READ DATA
  switch (chk){
    case DHTLIB_OK: 
                Serial.print("OK,\t");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.print("Checksum error,\t");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.print("Time out error,\t");
                break;
    default:
                Serial.print("Unknown error,\t");
                break;
  }
 // DISPLAT DATA
  Serial.print(DHT.humidity,1);
  Serial.print(",\t");
  Serial.println(DHT.temperature,1);

  delay(1000);
}

Arduino primi programmi

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor

 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  if (buttonState==1) {
    Serial.println(buttonState);
    digitalWrite(led, HIGH);
  }
  else { 
    Serial.println(buttonState);
    digitalWrite(led, LOW);
  }
  delay(100);        // delay in between reads for stability
}