// przypisanie poszczególnych komponentów alarmu do konkretnych pinów #define wylacznikLow 12 #define wylacznik 13 #define syrena 8 #define czerwony 11 #define zielony 10 #define niebieski 9 int inputPin1 = 3; // choose the input pin (for PIR sensor) int inputPin2 = 2; int inputPin3 = 4; int val1 = 0; int val2 = 0; int val3 = 0; // stworzenie zmiennych do przechowywania wartości odczytywanych przez czujnik ruchu oraz informacji o czasie unsigned long czas, czasPrzejscia; // zdefiniowanie czasów oczekiwania zanim alarm się uzbroi oraz czasu odliczania, zanim włączy się syrena po wykryciu ruchu int czasOczekiwania = 20; int czasOdliczania = 10; int incomingBajt[58]; // ustawienie domyślnego trybu pracy alarmu na "0" char tryb = 0; /* Możliwe tryby pracy: 0 - nieuzbrojony 1 - oczekiwanie 2 - uzbrojony 3 - odliczanie 4 - alarm */ byte msg[] = { 0x1E, 0x00, 0x0C, 0xD1, 0x00, 0x07, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x60, 0x00, 0x72, 0xD5 }; unsigned char FBusFrame[200]; // SMS Center number (your mobile phone provider should provide this)+48501200777 unsigned char SMSC[] = {0x07, 0x91, 0x84, 0x05, 0x21, 0x00, 0x77, 0xF7, 0x00, 0x00, 0x00, 0x00}; // Recipient SMS Number. After the 0x81, you insert the phone number with the numbers flipped // E.g to insert 0782898909 you would write it as below, 0x40 becomes 04, 0x21 becomes 12, etc unsigned char RecipientNo[] = {0x0A, 0x81, 0x70, 0x28, 0x98, 0x98, 0x90}; void setup() { pinMode(syrena, OUTPUT); pinMode(czerwony, OUTPUT); pinMode(niebieski, OUTPUT); pinMode(zielony, OUTPUT); pinMode(inputPin1, INPUT); pinMode(inputPin2, INPUT); pinMode(inputPin3, INPUT); digitalWrite(czerwony, LOW); digitalWrite(niebieski, LOW); digitalWrite(zielony, LOW); pinMode(wylacznikLow, OUTPUT); digitalWrite(wylacznikLow, LOW); pinMode(wylacznik, INPUT_PULLUP); Serial.begin(115200); delay(100); } void loop() { czas = millis(); if(digitalRead(wylacznik) == HIGH) { // alarm wyłączony digitalWrite(zielony, HIGH); digitalWrite(czerwony, LOW); digitalWrite(niebieski, LOW); digitalWrite(syrena, LOW); tryb = 0; } else { for (int x = 0; x < 128; x++) { Serial.write("U"); } // alarm włączony if(tryb == 0) {tryb = 1; czasPrzejscia = czas;} if(tryb == 1) { digitalWrite(niebieski, HIGH); digitalWrite(zielony, HIGH); digitalWrite(czerwony, LOW); if(czas >= czasPrzejscia + czasOczekiwania * 1000) {tryb = 2;} } if(tryb == 2) { digitalWrite(czerwony, HIGH); digitalWrite(niebieski, LOW); digitalWrite(zielony, LOW); val1 = digitalRead(inputPin1); val2 = digitalRead(inputPin2); val3 = digitalRead(inputPin3); if (val1 == HIGH) {tryb = 3; czasPrzejscia = czas; initialise(), sms1();}// Edit Vladimir Kozlov if (val2 == HIGH) {tryb = 3; czasPrzejscia = czas; initialise(), sms2();}// Edit Vladimir Kozlov if (val3 == HIGH) {tryb = 3; czasPrzejscia = czas; initialise(), sms3();}// Edit Vladimir Kozlov } if(tryb == 3) { digitalWrite(czerwony, HIGH); digitalWrite(niebieski, HIGH); digitalWrite(zielony, LOW); if(czas >= czasPrzejscia + czasOdliczania * 1000) {tryb = 4;} } if(tryb == 4) { digitalWrite(syrena, HIGH); digitalWrite(czerwony, HIGH); digitalWrite(niebieski, HIGH); digitalWrite(zielony, HIGH); //odbieranie if (Serial.available() >= 1) { for (int i=0; i<58; i++) { incomingBajt[i] = Serial.read(); } int result = incomingBajt[48] + incomingBajt[49] + incomingBajt[50] + incomingBajt[51] + incomingBajt[52]; if (result == 723){ tryb = 1; } } } } } void initialise() { // Initialise the Fbus by sending "U" 128 times for (int x = 0; x < 128; x++) { Serial.write("U"); } // Edit Vladimir Kozlov Serial.println(""); } void sms1() { // Send the SMS message SendSMS("Czujnik_1"); int licz1 = 0; // Keep listening for a reply (we won't send any acknowledgements back) while (licz1 == 0) { while (Serial.available() > 0) { int incomingByte = Serial.read(); Serial.print(incomingByte, HEX); Serial.print(" "); licz1++; } } } void sms2() { // Send the SMS message SendSMS("Czujnik_2"); int licz2 = 0; // Keep listening for a reply (we won't send any acknowledgements back) while (licz2 == 0) { while (Serial.available() > 0) { int incomingByte = Serial.read(); Serial.print(incomingByte, HEX); Serial.print(" "); licz2++; } } } void sms3() { // Send the SMS message SendSMS("Czujnik_3"); int licz3 = 0; // Keep listening for a reply (we won't send any acknowledgements back) while (licz3 == 0) { while (Serial.available() > 0) { int incomingByte = Serial.read(); Serial.print(incomingByte, HEX); Serial.print(" "); licz3++; } } } unsigned char SendSMS(const char *Message) { // Clear buffer unsigned char j = 0; memset(FBusFrame, 0, sizeof(FBusFrame)); unsigned char MsgLen = strlen(Message), FrameSize = 0; unsigned char c, w, n, shift = 0, frameIndex = 0; // Edit Vladimir Kozlov //unsigned char oddCheckSum, evenCheckSum = 0, SeqNo = 0x43; unsigned char oddCheckSum, evenCheckSum = 0, SeqNo = 0x60; unsigned char MsgStartIndex = 48; // Message will always start here // Encode the message into 7 bit characters for (n = 0; n < MsgLen; n++) { c = Message[n] & 0x7f; c >>= shift; w = Message[n+1] & 0x7f; w <<= (7-shift); shift += 1; c = c | w; if (shift == 7) { shift = 0x00; n++; } FBusFrame[frameIndex + MsgStartIndex] = c; frameIndex++; } FBusFrame[frameIndex + MsgStartIndex] = 0x01; FrameSize = frameIndex + 44; // The size of the frame is frameIndex+48 (FrameIndex + 48 + 1 - 5) // Insert the frame values to prepare to send an SMS FBusFrame[0] = 0x1E; FBusFrame[1] = 0x00; FBusFrame[2] = 0x0C; FBusFrame[3] = 0x02; FBusFrame[4] = 0x00; FBusFrame[5] = FrameSize; FBusFrame[6] = 0x00; FBusFrame[7] = 0x01; FBusFrame[8] = 0x00; FBusFrame[9] = 0x01; FBusFrame[10] = 0x02; FBusFrame[11] = 0x00; // Insert the SMS Center number for (j = 0; j < sizeof(SMSC); j++) { FBusFrame[12 + j] = SMSC[j]; } FBusFrame[24] = 0x15; //Message type?? FBusFrame[28] = MsgLen; //Message length (uncompressed) // Insert the Recipient number for (j = 0; j < sizeof(RecipientNo); j++) { FBusFrame[j + 29] = RecipientNo[j]; } FBusFrame[41] = 0xA7; // Validity period // Check if the Framesize is odd or even if (FrameSize & 0x01) { frameIndex = FrameSize + 5; FBusFrame[frameIndex] = SeqNo; frameIndex++; FBusFrame[frameIndex] = 0; // Insert to make the Frame even frameIndex++; } else { frameIndex = FrameSize + 5; FBusFrame[frameIndex] = SeqNo; frameIndex++; } // Calculate the checksum from the start of the frame to the end of the frame for (unsigned char i = 0; i < frameIndex+2; i += 2) { oddCheckSum ^= FBusFrame[i]; evenCheckSum ^= FBusFrame[i+1]; } FBusFrame[frameIndex] = oddCheckSum; FBusFrame[frameIndex+1] = evenCheckSum; // Send the full frame to the phone for (unsigned char j = 0; j < (frameIndex+2); j++) { // Debug to check in hex what we are sending //Serial.print(FBusFrame [j], HEX); //Serial.print(" "); Serial.write(FBusFrame [j]); } }