Arduino代码操作触发的MIT App Inventor 2 App通知

时间:2018-06-22 22:12:17

标签: android bluetooth arduino app-inventor

我有一个应用程序,除其他外,该应用程序应该在触发我的项目上的火灾警报时发出警报。该传感器是一个热敏电阻,当它获取的数据大于700时,它会激活指示灯闪烁和蜂鸣器。我正在使用蓝牙(HC-05)进行连接。我的应用程序中已经有一个应该独立工作的温度传感器,并且已经接收到Arduino的读数,因此我无法混合使用这些值(接收已使用的文本字节数)。我在想例如当LED闪烁时(表示已触发火灾警报),它将向应用程序发送某种信息,该信息通过扩展程序和内部通知程序通知我,而不会与其他模块发生冲突。

visual aspect of app`

blocks of app

[code]
#include <SoftwareSerial.h>

SoftwareSerial BT(10, 11); //TX, RX respetively
float temp;
float seno;
int frequencia;
float temptermistor;
String state;// string to store incoming message from bluetooth

void setup() {

BT.begin(9600);// bluetooth serial communication will happen on pin 10 and 11
  Serial.begin(9600); // serial com. to check the data on serial monitor
pinMode(A0, INPUT); //temperatura
pinMode(A1, INPUT); //termistor
pinMode(9, OUTPUT); //led alarme
pinMode(13, OUTPUT); //buzzer
}

void loop() {
temp = analogRead(A0);
delay(200);
temp = analogRead(A0);
temp = temp * 0.48828125;
temptermistor = analogRead(A1);
delay(10);
temptermistor = analogRead(A1);
delay(10);
Serial.println(temp);
BT.println(temp);
delay(250);
if (temptermistor >= 700)
{ 
digitalWrite(9, HIGH);
delay(50);
digitalWrite(9, LOW);
delay(50);
for(int x=0;x<180;x++){ //converte graus para rad. e obtém o valor do seno
seno=(sin(x*3.1416/180)); //gera uma frequência a partir do valor do seno
frequencia = 2000+(int(seno*1000));
tone(13,frequencia);
delay(2);
}
}
else
{
noTone(13);
}
while (BT.available()){  //Check if there is an available byte to read
delay(10); //Delay added to make thing stable 
char c = BT.read(); //Conduct a serial read
state += c; //build the string- either "On" or "off"
}  
if (state.length() > 0) {



if(state == "A") 
{
digitalWrite(2, HIGH);

  } 

else if(state == "a") 
{
digitalWrite(2, LOW);
 }
if(state == "B") 
{
digitalWrite(3, HIGH);

  } 

else if(state == "b") 
{
digitalWrite(3, LOW);
 }
if(state == "C") 
{
digitalWrite(4, HIGH);

  } 

else if(state == "c") 
{
digitalWrite(4, LOW);
 }
if(state == "D") 
{
digitalWrite(5, HIGH);

  } 

else if(state == "d") 
{
digitalWrite(5, LOW);
 }
if(state == "E") 
{
digitalWrite(6, HIGH);

  } 

else if(state == "e") 
{
digitalWrite(6, LOW);
 }
if(state == "F") 
{
digitalWrite(7, HIGH);

  } 

else if(state == "f") 
{
digitalWrite(7, LOW);
 }
if(state == "G") 
{
digitalWrite(8, HIGH);

  } 

else if(state == "g") 
{
digitalWrite(8, LOW);
 }
if(state == "H") 
{
digitalWrite(12, HIGH);

  } 

else if(state == "h") 
{
digitalWrite(12, LOW);
 }


state ="";}
}
[/code]

1 个答案:

答案 0 :(得分:1)

要发送温度值和通知消息,可以使用定界符,例如&BT.println();中的某种符号。

尝试这样的事情

BT.print(temp);
BT.print("&");
BT.println("notification");

当您在Bluetooth终端中看到输出时,您会看到这样的输出。

temp&notification
temp&notification

然后在移动应用中捕获该输出,并将其从&符号中断开。那么您可以抓住这两个变量。

您可以参考这些教程。 1 2 3