Proyecto alarma casera con Netduino


En este original   proyecto  se trata de proteger una habitación de la bomba de sumidero  potencialmente peligrosa para los niños  asegurándose  de que las estancias quedan las  puertas cerradas y las personas (principalmente niños) no entran  en esta.

Las soluciones «alarma de la puerta» disponibles en el mercado parecen dirigidas a congeladores y simplemente no se adaptan a  necesidades mas complejas del autor   que pensaba mas bien  en una solución  con  Netduino.

La idea  original por tanto era algo que hiciera sonar  una alarma corta cuando se abriera la puerta y luego alertara a intervalos (cada pocos minutos), similar a un detector de humo con una batería que funcionase hasta agotar la batería o  hasta que la puerta se cerrase

esquema_alarma

La unidad controla la puerta a través de un interruptor magnético. Cuando se abre la puerta varias cosas suceden:

  1.  La luz del techo se enciende con un relé que controla una carga de baja  tensión.
  2.  Un servicio web se llama el cual envía un MMS a un teléfono.
  3. Un contador de tiempo que  se activa qeu al concluir  producirá una alerta sonora y hara una  llamada webservice (MMS) si la puerta permanece abierta durante un periodo de tiempo determinado.

Cuando se cierra la puerta a la luz se apaga y el temporizador de la alarma se detiene. El proyecto también incorpora una pantalla LCD de 2 líneas para mensajes, 2 LEDs (1 para la energía, 1 para la indicación de modo de problemas / override) y algunos botones para interactuar con el sistema.

Las funcionalidades de esta alarma de puerta basada en Netduino pues son las siguientes :

  •  Apaga/enciende  la luz : la enciende automáticamente cuando se abre la puerta y la apaga cuando la puerta está cerrada
  •  Alarma del MMS al abrir la puerta (a través de llamadas de servicio web)
  •  Alerta audible y alerta MMS adicional si la puerta se deja abierta más tiempo que  el  valor del temporizador
  •  Alerta que se repiten a intervalos especificados si la puerta sigue dejándose abierta
  • 4 botones :
    •  Botón 1: Manual de luz de encendido / apagado de anulación
    • – Botón 2: Regreso al control de forma automática la luz
    • – Botón 3: mensajes de error Revisión (principalmente para conexiones de red)
    • – Botón 4: Encender / apagar el sensor de la puerta (permite prolongado puerta abierta sin alerta)

Respecto al software para Netduino  se utilizan  cuatro clases :

Una clase para  dar soporte  a la comunicación de red:

public static class Network
{
public static bool NetworkAvail = false;
public static NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];

static NoteClass[] networkUpTune = {
new NoteClass(5,NoteClass.C, NoteClass.ThirtySecond),
new NoteClass(5,NoteClass.Rest, NoteClass.ThirtySecond),
new NoteClass(5,NoteClass.C, NoteClass.ThirtySecond),
new NoteClass(5,NoteClass.Rest, NoteClass.ThirtySecond),
new NoteClass(5,NoteClass.C, NoteClass.ThirtySecond)
};

static NoteClass[] networkDownTune = {
new NoteClass(3,NoteClass.C, NoteClass.ThirtySecond),
new NoteClass(3,NoteClass.Rest, NoteClass.ThirtySecond),
new NoteClass(3,NoteClass.C, NoteClass.ThirtySecond),
new NoteClass(3,NoteClass.Rest, NoteClass.ThirtySecond),
new NoteClass(3,NoteClass.C, NoteClass.ThirtySecond)
};

static NoteClass[] alertTune = {
new NoteClass(5,NoteClass.C, NoteClass.Eighth),
new NoteClass(6,NoteClass.C, NoteClass.Eighth),
new NoteClass(5,NoteClass.C, NoteClass.Eighth),
new NoteClass(6,NoteClass.C, NoteClass.Eighth)
};

public static void InitNetwork()
{
ParallaxLCD.Print(«Network…»);
int count = 0;
//DHCP can take a few seconds to finish so wait (up to 10 seconds) for it to complete
while (true)
{
count++;
ni = NetworkInterface.GetAllNetworkInterfaces()[0];
//only try for 10 seconds
if (ni.IPAddress != «0.0.0.0» || count > 10) break;
Thread.Sleep(1000);
}
if (ni.IPAddress == «0.0.0.0»)
{
ParallaxLCD.Print(«Network…\rError: Failed»);
}
else
{
ParallaxLCD.Print(«Network… IP:\r» + ni.IPAddress);
NetworkAvail = true;
}
//wire in event handler to keep track of the current network up/down state
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}

static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
NetworkAvail = e.IsAvailable;
if (Program.initializing)
return;
if (NetworkAvail)
{
ParallaxLCD.Play(networkUpTune);
}
else
{
ParallaxLCD.Play(networkDownTune);
}
}

public static void SendAlert()
{
if (NetworkAvail)
{
try
{
//HttpWebRequest
HttpWebRequest alertRequest = (HttpWebRequest)WebRequest.Create(Program.AlertWebService);
alertRequest.Method = «GET»;
HttpWebResponse alertResponse = (HttpWebResponse)alertRequest.GetResponse();
if (alertResponse.StatusCode != HttpStatusCode.OK)
{
Program.LastError = «(SendAlert) » + alertResponse.StatusCode.ToString() + «: » + alertResponse.StatusDescription;
ParallaxLCD.Play(alertTune);
}
}
catch (Exception e)
{
Program.LastError = «(SendAlert) » + e.Message;
ParallaxLCD.Play(alertTune);
}
}
else
{
Program.LastError = «(SendAlert) Network unavailable»;
ParallaxLCD.Play(alertTune);
}
}

}
}

La clase  para controlar el display LCD

//Non-visible characters (Decimal ASCII) used by the Parallax LCD as commands
public enum LCD_COMMAND_CHARS
{
PrintCustomChar0 = 0,
PrintCustomChar1 = 1,
PrintCustomChar2 = 2,
PrintCustomChar3 = 3,
PrintCustomChar4 = 4,
PrintCustomChar5 = 5,
PrintCustomChar6 = 6,
PrintCustomChar7 = 7,
Backspace = 8,
Right = 9,
LineFeed = 10,
FormFeed = 12, //clears display and sets cursor to row 0 column 0
CR = 13,
BacklightOn = 17,
BacklightOff = 18,
DisplayOff = 21,
DisplayOn_NoCursor_NoBlink = 22,
DisplayOn_NoCursor_CharBlink = 23,
DisplayOn_Cursor_NoBlink = 24,
DisplayOn_Cursor_CharBlink = 25,
//Sound control
NoteLength_1_64 = 208,
NoteLength_1_32 = 209,
NoteLength_1_16 = 210,
NoteLength_1_8 = 211,
NoteLength_1_4 = 212,
NoteLength_1_2 = 213,
NoteLength_Whole = 214,
NoteScale_3 = 215,
NoteScale_4 = 216,
NoteScale_5 = 217,
NoteScale_6 = 218,
NoteScale_7 = 219,
Note_A = 220,
Note_A_Sharp = 221,
Note_B = 222,
Note_C = 223,
Note_C_Sharp = 224,
Note_D = 225,
Note_D_Sharp = 226,
Note_E = 227,
Note_F = 228,
Note_F_Sharp = 229,
Note_G = 230,
Note_G_Sharp = 231,
Note_Rest = 232,
LoadCustomChar0 = 248,
LoadCustomChar1 = 249,
LoadCustomChar2 = 250,
LoadCustomChar3 = 251,
LoadCustomChar4 = 252,
LoadCustomChar5 = 253,
LoadCustomChar6 = 254,
LoadCustomChar7 = 255
}

//This class assumes the Parallax LCD/speaker combo unit is connected to Netduino Plus Digital I/O PIN 3
//which is reserved for COM2 in the Netduino framework. Also assumes the Parallax LCD has been set to
//9600 baud using it’s onboard dip switches.
public static class ParallaxLCD
{
private static SerialPort LCD_PORT;
private static string COM_PORT = «COM2»; //Netduino Plus Digital I/O PIN 3
private static int COM_BAUD = 9600;

private static bool _LCD_state = false;

public static bool LCD
{
get
{
return _LCD_state;
}
set
{
if (value == _LCD_state)
{
//LCD already in specified state, don’t do anything
return;
}
if (value)
{
LCDOn();
BacklightOn();
}
else
{
BacklightOff();
LCDOff();
}
_LCD_state = value;
}
}
public static void Print(string inputstring)
{
//if the first line is >= 16 characters then remove the line break for a more natural behavior
int cr = inputstring.IndexOf(‘\r’, 0);
if (cr >= 16)
{
inputstring = inputstring.Substring(0, cr) + inputstring.Substring(cr + 1);
}
//this routine assumes all output will be «full screen», meaning no other data is left on the screen
ClearLCD();
byte[] bytes = UTF8Encoding.UTF8.GetBytes(inputstring);
LCD_PORT.Write(bytes, 0, bytes.Length);
}

public static void StartSerial()
{
LCD_PORT = new SerialPort(COM_PORT, COM_BAUD, Parity.None, 8, StopBits.One);
LCD_PORT.Open();
}

public static void BacklightOn()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.BacklightOn }, 0, 1);
}

public static void BacklightOff()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.BacklightOff }, 0, 1);
}

public static void ClearLCD()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.FormFeed }, 0, 1);
}

public static void LCDOff()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.DisplayOff }, 0, 1);
}

public static void LCDOn()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.DisplayOn_NoCursor_NoBlink }, 0, 1);
}

public static void LCDCR()
{
LCD_PORT.Write(new byte[] { (int)LCD_COMMAND_CHARS.CR }, 0, 1);
}

//Short, high-pitched sound to ack as notification that an action was detected (like a button press)
public static void Acknowledge()
{
Play(new NoteClass(7, NoteClass.A, NoteClass.ThirtySecond));
}

//Sound a traditional computer beep
public static void Beep()
{
Play(new NoteClass(5, NoteClass.A, NoteClass.Quarter));
}

public static void Play(NoteClass note)
{
byte[] sequence = new byte[3];
sequence[0] = (byte)note.ScaleChar;
sequence[1] = (byte)note.LengthChar;
sequence[2] = (byte)note.NoteChar;
LCD_PORT.Write(sequence, 0, 3);
}
public static void Play(NoteClass[] notes)
{
int currentScale = 0;
int currentLength = 0;
int count = 0;
if (notes != null && notes.Length > 0)
{
byte[] sequence = new byte[notes.Length * 3];
foreach (NoteClass note in notes)
{
if (note.ScaleChar != currentScale)
{
currentScale = note.ScaleChar;
sequence[count++] = (byte)currentScale;
}
if (note.LengthChar != currentLength)
{
currentLength = note.LengthChar;
sequence[count++] = (byte)currentLength;
}
sequence[count++] = (byte)note.NoteChar;
}
LCD_PORT.Write(sequence, 0, count);
}
}

public static byte[] CharsToBytes(char[] Input)
{
byte[] ReturnValue = new byte[Input.Length];

for (int Counter = 0; Counter < Input.Length; ++Counter)
{
ReturnValue[Counter] = (byte)Input[Counter];
}
return ReturnValue;
}

}
public class NoteClass
{
//Helper constants
public const int A = 0;
public const int ASharp = 1;
public const int B = 2;
public const int C = 3;
public const int CSharp = 4;
public const int D = 5;
public const int DSharp = 6;
public const int E = 7;
public const int F = 8;
public const int FSharp = 9;
public const int G = 10;
public const int GSharp = 11;
public const int Rest = 12;

public const int SixtyQuarter = (int)LCD_COMMAND_CHARS.NoteLength_1_64;
public const int ThirtySecond = (int)LCD_COMMAND_CHARS.NoteLength_1_32;
public const int Sixteenth = (int)LCD_COMMAND_CHARS.NoteLength_1_16;
public const int Eighth = (int)LCD_COMMAND_CHARS.NoteLength_1_8;
public const int Quarter = (int)LCD_COMMAND_CHARS.NoteLength_1_4;
public const int Half = (int)LCD_COMMAND_CHARS.NoteLength_1_2;
public const int Whole = (int)LCD_COMMAND_CHARS.NoteLength_Whole;

private int _scale = 5;
private int _note = A;
private int _length = Quarter;

public int Scale
{
get
{
return _scale;
}
set
{
if (value >= 3 && value <= 7)
{
_scale = value;
}
}
}
public int ScaleChar
{
get
{
return ((int)LCD_COMMAND_CHARS.NoteScale_3 – 3) + _scale;
}
}
public int Note
{
get
{
return _note;
}
set
{
if (value >= A && value <= Rest)
{
_note = value;
}
}
}
public int NoteChar
{
get
{
return (int)LCD_COMMAND_CHARS.Note_A + _note;
}
}
public int Length
{
get
{
return _length;
}
set
{
if (value >= SixtyQuarter && value <= Whole)
{
_length = value;
}
}
}
public int LengthChar
{
get
{
return _length;
}
}

public NoteClass(int scale, int note, int length)
{
Scale = scale;
Note = note;
Length = length;
}
}

Una clase para controlar los pulsadores

public static class Utility
{
public static string PadLeft(string val, int maxlen, string padchar)
{
string newval = val;
while (newval.Length < maxlen)
{
newval = padchar + newval;
}
return newval;
}
public static string PadRight(string val, int maxlen, string padchar)
{
string newval = val;
while (newval.Length < maxlen)
{
newval += padchar;
}
return newval;
}
}

Y por último la clase principal:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace DoorManager
{
public enum LightModes { Auto, ManualOn, ManualOff }

public class Program
{
#region User Settings & Constants
public static Cpu.Pin DoorSensorPin = Pins.GPIO_PIN_D11; //Magnetic switch to monitor door open/close state
public static Cpu.Pin LEDPin = Pins.GPIO_PIN_D13; //Status indicator (blinks if error or override on)
public static Cpu.Pin LightPin = Pins.GPIO_PIN_D12; //Connection to high voltage relay to control room light
public static Cpu.Pin Button1Pin = Pins.GPIO_PIN_D7; //Manually turns light on/off
public static Cpu.Pin Button2Pin = Pins.GPIO_PIN_D6; //Returns light control to automatic
public static Cpu.Pin Button3Pin = Pins.GPIO_PIN_D5; //Display error messages
public static Cpu.Pin Button4Pin = Pins.GPIO_PIN_D4; //Toggles door sensor (when off, no alerts are created)
public const int InitialDoorOpenAlertDelay = 30; //in minutes
public const int OngoingDoorOpenSoundAlertDelay = 1; //in minutes
public const int OngoingDoorOpenMessageAlertDelay = 30; //in minutes
public const int LCDOffDelay = 10; //in seconds
public const string AlertWebService = «http://192.168.1.2/WS/SendAlert.aspx?a=Door+Manager&e=Door+Opened»;
#endregion

#region Fields
public static string LastError = «»;
public static InterruptPort DoorSensor;
public static OutputPort LED;
public static OutputPort Light;
public static InterruptPort Button1;
public static InterruptPort Button2;
public static InterruptPort Button3;
public static InterruptPort Button4;
private static bool IsDoorSensorProcessing = false;
private static bool IsDoorSensorIgnored = false;
public static bool IsDoorOpen = false;
private static bool _LightState = false;
public static bool initializing = true;
public static bool IgnoreButtons = true;
public static bool IgnoreAlerts = false;
public static bool AlertScheduled = false;
public static Timer DoorTimer = new Timer(new TimerCallback(DoorTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static Timer LCDTimer = new Timer(new TimerCallback(LCDTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static Timer LEDTimer = new Timer(new TimerCallback(LEDTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static Timer ButtonTimer = new Timer(new TimerCallback(ButtonTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static Timer SendAlertTimer = new Timer(new TimerCallback(SendAlertTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static Timer DoorCloseTimer = new Timer(new TimerCallback(DoorCloseTimerHandler), null, Timeout.Infinite, Timeout.Infinite);
public static int CurrentMessageAlertDelayTimeout = 0;
public static int CurrentSoundAlertDelayTimeout = 0;
public static int CurrentAlertDelayTime = 0;
public static int CurrentLCDDelayTime = 0;
public static LightModes LightMode = LightModes.Auto;
private static int _LEDBlinkCount = 0;
public static bool _LEDBlink = false;

static NoteClass[] initTune = {
new NoteClass(5,NoteClass.C, NoteClass.Sixteenth),
new NoteClass(5,NoteClass.D, NoteClass.Sixteenth),
new NoteClass(5,NoteClass.E, NoteClass.Sixteenth),
new NoteClass(5,NoteClass.F, NoteClass.Sixteenth),
new NoteClass(5,NoteClass.G, NoteClass.Sixteenth),
new NoteClass(6,NoteClass.A, NoteClass.Sixteenth),
new NoteClass(6,NoteClass.B, NoteClass.Sixteenth),
new NoteClass(6,NoteClass.C, NoteClass.Sixteenth)
};
#endregion

#region Properties
public static bool LEDBlink
{
set
{
if (value)
_LEDBlinkCount++;
else
_LEDBlinkCount–;
if (_LEDBlinkCount > 0 && !_LEDBlink)
{
_LEDBlink = true;
LEDTimer.Change(250, Timeout.Infinite); //start timer
}
else if (_LEDBlinkCount == 0 && _LEDBlink)
{
LEDTimer.Change(Timeout.Infinite, Timeout.Infinite); //cancel timer
_LEDBlink = false;
LED.Write(false);
}
}
}
#endregion

#region Program Start
public static void Main()
{
Initialize();
ShowDoorSensorState();

Thread.Sleep(Timeout.Infinite); //keep running forever
}
#endregion

#region Methods
static void IgnoreButton()
{
IgnoreButtons = true;
ButtonTimer.Change(500, Timeout.Infinite); //restart timer
}

public static bool SetLightState()
{
bool expectedState = IsDoorOpen;
switch (LightMode)
{
case LightModes.ManualOn:
expectedState = true;
break;
case LightModes.ManualOff:
expectedState = false;
break;
}
if (expectedState != _LightState)
{
_LightState = expectedState;
Light.Write(expectedState);
}
return _LightState;
}

public static void SetIgnoreDoorSensor(bool ignore)
{
if (ignore != IsDoorSensorIgnored)
{
IsDoorSensorIgnored = ignore;
if (ignore)
{
LEDBlink = true;
//stop door sensor
DoorTimer.Change(Timeout.Infinite, Timeout.Infinite);
ParallaxLCD.LCD = true;
ParallaxLCD.Print(«Door Sensor\rDisabled»);
SetLCDOffTimer();
}
else
{
LEDBlink = false;
ParallaxLCD.LCD = true;
ParallaxLCD.Print(«Door Sensor\rEnabled»);
Thread.Sleep(2000);
//Check door
ShowDoorSensorState();
}
}
}

static void ShowDoorSensorState()
{
if (IsDoorSensorIgnored)
return;
ParallaxLCD.LCD = true; //ensure LCD is on
ParallaxLCD.Print(«Door » + (IsDoorOpen ? «Open» : «Closed»));
if (IsDoorOpen)
{
//start door timer
CurrentSoundAlertDelayTimeout = InitialDoorOpenAlertDelay * 60 * 1000; //convert minutes to milliseconds
CurrentAlertDelayTime = 0;
DoorTimer.Change(0, Timeout.Infinite);
}
else
{
//clear door timer
DoorTimer.Change(Timeout.Infinite, Timeout.Infinite);
//set LCD timer
SetLCDOffTimer();
}
SetLightState();
}

static void SetLCDOffTimer()
{
CurrentLCDDelayTime = LCDOffDelay * 1000;
LCDTimer.Change(1000, Timeout.Infinite);
}

static void Initialize()
{
ParallaxLCD.StartSerial();
ParallaxLCD.LCD = true;
ParallaxLCD.ClearLCD();
Thread.Sleep(1000);
ParallaxLCD.Print(«Initializing…»);
ParallaxLCD.Beep();
Thread.Sleep(1000);

//set LED
LED = new OutputPort(LEDPin, false);
LEDBlink = true;

//setup door sensor
DoorSensor = new InterruptPort(DoorSensorPin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
DoorSensor.OnInterrupt += new NativeEventHandler(DoorSensor_OnInterrupt);
IsDoorOpen = DoorSensor.Read();

//setup light control
Light = new OutputPort(LightPin, false);
Light.Write(_LightState);

//setup buttons
Button1 = new InterruptPort(Button1Pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
Button1.OnInterrupt += new NativeEventHandler(Button1_OnInterrupt);
Button2 = new InterruptPort(Button2Pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
Button2.OnInterrupt += new NativeEventHandler(Button2_OnInterrupt);
Button3 = new InterruptPort(Button3Pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
Button3.OnInterrupt += new NativeEventHandler(Button3_OnInterrupt);
Button4 = new InterruptPort(Button4Pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
Button4.OnInterrupt += new NativeEventHandler(Button4_OnInterrupt);

//setup network
Network.InitNetwork();

Thread.Sleep(5000);

ParallaxLCD.Print(«Ready»);
ParallaxLCD.Play(initTune);

Thread.Sleep(2000);

LEDBlink = false;

initializing = false;
IgnoreButtons = false;
}
#endregion

#region Timer Handlers
static void DoorCloseTimerHandler(object sender)
{
//this timer turns off sending alerts for a period of time after the door has been closed; this is another «bounce» preventative measure
DoorCloseTimer.Change(Timeout.Infinite, Timeout.Infinite); //cancel timer
IgnoreAlerts = false;
}

static void SendAlertTimerHandler(object sender)
{
//performing this in a timer ensures switch «bounce» doesn’t send multiple alerts
SendAlertTimer.Change(Timeout.Infinite, Timeout.Infinite); //cancel timer
Network.SendAlert();
AlertScheduled = false;
}

static void ButtonTimerHandler(object sender)
{
//this routine is to delay reaction to button presses to eliminate «bounce» and false presses
ButtonTimer.Change(Timeout.Infinite, Timeout.Infinite); //cancel timer
IgnoreButtons = false;
}

static void LEDTimerHandler(object sender)
{
if (_LEDBlink)
{
LED.Write(!LED.Read());
LEDTimer.Change(250, Timeout.Infinite); //restart timer
}
else
{
LED.Write(false);
}
}

static void LCDTimerHandler(object sender)
{
if (!IsDoorOpen || IsDoorSensorIgnored)
{
CurrentLCDDelayTime -= 1000;
if (CurrentLCDDelayTime <= 0)
{
CurrentLCDDelayTime = 0;
ParallaxLCD.LCD = false; //turn off LCD
//clear LCD timer
LCDTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
else
{
LCDTimer.Change(1000, Timeout.Infinite);
}
}
else
{
//clear LCD timer
LCDTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
}

static void DoorTimerHandler(object sender)
{
if (IsDoorOpen && !IsDoorSensorIgnored)
{
if (CurrentSoundAlertDelayTimeout > 0)
{
CurrentSoundAlertDelayTimeout -= CurrentAlertDelayTime;
}
if (CurrentMessageAlertDelayTimeout > 0)
{
CurrentMessageAlertDelayTimeout -= CurrentAlertDelayTime;
}
if (CurrentMessageAlertDelayTimeout <= 0)
{
//alert
SendAlertTimer.Change(5000, Timeout.Infinite); //send alert after 5 seconds
//restart delay
CurrentMessageAlertDelayTimeout = OngoingDoorOpenMessageAlertDelay * 60 * 1000; //convert minutes to milliseconds
}
if (CurrentSoundAlertDelayTimeout > 0)
{
int mins = CurrentSoundAlertDelayTimeout / 60000;
int secs = (CurrentSoundAlertDelayTimeout – (mins * 60000)) / 1000;
ParallaxLCD.Print(«Door Open\rAlert in » + (mins > 1 ? mins.ToString() + » min» : secs.ToString() + » sec»));
if (mins > 1)
{
CurrentAlertDelayTime = 60000;
}
else
{
CurrentAlertDelayTime = 1000;
}
}
else
{
//alert
ParallaxLCD.Play(new NoteClass(7, NoteClass.A, NoteClass.Half));
//restart delay
CurrentSoundAlertDelayTimeout = OngoingDoorOpenSoundAlertDelay * 60 * 1000; //convert minutes to milliseconds
CurrentAlertDelayTime = 0;
}
DoorTimer.Change(CurrentAlertDelayTime, Timeout.Infinite);
}
}
#endregion

#region Event Handlers
static void DoorSensor_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (initializing || IsDoorSensorProcessing)
return;
IsDoorSensorProcessing = true;
//magnetic switch wired to normally open / onboard switch normally high
//therefore when the door is closed (magnetic switch open) the onboard switch (normally high) is on
IsDoorOpen = (data2 == 1);
if (IsDoorOpen && !IgnoreAlerts)
{
AlertScheduled = true;
SendAlertTimer.Change(5000, Timeout.Infinite); //send alert after 5 seconds
}
else if (!IsDoorOpen && !AlertScheduled)
{
IgnoreAlerts = true;
DoorCloseTimer.Change(5000, Timeout.Infinite); //prevent alerts for 5 seconds
}
ShowDoorSensorState();
IsDoorSensorProcessing = false;
}

/// <summary>
/// Button 1 turns the light on/off manually.
/// </summary>
static void Button1_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (initializing)
return;
if (!IgnoreButtons)
{
IgnoreButtons = true;
ParallaxLCD.Acknowledge();
LightModes oldMode = LightMode;
if (_LightState)
LightMode = LightModes.ManualOff;
else
LightMode = LightModes.ManualOn;
SetLightState();
//if we’re moving off auto mode start the led blinking for awareness
if (oldMode == LightModes.Auto)
LEDBlink = true;
IgnoreButton();
}
}

/// <summary>
/// Button 2 sets the light back to automatic (door sensor controlled).
/// </summary>
static void Button2_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (initializing)
return;
if (!IgnoreButtons)
{
IgnoreButtons = true;
if (LightMode != LightModes.Auto)
{
ParallaxLCD.Acknowledge();
LightMode = LightModes.Auto;
SetLightState();
LEDBlink = false;
}
IgnoreButton();
}
}

static void Button3_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (initializing)
return;
if (!IgnoreButtons && (!IsDoorOpen || IsDoorSensorIgnored))
{
IgnoreButtons = true;
ParallaxLCD.Acknowledge();
if (LastError != null && LastError.Length > 0)
{
string msg = LastError;
//show error in byte size pieces
ParallaxLCD.LCD = true;
while (msg.Length > 0)
{
if (msg.Length > 32)
{
ParallaxLCD.Print(msg.Substring(0,32));
msg = msg.Substring(32);
}
else
{
ParallaxLCD.Print(msg);
msg = «»;
}
Thread.Sleep(5000);
}
ParallaxLCD.Acknowledge();
SetLCDOffTimer();
}
else
{
ParallaxLCD.LCD = true;
ParallaxLCD.Print(«No Errors»);
SetLCDOffTimer();
}
IgnoreButton();
}
}

/// <summary>
/// Button 4 disables/re-enables the door sensor.
/// </summary>
static void Button4_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (initializing)
return;
if (!IgnoreButtons)
{
IgnoreButtons = true;
ParallaxLCD.Acknowledge();
SetIgnoreDoorSensor(!IsDoorSensorIgnored);
IgnoreButton();
}
}
#endregion
}
}

Lista de componentes :

  • 2 LED, 4 interruptores momentáneos, tornillos diverso, caja de proyecto
  • Opcional: Kit Protoshield para Ardiono – $ 9.90  http://www.seeedstud…uino-p-318.html
  • Este kit incluye una placa de circuito de prototipos, conectores, resistencias, los LED (no caja montable en proyecto), conmutadores (no caja montable proyecto) y otras piezas misceláneas que puede ayudar a un iniciado a empezar.

Fuente  aqui

Anuncio publicitario

Domótica con Netduino y Kinect


Introducción

Hay un montón de mediocre  tecnología sobre el tema de la domotica  en el mercado siendo ademas  los productos  demasiado caros, así que el autor Dan Thyer  decidió construir su  propio sistema domótico. El autor empezó con el microcontrolador Arduino, pero el código rápidamente se convirtió en difícil de mantener porque la programación en Arduino   no esta orientada a objetos,  no puede  hacer multithreading  asi como tampoco permite depuración real con puntos de interrupción tal como se puede hacer con Netduino .Refactorizó entones  su  código para C # y  NET Micro Framework eligiendo  Netduino plus, http://www.netduino.com/netduinoplus/specs.htm , para el microcontrolador  (la versión Plus incluye  un adaptador de Ethernet para la comunicación de red).

Netduino controlado Pistola de agua

El primer proyecto  era una pistola de agua servo controlada para la piscina. El código  para la Netduino controla los servos para rociar el arma de fuego en diferentes patrones en la piscina. Entonces construyo una interfaz de Windows Phone 7 para apuntar los servos a la posición de la pantalla donde se toca. El  autor solía usar IIS Smooth Streaming para transmitir vídeo en el teléfono así que intento apuntar de forma remota a los niños en la piscina desde cualquier lugar. Así consiguió  resultados mixtos con la pieza de video  pero el autor reconoce  que necesitaría perfeccionar lo reduciendo el tiempo de almacenamiento en búfer para que sea más tiempo real.

NetduinoHomeAutomation/LogicalDan_002.jpg

Jardín

El próximo proyecto era controlar el riego del jardín habilitando los  horarios a través  de código los tiempos para regar el jardín y controlar la duración del riego.

NetduinoHomeAutomation/LogicalDan_004.jpg

Kinect

Muchas personas usan  para a hacer proyectos con el Kinect de Microsoft, que tiene una rica SDK completo con los controladores, APIs y un montón de buen código de ejemplo. El Kinect tiene un montón de sensores, incluyendo una cámara RGB, un sensor de profundidad y un micrófono de múltiples matrices. Con Kinect, usted es el controlador! de modo  que el autor tuvo  la idea de usar el Kinect para el controlador de la pistola de agua en la piscina de modo que puede puede apuntar el arma apuntando a donde usted quiera para disparar. El disparador se controla doblando su otro brazo para que la mano está por encima de la articulación del codo. La conexión del Kinect al  controlador fue  muy simple debido a la rica API Kinect y porque el autor había escrito los niveles de back-end para comunicarse con el microcontrolador Netduino.

NetduinoHomeAutomation / VideoSquirtGun.JPG
Vea este vídeo de la pistola de

Reconocimiento de voz en el Kinect

Otra de las características en el Kinect es el micrófono de múltiples matrices con reconocimiento de voz de modo que se puede jugar un poco con los comandos de voz para controlar la pistola de agua y para abrir el garaje.

Vea este vídeo de Simon Says Kinect

Android Puerta de cochera

Es autor ademas   escribio   una aplicación nativa para Android para llamar a un servicio web REST (WCF) que se comunica con el Netduino para abrir el garaje.

Poniendo todo junto

La imagen siguiente muestra la comunicación entre los componentes

NetduinoHomeAutomation/LogicalDan_005.jpg

La imagen de abajo muestra los dispositivos que controla el Netduino

NetduinoHomeAutomation/LogicalDan_006.jpg

NetduinoHomeAutomation / VideoAll.JPG
Vea este vídeo para ver cómo se organiza todo esto

Comunicación Ethernet

La comunicación Ethernet con el Netduino fue la parte más difícil del proyecto.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Threading;

namespace Netduino.Controller
{
    public delegate void MessageEventHandler(string Message);

    class EthernetCommunication
    {
        #region Private Variables
        private string _hostAddress = null;
        private int _port = 80;
        private string _netduinoStaticIPAddress = null;
        private string _subnetMask = null;
        private string _gatewayAddress = null;
        private Thread _listeningThread;
        private Socket _clientSocket = null;
        private static EthernetCommunication _ethernetCommunication;
        #endregion

        #region Constructors
        //This keeps other classes from creating an instance
        private EthernetCommunication()
        {
        }
        #endregion

        #region Public Properties
        public string HostAddress
        {
            set { _hostAddress = value; }
            get { return _hostAddress; }
        }
        public int Port
        {
            set { _port = value; }
            get { return _port; }
        }
        public string NetduinoStaticIPAddress
        {
            set 
            { 
                _netduinoStaticIPAddress = value;
                SetNetduinoStaticIPConfiguration();
            }
            get { return _netduinoStaticIPAddress; }
        }
        public string SubnetMask
        {
            set
            {
                _subnetMask = value;
                SetNetduinoStaticIPConfiguration();
            }
            get { return _subnetMask; }
        }
        public string GatewayAddress
        {
            set 
            {
                _gatewayAddress = value;
                SetNetduinoStaticIPConfiguration();
            }
            get { return _gatewayAddress; }
        }
        #endregion

        #region Events
        public static event MessageEventHandler EventHandlerMessageReceived;
        #endregion

        #region Public Methods

        private void StartListening()
        {
            _listeningThread = new Thread(new ThreadStart(ReceiveSocketsInListeningThread));
            _listeningThread.Start();
        }

        private void InitializeConfiguration()
        {
            if (_netduinoStaticIPAddress == null)
                throw new Exception("The netduino Static IP Address nust be set!");

            if (_subnetMask == null)
                throw new Exception("The Subnet Mask must be set!");

            if (_gatewayAddress == null)
                throw new Exception("The Gateway address must be set.");

            SetNetduinoStaticIPConfiguration();
            NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

            if (_netduinoStaticIPAddress != networkInterface.IPAddress)
                throw new Exception("Problem setting the static IP.");

            if (_subnetMask != networkInterface.SubnetMask)
                throw new Exception("Problem setting the subnet mask.");

            if (_gatewayAddress != networkInterface.GatewayAddress)
                throw new Exception("Problem setting the gateway address.");
        }
        #endregion

        #region Public Static Methods
        public static EthernetCommunication GetInstance()
        {
            if (_ethernetCommunication == null)
            {
                _ethernetCommunication = new EthernetCommunication();
                _ethernetCommunication.HostAddress = Config.HostAddress;
                _ethernetCommunication.Port = Config.Port;
                _ethernetCommunication.NetduinoStaticIPAddress = Config.NetduinoStaticIPAddress;
                _ethernetCommunication.SubnetMask = Config.SubnetMask;
                _ethernetCommunication.GatewayAddress = Config.GatewayAddress;
                _ethernetCommunication.InitializeConfiguration();
                _ethernetCommunication.StartListening();
            }
            return _ethernetCommunication;
        }

        public static void SendMessage(string message)
        {
            GetInstance().SendEthernetMessage(message);
        }
        #endregion

        #region Private Methods
        private bool IsSocketConnected(Socket socket)
        {
            bool connectionNotClosedResetOrTerminated = !socket.Poll(1000, SelectMode.SelectRead);
            bool socketHasDataAvailableToRead = (socket.Available != 0);
            return (connectionNotClosedResetOrTerminated || socketHasDataAvailableToRead);
        }

        private void ReceiveSocketsInListeningThread()
        {
            string receiveMessage = "";
            bool exitProgram = false;

            using (System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Any, _port));
                socket.Listen(10);

                while (!exitProgram)
                {
                    Debug.Print("Waiting for connection...");
                    _clientSocket = socket.Accept();  //This call is "blocking" and will will wait for a connection, which also means the thread hangs around
                    Debug.Print( "Connection Accepted!");

                    using (_clientSocket)
                    {
                        while (IsSocketConnected(_clientSocket))
                        {
                            int availablebytes = _clientSocket.Available;
                            byte[] buffer = new byte[availablebytes];
                            _clientSocket.Receive(buffer);
                            if (buffer.Length > 0)
                            {
                                receiveMessage = new string(Encoding.UTF8.GetChars(buffer));
                                RaiseMessageReceivedEvent(receiveMessage);
                                if (receiveMessage.ToUpper() == "EXIT")
                                {
                                    exitProgram = true;
                                }
                            }

                        }
                    }
                }
            }
        }

        private void RaiseMessageReceivedEvent(string message)
        {
            // Event will be null if there are no subscribers
            if (EventHandlerMessageReceived != null)
            {
                EventHandlerMessageReceived(message);
            }
        }

        private void SetNetduinoStaticIPConfiguration()
        {
            //Exit if not all of the configuration properties are set
            if (_netduinoStaticIPAddress == null || _subnetMask == null || _gatewayAddress == null)
                return;

            NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

            bool _ipAddressAlreadySet = _netduinoStaticIPAddress == networkInterface.IPAddress;
            bool _subnetMaskAlreadySet = _subnetMask == networkInterface.SubnetMask;
            bool _gatewayAlreadySet = _gatewayAddress == networkInterface.GatewayAddress;

            if (_ipAddressAlreadySet && _subnetMaskAlreadySet && _gatewayAlreadySet)
                return;

            // Set our IP address to a new value
            // This will be saved in the config sector of the netduino and will survive reboots 
            networkInterface.EnableStaticIP(_netduinoStaticIPAddress, _subnetMask, _gatewayAddress);
        }

        private void SendEthernetMessage(string message)
        {
            if (_hostAddress != null && _port > 0)
            {
                using (System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    IPHostEntry entry = Dns.GetHostEntry(_hostAddress);
                    IPAddress address = entry.AddressList[0];
                    IPEndPoint endpoint = new IPEndPoint(address, _port);

                    try
                    {
                        socket.Connect(endpoint);
                        socket.Send(Encoding.UTF8.GetBytes(message));
                        socket.Close();
                        Debug.Print(message);
                    }
                    catch (SocketException se)
                    {
                        Debug.Print("Socket Exception!  Probably no server or bad ip?");
                        Debug.Print(se.StackTrace);
                    }
                }
            }
        }
        #endregion
    }
}

Si desea ver un ejemplo de cómo hablar con una aplicación de escritorio, descargue el código fuente y  estudie  el proyecto Netduino.Desktop.Messenger.

Comunicación Servo

Los servos era divertido para programar. Yo escribí una clase servo que ajuste el ángulo y el Mínimo y Máximo grados. He añadido la propiedad invertida para invertir el ángulo porque  el autor tenia  una versión de interior de la pistola de agua que se monta en el suelo mientras la versión de exterior está montada al revés.

using System;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;

namespace Netduino.Controller
{
    public class Servo : IDisposable
    {
        #region Private Variables
        private PWM _servo;
        private bool _invertAngle = false;
        private int _degreeMin = Config.DegreeMinDefault;
        private int _degreeMax = Config.DegreeMaxDefault;
        private uint _durationMin = Config.DurationMinDefault;
        private uint _durationMax = Config.DurationMaxDefault;
        private uint _angle = Config.HomeDefaultAngle;
        private uint _period = Config.PeriodDefault;
        #endregion

        #region Constructors
        public Servo(Cpu.Pin pin)
        {
            _servo = new PWM(pin);
            _servo.SetDutyCycle(0);
        }
        #endregion

        #region Public Methods
        public void Dispose()
        {
            DisengageServo();
            _servo.Dispose();
        }

        /// <summary> 
        /// Disengage the servo.  
        /// The servo motor will stop trying to maintain an angle 
        ///  
        public void DisengageServo()
        {
            _servo.SetDutyCycle(0);
        }

        public void EngageServo()
        {
            SetPulse();
        }
        #endregion

        #region Private Methods
        private void SetPulse()
        {
            uint angle = _invertAngle ? 180 - _angle: _angle;
            uint duration = (angle) * (_durationMax - _durationMin) / 180 + _durationMin;
            _servo.SetPulse(period: _period, duration: duration);  
        }
        #endregion

        #region Public Properties
        public int Angle
        {
            set
            {
                if (value > _degreeMax)
                    value = _degreeMax;

                if (value < _degreeMin)
                    value = _degreeMin;

                if (value < 0)
                    value = 0;

                _angle = (uint)value;
                SetPulse();
            }
            get
            {
                return (int)_angle;
            }
        }

        public bool InvertAngle
        {
            set {_invertAngle = value;}
            get { return _invertAngle; }
        }

        public int DegreeMin
        {
            set {_degreeMin  = value;}
            get { return _degreeMin; }
        }

        public int DegreeMax
        {
            set { _degreeMax = value; }
            get { return _degreeMax; }
        }

        public uint durationMin
        {
            set { _durationMin = value; }
            get { return _durationMin; }
        }

        public uint durationMax
        {
            set { _durationMax = value; }
            get { return _durationMax; }
        }

        public uint period
        {
            set { _period = value; }
            get { return _period; }
        }
        #endregion
    }
}

Controlar el Jardín

El autor ademas  escribiió  una biblioteca de comandos de tiempo para el NET Micro Framework. Tenga en cuenta que el. NET Micro Framework es muy rico, pero no tiene los genéricos.

using System;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;

namespace Netduino.Controller
{
    public delegate void AlarmCallback();

    class AlarmData
    {
        public int Key { get; set; }
        public ExtendedTimer ExtendedTimer { get; set; }
        public bool RemoveAfterRun { get; set; }
        public AlarmCallback Callback { get; set; }
    }

    class Time
    {
        #region Private Variables
        private static Hashtable _alarmHashtable;
        private static int _key;
        #endregion

        #region Constructors
        //This keeps other classes from creating an instance
        private Time()
        {
        }
        #endregion

        #region Public Static Methods
        public static void SetTime(int year, int month, int day, int hour,int minute, int second, int millisecond )
        {
            DateTime presentTime = new DateTime( year, month, day, hour, minute, second, millisecond);
            Microsoft.SPOT.Hardware.Utility.SetLocalTime(presentTime);
        }

        public static void RunDaily(AlarmCallback alarmCallback, int hour, int minute, int second)
        {
            DateTime alarmTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second, 0);

            //If we already missed today then tomorrow is the first day to run
            if(alarmTime<DateTime.Now)
            {
                alarmTime = alarmTime.AddDays(1);
            }

            TimeSpan dailyTimeSpan = new TimeSpan(24, 0, 0);
            CreateAlarm(alarmCallback, alarmTime, dailyTimeSpan, false);
        }

        public static void RunOnDelay(AlarmCallback alarmCallback, int runInMilliseconds)
        {
            DateTime alarmTime = DateTime.Now.AddMilliseconds(runInMilliseconds);
            CreateAlarm(alarmCallback, alarmTime, TimeSpan.Zero, true);
        }

        public static void RunRepetitively(AlarmCallback alarmCallback, int repeatMilliseconds)
        {
            DateTime alarmTime = DateTime.Now.AddMilliseconds(repeatMilliseconds);
            TimeSpan repeatTimeSpan = new TimeSpan(0, 0, 0, 0, repeatMilliseconds);
            CreateAlarm(alarmCallback, alarmTime, repeatTimeSpan, false);
        }
        #endregion

        #region Private Methods
        private static void CreateAlarm(AlarmCallback alarmCallback, DateTime alarmTime, TimeSpan timeSpan, bool removeAfterRun)
        {
            if (_alarmHashtable == null)
                _alarmHashtable = new Hashtable();

            _key=_key+1;

            AlarmData alarmData = new AlarmData();
            alarmData.Key = _key;
            alarmData.Callback = alarmCallback;
            alarmData.ExtendedTimer = new ExtendedTimer(OnExecuteAlarm, alarmData, alarmTime, timeSpan);
            alarmData.RemoveAfterRun = removeAfterRun;

            _alarmHashtable.Add(_key, alarmData);
        }

        private static void OnExecuteAlarm(object target)
        {
            AlarmData alarmData = (AlarmData)target;

            if (alarmData.RemoveAfterRun)
                _alarmHashtable.Remove(alarmData.Key);

            alarmData.Callback.Invoke();
        }
        #endregion
    }
}

El Programa Principal. NET Micro Framework

Hay un controlador de eventos para la comunicación Ethernet que se ejecuta cuando se recibe un mensaje.

EthernetCommunication.EventHandlerMessageReceived += new MessageEventHandler(OnMessageReceived);
El método OnMessageReceived analiza el mensaje y llama a los métodos para ejecutar los comandos. El siguiente fragmento de código es sólo parcial, pero la fuente completa está disponible para su descarga aqui (necesita previamente darse de alta en en el site de  code project si no lo estaba con anterioridad)
private static void OnMessageReceived(string message)
        {
            string[] parts = message.Split(' ');
            switch(parts[0].ToUpper()) 
            {
                case "M":
                case "MOVE":
                    if (parts.Length != 3)
                    {
                        EthernetCommunication.SendMessage("The move command takes 3 arguments.");
                        break;
                    }
                    int leftRightAngle = int.Parse(parts[1]);
                    int upDownAngle = int.Parse(parts[2]);
                    _squirtGun.MoveToPosition(leftRightAngle, upDownAngle);
                    break;

                case "U":
                case "UP":
                    int upDelta = parts.Length > 1 ? int.Parse(parts[1]) : 1;
                    _squirtGun.UpDownAngle = _squirtGun.UpDownAngle + upDelta;
                    break;
Fuente aqui