197 lines
3.6 KiB
C++
197 lines
3.6 KiB
C++
|
|
|
|
#include <SPI.h>
|
|
#include <Ethernet.h>
|
|
#include <Z_OSC.h>
|
|
|
|
#define debug 1
|
|
#define DHCPon 0
|
|
#define RELE1 A0
|
|
#define RELE2 A1
|
|
#define RELE3 A2
|
|
#define RELE4 A3
|
|
|
|
|
|
byte myMac[] = {
|
|
0x90, 0xA2, 0xDA, 0x0F, 0xCA, 0x50 };
|
|
byte myIp[] = {
|
|
10, 100, 50, 21 };
|
|
//byte myIp[] = {
|
|
// 192, 168, 0, 15 };
|
|
int serverPort = 9000;
|
|
byte destIp[] = {
|
|
10, 100, 50, 2}; // TO SET ADDRESS IP COMPUTER
|
|
//byte destIp[] = {
|
|
// 192, 168, 0, 2}; // TO SET ADDRESS IP COMPUTER
|
|
int destPort = 9997; // TO SET SENDING PORT
|
|
|
|
Z_OSCServer server;
|
|
Z_OSCMessage *rcvMes;
|
|
Z_OSCClient client;
|
|
Z_OSCMessage message;
|
|
|
|
short flagTimer = 0;
|
|
long securityTimer = 0;
|
|
long previousMillis = 0; // will store last time LED was updated
|
|
long interval = 8000; // interval at which to blink (milliseconds)
|
|
|
|
|
|
void setup(){
|
|
|
|
|
|
pinMode(RELE1, OUTPUT);
|
|
pinMode(RELE2, OUTPUT);
|
|
pinMode(RELE3, OUTPUT);
|
|
pinMode(RELE4, OUTPUT);
|
|
|
|
digitalWrite(RELE1,LOW); //disable device
|
|
digitalWrite(RELE2,LOW); //disable device
|
|
digitalWrite(RELE3,LOW); //disable device
|
|
digitalWrite(RELE4,LOW); //disable device
|
|
|
|
Serial.begin(115200);
|
|
|
|
Ethernet.begin(myMac ,myIp);
|
|
#if DHCPon
|
|
Ethernet.begin(myMac); //DHCP
|
|
Serial.print("My IP address: ");
|
|
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
|
// print the value of each byte of the IP address:
|
|
Serial.print(Ethernet.localIP()[thisByte], DEC);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println();
|
|
#endif
|
|
|
|
server.sockOpen(serverPort);
|
|
|
|
Serial.println("Welcome, Security BOX Ready!");
|
|
|
|
}
|
|
|
|
|
|
void loop(){
|
|
|
|
if(server.available()){
|
|
rcvMes=server.getMessage();
|
|
//logMessage();
|
|
cmdProcess();
|
|
}
|
|
|
|
|
|
if(flagTimer)
|
|
{
|
|
if(millis() - securityTimer > interval)
|
|
{
|
|
flagTimer = 0;
|
|
digitalWrite(RELE1, LOW);
|
|
digitalWrite(RELE2, LOW);
|
|
digitalWrite(RELE3, LOW);
|
|
digitalWrite(RELE4, LOW);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void cmdProcess()
|
|
{
|
|
|
|
uint32_t intValue;
|
|
|
|
if( !strcmp( rcvMes->getZ_OSCAddress() , "/cajaSeguridad" ) )
|
|
{
|
|
intValue = rcvMes->getInteger32(0);
|
|
Serial.print("Caja Seguridad: ");
|
|
Serial.println(intValue);
|
|
securityTimer = millis();
|
|
flagTimer = 1;
|
|
|
|
if(intValue)
|
|
{
|
|
digitalWrite(RELE3, LOW);
|
|
digitalWrite(RELE4, LOW);
|
|
delay(500);
|
|
digitalWrite(RELE1, HIGH);
|
|
digitalWrite(RELE2, HIGH);
|
|
|
|
}
|
|
else
|
|
{
|
|
digitalWrite(RELE1, LOW);
|
|
digitalWrite(RELE2, LOW);
|
|
delay(500);
|
|
digitalWrite(RELE3, HIGH);
|
|
digitalWrite(RELE4, HIGH);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void logMessage()
|
|
{
|
|
|
|
uint16_t i;
|
|
byte *ip=rcvMes->getIpAddress();
|
|
|
|
long int intValue;
|
|
float floatValue;
|
|
char *stringValue;
|
|
|
|
Serial.print(ip[0],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[1],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[2],DEC);
|
|
Serial.print(".");
|
|
Serial.print(ip[3],DEC);
|
|
Serial.print(":");
|
|
|
|
Serial.print(rcvMes->getPortNumber());
|
|
Serial.print(" ");
|
|
Serial.print(rcvMes->getZ_OSCAddress());
|
|
Serial.print(" ");
|
|
Serial.print(rcvMes->getTypeTags());
|
|
Serial.print("--");
|
|
|
|
for(i=0 ; i<rcvMes->getArgsNum(); i++){
|
|
|
|
switch( rcvMes->getTypeTag(i) ){
|
|
|
|
case 'i':
|
|
intValue = rcvMes->getInteger32(i);
|
|
|
|
Serial.print(intValue);
|
|
Serial.print(" ");
|
|
break;
|
|
|
|
|
|
case 'f':
|
|
floatValue = rcvMes->getFloat(i);
|
|
|
|
Serial.print(floatValue);
|
|
Serial.print(" ");
|
|
break;
|
|
|
|
|
|
case 's':
|
|
stringValue = rcvMes->getString(i);
|
|
|
|
Serial.print(stringValue);
|
|
Serial.print(" ");
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
Serial.println("");
|
|
}
|
|
|
|
|
|
|
|
|