Arduino + Ethernet Shield + PIR + EMAIL Alarm

4

Posted by rdm | Posted in ARDUINO | Posted on 16-07-2014

Etiquetas:

Una función interesante del Arduino es fabricarte una alarma casera a través del sensor PIR. El objetivo es que el sensor PIR detecta presencia y a través del módulo Ethernet se manda un correo electrónico que te avisa a tu móvil.

Material necesario:

– Arduino R3.

– Ethernet Shield.

-Sensor PIR.

Y disponer de una conexión de internet claro :).

 

Comento algunas líneas. Lo he probado con un correo de Godaddy y llegan las alarmas.

 

Foto del proyecto.

Código a grabar en Arduino:

 

#include <SPI.h>

#include <Ethernet.h>

 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xAF, 0xCE, 0xDD }; // La mac que quieras usar.

byte ip[] = { 192, 168, 1, 109 }; La ip dentro de tu router.

byte gateway [] = { 192,168,1,254 }; // Puede que no haga falta esta línea.

byte subnet [] = { 255,255,255,0 };

byte server[] = { 68, 178, 252, 101 };//  Dirección del servidor smtp. En este caso de Godaddy. Se sabe haciendo un ping al servidor ( smtpout.secureserver.net )

EthernetClient client;

 

/////////////////////////////

//VARS

//Tiempo que damos para calibrar el sensor. (10-60 segundos) Para pruebas pon poco luego ya lo dejas en 60 para activar la alarma y salir.

int calibrationTime = 60;

 

//the time when the sensor outputs a low impulse

long unsigned int lowIn;

 

//the amount of milliseconds the sensor has to be low

//before we assume all motion has stopped

long unsigned int pause = 5000;

 

boolean lockLow = true;

boolean takeLowTime;

 

int pirPin = 3;   //the digital pin connected to the PIR sensor’s output

int ledPin = 13;

int nPIR_detect;

 

void setup()

{

Ethernet.begin(mac, ip);

Serial.begin(57600);

 

pinMode(pirPin, INPUT);

digitalWrite(pirPin, LOW);

//give the sensor some time to calibrate

Serial.print(“calibrating sensor “);

for(int i = 0; i < calibrationTime; i++){

Serial.print(“.”);

delay(1000);

}

Serial.println(” done”);

Serial.println(“SENSOR ACTIVE”);

delay(50);

nPIR_detect = 0;

}

 

void loop()

{

delay(1000);

if(PIR_detected()) // PIR : HIGH

{

if (client.available()) {

char c = client.read();

Serial.print(c);

}

 

Serial.println(“connecting…”);

 

if (client.connect(server, 25)) {

Serial.println(“connected”);

client.println(“helo smtpout.secureserver.net”);

client.println(“AUTH LOGIN”);                         //Mira en “http://base64-encoder-online.waraxe.us/”

client.println(“***********************”);           //Nombre de usuario *****@gmail.com.my and encode it

client.println(“***********************”); La contraseña…

delay(10);

client.println(“MAIL From: ***@***.com”); // replace the ** with your mail address

delay(10);

client.println(“RCPT TO: +++@+++.com”);       // replace the ** with to mail address

delay(10);

client.println(“DATA”);

delay(10);

client.println(“From: ***@***.com”);

delay(10);

client.println(“TO: +++@+++.com”);

delay(10);

client.println(“SUBJECT: Algo detectado en casa. ALERTA PIR”);

delay(10);

client.println();

client.println(“ESTADO DE ALERTA”);

client.println(“¡¡¡PELIGRO ALGO DETECTADO!!”);

client.println(“.”);

client.println(“.”);

delay(10);

 

delay(1000);

client.stop();

Serial.println(“mail sent!!”);

delay(30000);

}

else

{

Serial.println(“connection failed”);

}

}

}

 

boolean PIR_detected()

{

boolean bPIR;

if(digitalRead(pirPin) == HIGH){

digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state

if(lockLow){

//makes sure we wait for a transition to LOW before any further output is made:

lockLow = false;

Serial.println(“—“);

Serial.print(“motion detected at “);

Serial.print(millis()/1000);

Serial.println(” sec”);

delay(50);

}

takeLowTime = true;

bPIR = true;

}

if(digitalRead(pirPin) == LOW){

digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

if(takeLowTime){

lowIn = millis();         //save the time of the transition from high to LOW

takeLowTime = false;       //make sure this is only done at the start of a LOW phase

}

//if the sensor is low for more than the given pause,

//we assume that no more motion is going to happen

if(!lockLow && millis() – lowIn > pause){

//makes sure this block of code is only executed again after

//a new motion sequence has been detected

lockLow = true;

Serial.print(“motion ended at “);     //output

Serial.print((millis() – pause)/1000);

Serial.println(” sec”);

delay(50);

}

bPIR = false;

}

return bPIR;

}

ARDUINO & PCLAPCOUNTER & SCALEXTRIC

9

Posted by rdm | Posted in ARDUINO, scalextric | Posted on 18-03-2014

Aquí os dejo como voy llevo el código para hacer funcionar el programa que controla la información, una pista de scalextric PCLAPCOUNTER   www.pclapcounter.com  con un Arduino.

En mi caso he usado el arduino mega, 28 leds y un lcd 20×4. Este código no es definitivo, pero ya es funcional.

arduinomegapclapcounter

 

 

 

 

 

CÓDIGO ACTUAL  (19/03/2014)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

//start
// define which pins are to be used for the various light functions
#define RED1 22// 1st red pin   1
#define RED2 23// 2st red pin   2
#define RED3 24// 3st red pin   3
#define RED4 25// 4st red pin   4
#define RED5 26// 5st red pin   5
#define RED6 27// STOP LIGT     6

#define GREEN1 30// GO LIGT     7
#define YELLOW1 31// BANDERA AMARILLA 8

#define POWER1 32// CARRIL 1 POWER
#define POWER2 33// CARRIL 2 POWER

#define FALSESTART1 34// CARRIL 1 FS
#define FALSESTART2 35// CARRIL 2 FS

#define STOPGO1 36// CARRIL 1 STOP&GO
#define STOPGO2 37// CARRIL 2 STOP&GO

#define FIRSTPLACE1 40// CARRIL 1 FIRST PLACE
#define FIRSTPLACE2 41// CARRIL 2 FIRST PLACE

#define OUTFUEL1 42// CARRIL 1 OUT FUEL
#define OUTFUEL2 43// CARRIL 2 OUT FUEL

#define LOWFUEL1 44// CARRIL 1 LOW FUEL
#define LOWFUEL2 45// CARRIL 2 LOW FUEL

#define MAXFUEL1 46// CARRIL 1 MAX FUEL
#define MAXFUEL2 47// CARRIL 2 MAX FUEL

#define PITSTOP1 48// CARRIL 1 PIT STOP
#define PITSTOP2 49// CARRIL 2 PIT STOP

int ledPin = 13;
unsigned long nextBlink;
char ledState = LOW;
const int PWRPin = 2;

String inData;
char recieved;
int charsReceived = 0;

// setup the pins for outputs, blinking and open the serial port
void setup()
{
lcd.init();                      // initialize the lcd
lcd.backlight();
digitalWrite (PWRPin, HIGH);
lcd.setCursor (3,0); lcd.print(“PC LAP COUNTER”);
lcd.setCursor (4,1); lcd.print(“ARDUINO MEGA”);
lcd.setCursor (1,2); lcd.print(“24 LEDS & LCD 20×4”);
lcd.setCursor (0,3); lcd.print(“v.19/03/2014”);
pinMode(GREEN1, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(RED6, OUTPUT);
pinMode(YELLOW1, OUTPUT);
pinMode(POWER1, OUTPUT);
pinMode(POWER2, OUTPUT);
pinMode(FALSESTART1, OUTPUT);
pinMode(FALSESTART2, OUTPUT);
pinMode(STOPGO1, OUTPUT);
pinMode(STOPGO2, OUTPUT);
pinMode(FIRSTPLACE1, OUTPUT);
pinMode(FIRSTPLACE2, OUTPUT);
pinMode(OUTFUEL1, OUTPUT);
pinMode(OUTFUEL2, OUTPUT);
pinMode(LOWFUEL1, OUTPUT);
pinMode(LOWFUEL2, OUTPUT);
pinMode(PITSTOP1, OUTPUT);
pinMode(PITSTOP2, OUTPUT);
pinMode(MAXFUEL1, OUTPUT);
pinMode(MAXFUEL2, OUTPUT);

pinMode(ledPin, OUTPUT);

ledState = LOW;
digitalWrite (GREEN1, HIGH) ;

Serial.begin(9600);

}
// look for data on the serial port and turn on/off the lights
// data is ascii strings

void loop()

{

if ( Serial.available() )
{
String b;
b = Serial.readStringUntil(‘[‘);
{ String s;

s = Serial.readStringUntil(‘]’);

if ( s == “SL010″ ) { digitalWrite(RED1, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL011″ ) { digitalWrite(RED1, HIGH); lcd.setCursor(7,3); lcd.print(”   1 “); }
if ( s == “SL020″ ) { digitalWrite(RED2, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL021″ ) { digitalWrite(RED2, HIGH); lcd.setCursor(7,3); lcd.print(”   2 “); }
if ( s == “SL030″ ) { digitalWrite(RED3, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL031″ ) { digitalWrite(RED3, HIGH); lcd.setCursor(7,3); lcd.print(”   3 “); }
if ( s == “SL040″ ) { digitalWrite(RED4, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL041″ ) { digitalWrite(RED4, HIGH); lcd.setCursor(7,3); lcd.print(”   4 “); }
if ( s == “SL050″ ) { digitalWrite(RED5, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL051″ ) { digitalWrite(RED5, HIGH); lcd.setCursor(7,3); lcd.print(”   5 “); }
if ( s == “SL060″ ) { digitalWrite(GREEN1, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(0,3); lcd.print(”        STOP        “) ; }
if ( s == “SL061″ ) { digitalWrite(GREEN1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,3); lcd.print(”         GO “) ; }

if ( s == “SL070” ) { digitalWrite(RED6, LOW); } //; { digitalWrite(RED2, LOW); } ; { digitalWrite(RED3, LOW); } ; { digitalWrite(RED4, LOW); } ; { digitalWrite(RED5, LOW); } ; { digitalWrite(RED6, LOW); }
if ( s == “SL071” ) { digitalWrite(RED6, HIGH); } //; { digitalWrite(RED2, HIGH); } ; { digitalWrite(RED3, HIGH); } ; { digitalWrite(RED4, HIGH); } ; { digitalWrite(RED5, HIGH); } ; { digitalWrite(RED6, HIGH); }

if ( s == “SL080″ ) { digitalWrite(YELLOW1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);  lcd.print(”                    “) ; } //YELLOW FLAG OFF
if ( s == “SL081″ ) { digitalWrite(YELLOW1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2); lcd.print(”  BANDERA AMARILLA  “) ; } //YELLOW FLAG ON

if ( s == “PW000” ) { digitalWrite(POWER1, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(0,0);  lcd.print(“1 Off     “) ; }
if ( s == “PW001” ) { digitalWrite(POWER1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,0);  lcd.print(“1 On      “) ; }
if ( s == “PW010″ ) { digitalWrite(POWER2, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(10,0); lcd.print(”     Off 2″) ; }
if ( s == “PW011″ ) { digitalWrite(POWER2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,0); lcd.print(”     On  2″) ; }

if ( s == “FS010″ ) { digitalWrite(FALSESTART1, LOW); digitalWrite(PWRPin, HIGH);lcd.setCursor(0,2);   lcd.print(”            “); }
if ( s == “FS011” ) { digitalWrite(FALSESTART1, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(0,2);  lcd.print(“FALSE START “); }
if ( s == “FS020″ ) { digitalWrite(FALSESTART2, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,2); lcd.print(”            “); }
if ( s == “FS021” ) { digitalWrite(FALSESTART2, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(10,2); lcd.print(“FALSE START “); }

if ( s == “SG010″ ) { digitalWrite(STOPGO1, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(0,2);  lcd.print(”          “); }
if ( s == “SG011” ) { digitalWrite(STOPGO1, HIGH);digitalWrite(PWRPin, HIGH);  lcd.setCursor(0,2);  lcd.print(“STOP & GO “); }
if ( s == “SG020″ ) { digitalWrite(STOPGO2, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(10,2); lcd.print(”          “); }
if ( s == “SG021″ ) { digitalWrite(STOPGO2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,2); lcd.print(” STOP & GO”); }

if ( s == “FP010” ) { digitalWrite(FIRSTPLACE1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,3); lcd.print(“SEGUNDO”); }
if ( s == “FP011” ) { digitalWrite(FIRSTPLACE1, HIGH); digitalWrite(PWRPin, LOW); lcd.setCursor(0,3); lcd.print(“PRIMERO”); }
if ( s == “FP020” ) { digitalWrite(FIRSTPLACE2, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(13,3);lcd.print(“SEGUNDO”); }
if ( s == “FP021” ) { digitalWrite(FIRSTPLACE2, HIGH); digitalWrite(PWRPin, LOW); lcd.setCursor(13,3);lcd.print(“PRIMERO”); }

if ( s == “OF010” ) { digitalWrite(OUTFUEL1, LOW); }
if ( s == “OF011″ ) { digitalWrite(OUTFUEL1, HIGH); digitalWrite(MAXFUEL1, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);  lcd.print(”   VACIO  “); }
if ( s == “OF020” ) { digitalWrite(OUTFUEL2, LOW); }
if ( s == “OF021″ ) { digitalWrite(OUTFUEL2, HIGH); digitalWrite(MAXFUEL2, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(”   VACIO  “); }

if ( s == “LF010” ) { digitalWrite(LOWFUEL1, LOW); }
if ( s == “LF011” ) { digitalWrite(LOWFUEL1, HIGH);  digitalWrite(MAXFUEL1, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);lcd.print(“GAS BAJO  “);  }
if ( s == “LF020” ) { digitalWrite(LOWFUEL2, LOW); }
if ( s == “LF021″ ) { digitalWrite(LOWFUEL2, HIGH);  digitalWrite(MAXFUEL2, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(” GAS BAJO “); }

if ( s == “MF011″ ) { digitalWrite(MAXFUEL1, LOW);  digitalWrite(PWRPin, LOW); lcd.setCursor(0,1);   lcd.print(”          “) ; }
if ( s == “MF010” ) { digitalWrite(MAXFUEL1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);  lcd.print(“GAS LLENO “) ; }
if ( s == “MF021″ ) { digitalWrite(MAXFUEL2, LOW);  digitalWrite(PWRPin, LOW); lcd.setCursor(10,1);  lcd.print(”          “) ; }
if ( s == “MF020″ ) { digitalWrite(MAXFUEL2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(” GAS LLENO”) ; }

if ( s == “PT010″ ) { digitalWrite(PITSTOP1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);   lcd.print(”          “); }
if ( s == “PT011″ ) { digitalWrite(PITSTOP1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);  lcd.print(” EN BOXES “); }//digitalWrite (YELLOW1, HIGH) ; digitalWrite(GREEN1, HIGH ) ; }
if ( s == “PT020″ ) { digitalWrite(PITSTOP2, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(10,2); lcd.print(”          “); }
if ( s == “PT021″ ) { digitalWrite(PITSTOP2, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(10,2);  lcd.print(” EN BOXES “); }//digitalWrite (YELLOW2, HIGH) ; digitalWrite(GREEN2, HIGH ) ; }
}
}
inData = “”; // Clear recieved buffer
}
//end

Buscar los puertos I2C en ARDUINO

0

Posted by rdm | Posted in ARDUINO | Posted on 30-11-2013

Etiquetas:

Si al ARDUINO le conectas por I2C un dispositivo, puede que no escuentres el puerto que usa.
Con este programa que grabas en el ARDUINO lo encuentras.
Quizás tengas que ajustar la velocidad de 19200 a lo que necesites.

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

void setup()
{
  Wire.begin();

  Serial.begin(19200);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}