Arduino Pro Mini HLK RM-04 SR-501 DS1302 ReedSwitch

总体上具备:

无线通信(RM-04串口透传)

门磁报警(ReedSwitch)

红外报警(SR-501)

时间同步(DS1302)

蜂鸣器(无源蜂鸣器)

闪烁灯(LED Pin13)

 

/*
Buzzer      Arduino
 IO      ->  D2
 */
int BUZZER_PIN = 2;
/*
Reed Switch
 RS          Arduino
 IO      ->  D3
 */
int RS_PIN = 3;
/*
SR501        Arduino
 IO       ->  D4
 */
int SR_PIN = 4;
/*
DS1302        ArduinoMini
 CE/RST    ->  D5
 IO        ->  D6
 SCLK      ->  D7
 */
int CE_PIN  = 5;
int IO_PIN  = 6;
int SCLK_PIN  = 7;
/*
LED
*/
int LED_PIN = 13;

#include <DS1302.h>
#include <RingQueue.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1);

char buf[50];
char day[10];
String timedata = "";
int numdata[7] = {0};
int j=0;
int mark=0;
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
// DS1302 end
int interval_time = 1000;  // ms
String data = "";
Queue<int, 5> RS_Queue; //ReedSwitch
Queue<int, 5> SR_Queue; //HC-SR501
String cms_ip = "";

void print_time()
{
  Time t= rtc.time();
  // if ds1302 didn't plugged in, print "Saturday 2165-25-45 27:165:85"
  // if ds1302 plugged in, but not power, print"0 2000-00-00 00:00:00"
  snprintf(buf, sizeof(buf), "%d %04d-%02d-%02d %02d:%02d:%02d", t.day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
  memset(day, 0, sizeof(day));
  mySerial.println(buf);
}

// buzzer:  1times-normal, 2times-, 3times-error
void buzzer(int count)
{
  for(int i=0; i<count; i++)
  {
    pinMode(BUZZER_PIN, OUTPUT);
    digitalWrite(BUZZER_PIN, HIGH);
    delay(50);
    digitalWrite(BUZZER_PIN, LOW);
    delay(50);
    pinMode(BUZZER_PIN, INPUT);
  }
}

void setup(){
  mySerial.begin(9600);
  pinMode(SR_PIN, INPUT);
  pinMode(RS_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  rtc.writeProtect(false);
  rtc.halt(false);
  buzzer(1);
}

void loop(){
  //digitalWrite(BUZZER_PIN, LOW);
  while(mySerial.available()>0)
  {
    data += (char)mySerial.read();
    delay(2);
  }
  // receiving data, and get header of string, if header=="mark==1",set time
  // eg : settime,2014,11,30,22,09,40,5 as 2014-11-30 22:09:40 Thursday
  if(data.length()>0)
  {
    if(data.substring(0,7) == "settime")  //if header == "settime"
    {
      mySerial.print("InputDate:");
      timedata = data.substring(8, data.length());
      mySerial.println(timedata);
      /*char date[32] = {};
      timedata.toCharArray(date, timedata.length());
      mySerial.print("date:");
      mySerial.println(date);
      char* p;
      p = strtok(date, ",");
      int count = 0;
      while(p!=NULL)
      {
        numdata[count] = atoi(p);
        mySerial.println(numdata[count]);
        count++;
        p = strtok(NULL, ",");
      }*/
      //http://www.geek-workshop.com/forum.php?mod=viewthread&tid=205&highlight=ds1302
      //http://www.cnblogs.com/xiaowuyi/p/3448394.html
      for(int i=0; i<timedata.length(); i++)
      {
        if(timedata[i] == ',' || timedata[i] == 0x10 || timedata[i] == 0x13)
        {
          j++;
        }
        else
        {
          numdata[j] = numdata[j]*10 + (timedata[i]-'0');
          mySerial.print(j);
          mySerial.print(numdata[j]);
          mySerial.print("#");
          mySerial.print(timedata[i]);
          mySerial.println();
        }
      }
      // test
      mySerial.print("Time:");
      for (int i=0; i<7; i++)
      {
        mySerial.print("[");
        mySerial.print(i);
        mySerial.print("]");
        mySerial.print(numdata[i]);
        mySerial.print(" ");
      }
      mySerial.println("");
      // test end
      Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], /*numdata[6]*/(Time::Day)(numdata[6]));
      rtc.time(t);
      mark=0;
      j=0;
      timedata = "";
      for(int i=0; i< 7; i++)
      {
        numdata[i] = 0;
      }
    }
    else if(data.substring(0,5) == "infra")
    {
      //mySerial.print("infra=");
      //mySerial.println(infraredState);
      print_time();
      buzzer(1);
    }
    else if(data.substring(0,2) == "SR")
    {
      int value = SR_Queue.GetAVG();
      mySerial.println(value);
      print_time();
      buzzer(1);
    }
    else if(data.substring(0,2) == "RS")
    {
      int value = RS_Queue.GetAVG();
      mySerial.println(value);
      print_time();
      buzzer(1);
    }
    else if(data.substring(0, 5) == "setip")
    {
      String new_ip = data.substring(6, data.length()-6);
      cms_ip = new_ip;
      mySerial.print("new cms_ip:");
      mySerial.println(cms_ip);
    }
    else if(data.substring(0, 5) == "getip")
    {
      mySerial.print("cms_ip:");
      mySerial.println(cms_ip);
    }
    else
    {}
    data = "";
  }
  {
    int SR = digitalRead(SR_PIN);//HC-SR501
    SR_Queue.Push(SR);
    // if there is an alarm, then send message to cms_ip
  }
  {
    int RS = digitalRead(RS_PIN);//ReedSwitch
    RS_Queue.Push(RS);
    // if there is an alarm, then send message to cms_ip
  }
  digitalWrite(LED_PIN, HIGH);
  delay(200);
  digitalWrite(LED_PIN, LOW);
  delay(200);

  //delay(interval_time);
}

 

 

// RingQueue.h

#ifndef RING_QUEUE_H
#define RING_QUEUE_H

#include <stdio.h>

template<typename T, int Max>
struct QueueStruct
{
  int head;
  int tail;
  int size;
  T data[Max];
};

template<class T, int Max>
class Queue
{
public:
  Queue()
  {
    m_queue.head = 0;
    m_queue.tail = 0;
    m_queue.size = Max;
    for (int i=0; i<Max; i++)
    {
      m_queue.data[i] = 0;
    }
  }
  T Pop()
  {
    if (is_Empty())
    {
      return 0;
    }
    T p = m_queue.data[m_queue.head];
    m_queue.head = (m_queue.head+1) % m_queue.size;
    return p;
  }
  void Push(T item)
  {
    if (is_Full())
    {
      T e = Pop();
    }
    m_queue.data[m_queue.tail] = item;
    m_queue.tail = (m_queue.tail + 1) % m_queue.size;
  }
  T GetAVG()
  {
    T sum = 0;
    for (int i=0; i<m_queue.size; i++)
    {
      sum += m_queue.data[i];
    }
    return sum/m_queue.size;
  }
  bool is_Full()
  {
    return ( ((m_queue.tail+1)%m_queue.size) == m_queue.head );
  }
  bool is_Empty()
  {
    return (m_queue.head == m_queue.tail);
  }

  void print(bool flag)
  {
    for (int i=0; i<m_queue.size; i++)
    {
      if (flag)
        printf(" %f ", m_queue.data[i]);
      else
        printf(" %d ", m_queue.data[i]);
    }
    printf("\n");
  }

  QueueStruct<T, Max> m_queue;
};

#endif //RING_QUEUE_H

添加新评论