JiHuo.Ma

SmartHome系列之Aduino配置与调试

0x00 摘要

利用Arduino Mega 2560操作继电器从而控制灯、插线板。 人体红外感应模块用来控制玄关灯在某时间段回家就自动开灯,当然也可以使用光敏电阻更智能一些。

0x01 概述

懒惰促进发展,智能家居应该会是未来发展的趋势,各大厂家产品有很多,而且小巧、颜值高。但个人认为认知度越高的产品,黑产关注度越多,带来的隐患就越高。比如央视曝光的智能摄像机。2015年JD活动¥129买过一款某小众智能摄像机,用于出差时看看家里情况,用的不亦乐乎。某一天余光扫到在我没操控它的情况下,居然动了一下。断电,封箱。默默的拿出USB摄像头插在GEN8上,反正GEN8也是24小时开机的。所以和各种云产品相比,在不被人主动攻击的情况下,自己搭建的还是安全一些的。

0x02 准备工作

硬件部分

Arduino MEGA 2560:某宝不带电源¥50以内,我用的开关电源。 5V继电器模块:高/低电平触发,¥2.75不包邮。 人体红外感应模块:可设置重复触发和延时时间等,¥3.15不包邮。 杜邦线:公转母40p,¥3不包邮。

软件部分

Arduino IDE:1.0版本用起来习惯一些。[点击下载Arduino IDE Ver1.0](http://downloads.arduino.cc/arduino-1.0-windows.zip) Arduino驱动:非官方版本Arduino也许需要驱动才能与计算机通讯,卖家会提供驱动的。

连接设备

USB线将Arduino Mega 2560和计算机连接(也许需要手动安装驱动),[设备管理器]查看端口号,我这里是COM3(如有多个串行设备,不能确定哪个端口号,通过插拔Arduino来判断)。 跳线帽设置继电器为高电平触发,下面是接线图。Arduino的8针脚进行测试,不要用Arduino 13针脚,貌似是贴片led针脚。每次触发会通断一次。

Arduino 继电器 红外
+5v DC+ VCC
GND DC- GND
8 IN  
2   OUT

0x03 烧写Arduino程序

运行Arduino IDE [Tools] -> [Board] -> [Arduino Mega 2560 or Mega ADK] [Tools] -> [Serial Port] -> [COM3](我这里是COM3)

String val = \"\";
int var;
void setup()
{
  Serial.begin(115200);

  /*
    attachInterrupt(0, blink2, CHANGE);
    attachInterrupt(1, blink3, CHANGE);
    attachInterrupt(2, blink21, CHANGE);//RISING
    attachInterrupt(3, blink20, CHANGE);
    attachInterrupt(4, blink19, CHANGE);//CHANGE
    attachInterrupt(5, blink18, CHANGE);
  */
}

/*
  LOW(低电平,继电器断开)返回值为pin_LOW
  INPUT
  HIGH(高电平,继电器闭合)返回值为pin_HIGH
  input格式:003_HIGH/003_LOW/003_CHECK/003_INPUT
  pin 2/3/18/19/20/21:中断
  pin 4:入户门
  pin 5:客厅灯
  pin 6:卫生间灯
  pin 7:次卧灯
  pin 8:次卧电脑桌
  analog in 区域英文输入未调试成功,可以用数字代替
  A0 = 054(本次只用54 55,作为input触发)
  A1 = 055
  A15 = 069


*/
//attachInterrupt 电平变化运行函数
/*
  void blink2()
  {
    Serial.print(\"002_HIGH
\");
  }
  void blink3()
  {
    Serial.print(\"003_HIGH
\");
  }
  void blink21()
  {
    Serial.print(\"021_HIGH
\");
  }
  void blink20()
  {
    Serial.print(\"020_HIGH
\");
  }
  void blink19()
  {
    Serial.print(\"019_HIGH
\");
  }
  void blink18()
  {
    Serial.print(\"018_HIGH
\");
  }
*/
void to_LOW(int pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  if (digitalRead(pin) == LOW)
  {
    Serial.print(String(pin) + \"_LOW
\");
  }
}
void to_HIGH(int pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
  if (digitalRead(pin) == HIGH)
  {
    Serial.print(String(pin) + \"_HIGH
\");
  }
}
void to_INPUT(int pin)
{
  pinMode(pin, INPUT);
  Serial.print(digitalRead(pin));
  if (digitalRead(pin) == 1)
  {
    Serial.print(String(pin) + \"_INPUT
\");
  }
}

void Check_pin(int pin)
{
  if (digitalRead(pin) == LOW)
  {
    Serial.print(String(pin) + \"_LOW
\");
  }
  else if (digitalRead(pin) == HIGH)
  {
    Serial.print(String(pin) + \"_HIGH
\");
  }
}

void Check_input(int pin)
{
  for (var = pin; var <= 55; var++) {
    if (digitalRead(var) == HIGH)
    {
      Serial.print(String(var) + \"_HIGH
\");
      delay(100);
    }
  }
  /*
    if(digitalRead(pin) == HIGH)
    {
     Serial.print(String(pin) + \"_HIGH
\");
     delay(500);
    }

     for(var = pin;var <= 69;var++){
         pinMode(var,INPUT_PULLUP);
         if(digitalRead(var) == LOW)
    {
     Serial.print(String(var) + \"_LOW
\");
    }
     }
  */
}
/*
  void Test_input(int pin)
    {
        int sensorValue = digitalRead(pin);
        Serial.println(sensorValue, DEC);
    }
*/
void loop()
{
  while (Serial.available() > 0)
  {
    val += char(Serial.read());
    delay(5);
  }
  if (val[3] == '_')
  {
    if (val[4] == 'H' && val[5] == 'I' && val[6] == 'G' && val[7] == 'H')
    {
      to_HIGH((String(val[0]) + String(val[1]) + String(val[2])).toInt());
    }
    else if (val[4] == 'L' && val[5] == 'O' && val[6] == 'W')
    {
      to_LOW((String(val[0]) + String(val[1]) + String(val[2])).toInt());
    }
    else if (val[4] == 'C' && val[5] == 'H' && val[6] == 'E' && val[7] == 'C' && val[8] == 'K')
    {
      Check_pin((String(val[0]) + String(val[1]) + String(val[2])).toInt());
    }
    else if (val[4] == 'I' && val[5] == 'N' && val[6] == 'P' && val[7] == 'U' && val[8] == 'T')
    {
      to_INPUT((String(val[0]) + String(val[1]) + String(val[2])).toInt());
    }
    else
    {
      Serial.print(\"Error:Not define.\");
    }
  }
  val = \"\";
  Check_input(54);
}

粘贴到代码区之后点击[Verify]进行校验,无误后点击[Upload]进行烧写。

0x04 测试

[Tools] -> [Serial Monitor]设置波特率为:[115200 baud] - 键入008_HIGH,然后[Send],继电器动作的同时,Arduino IDE输出:8_HIGH - 键入008_LOW,然后[Send],继电器动作的同时,Arduino IDE输出:8_LOW - 键入008_CHCEK,然后[Send],Arduino IDE输出针脚当时状态。

0xFF 完结撒花

红外联动继电器的坑随后再填,接下来是LEDE和Arduino利用python进行控制的后端和手机网页控制Arduino的前端。凌晨1点了,今天还要上班。

# ARTICLE

评论区 4

回复
游客 会员 2026-02-22 03:48
Hookup Girls Makes use of Free of charge Matters? An Excellent Horizontal Advantage! Free of charge hookup women Hookup apps and sites on the internet is the perfect solution if you're sick and tired of planning to pubs and organizations just to be prevented, or perhaps worse, laughed at. I understand what it's like because I've been there. I was one and distressed back in the time -- I essential a fresh spouse -- nevertheless i kept on seeking because I needed not one other choice. If you're an individual gentleman who wishes to hookup with hot girls without gonna those places where the girls are alone, than the report may just change your lifestyle. It is going to clarify why dating on the internet is the greatest choice if you're a men who is too shy to technique a lovely woman in the bar or club.
游客 会员 2026-02-22 03:48
Hookup Females Employs Cost-free Issues? A Great Horizontal Advantage! Free hookup women hookup apps on the web is the best solution if you're tired with likely to bars and organizations only to be ignored, or even worse, laughed at. I realize what it's like because I've been there. I had been one and needy back into the time -- I needed a new lover -- but I kept on seeking because I had not one other choice. If you're just one gentleman who wants to hookup with attractive girls without planning to those locations the location where the girls are by yourself, then this write-up may just make positive changes to existence. It will clarify why courting on the web is the greatest choice if you're a male who seems to be shy to approach an attractive girl within a bar or club.
游客 会员 2026-02-22 03:48
<a href=https://rescuer.info/stati/9-vliyanie-ulybki-na-polnocennuyu-zhizn.html>что такое улыбка</a>
O
OlkinleM 2026-03-07 00:34
Я просто не можу не розповісти смішною ситуацією, що трапилася зі мене минулого тижня, і це було.... Моя крихітка вимовила бажання, аби я зробила дещо святкове на її ДР. Я, як завжди, безумовно, взялася шукати врятувальну знахідку в інтернеті і отримала несподіванку. Провела цілих півтора години, переходячи із сайту у бік другий сайт! Деякі р