APP INVENTOR硬件交互学习教程04——蓝牙控制继电器

« 返回首页 Iot 专题

这节主要是设计一个APP,连接蓝牙模块,通过按钮控制继电器的打开和关闭。

一、硬件部分

img

二、板子程序

串口接收字符,并输出控制继电器



// 引脚定义
const int ledPin1 =  5;// the number of the LED pin
const int ledPin2 =  6;
const int ledPin3 =  3;
const int bluePin =  6;// the number of the LED pin
const int greenPin =  5;
const int redPin =  3;
const int beepPin =  15;
const int relayPin =  14;
const int keyPin1 =  2;
const int keyPin2 =  4;
const int keyPin3 =  7;
const int bluetoothPin = 13;

// 变量定义

int inByte=0;   //接收参数

#define TRUE 1
#define FALSE 0
void setup() 
{
  // 配置输出引脚
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(beepPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  // 配置输入引脚
  pinMode(keyPin1, INPUT);
  pinMode(keyPin2, INPUT);
  pinMode(keyPin3, INPUT);
  pinMode(bluetoothPin, INPUT);
  // 配置串口
  Serial.begin(9600);

}
void loop() { 
  if(Serial.available()) {
  inByte = Serial.read();
  
    if(inByte == 'H'){ digitalWrite(relayPin, HIGH);}
    if(inByte == 'L'){ digitalWrite(relayPin, LOW);} 
    
  } 
}

三、app inventor

3.1界面

bbs_img

3.2程序逻辑块

bbs_img

源码下载

demo_04.aia 源码下载

文档反馈