简体   繁体   English

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

[英]MIT App Inventor 2 App notification triggered by an Arduino code action

I have an App that is, besides other things, supposed to alert when the Fire Alarm that I have on my project is triggered. 我有一个应用程序,除其他外,该应用程序应该在触发我的项目上的火灾警报时发出警报。 The sensor is a thermistor and it activates a blinking led and a buzzer when the data it is getting is >700. 该传感器是一个热敏电阻,当它获取的数据大于700时,它会激活指示灯闪烁和蜂鸣器。 I am using Bluetooth (HC-05) for my connection. 我正在使用蓝牙(HC-05)进行连接。 I already have on the app a temperature sensor that is supposed to work independently and the readings from the Arduino are already being received, so I can´t mix the values (receive text number of bytes already being used). 我的应用程序中已经有一个应该独立工作的温度传感器,并且已经接收到Arduino的读数,因此我无法混合使用这些值(接收已使用的文本字节数)。 I was thinking of, eg when the led is blinking (meaning that the fire alarm is triggered), it sends some kind of info to the app that notifies me through an extension and the internal Notifier, without entering in conflict with the other blocks.` 我想到的是,例如,当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]

For sending the Temperature value and notification message you can use delimiter as like & or some kind of symbol in the BT.println(); 为了发送温度值和通知消息,您可以在BT.println();使用分隔符,例如&或某种符号BT.println(); .

Try something like this 试试这个

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

When you see the output in Bluetooth terminal you can see output like this. 当您在Bluetooth终端中看到输出时,您会看到这样的输出。

temp&notification
temp&notification

Then at the Mobile App capture that output and break it from the & symbol. 然后在移动应用程序中捕获该输出,并将其从&符号中断开。 then you can grab the two variables. 那么您可以抓住这两个变量。

You can refer these tutorials. 您可以参考这些教程。 1 2 3 1 2 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM