简体   繁体   English

通过蓝牙将数据从Arduino发送到MIT App Inventor 2

[英]Sending data from Arduino to MIT app Inventor 2 via bluetooth

I have an Arduino Uno microprocessor connected with a temperature sensor, I am able to print the temperature on the Serial Monitor successfully. 我有一个与温度传感器连接的Arduino Uno微处理器,我能够在串行监视器上成功打印温度。

The idea is, I wanna dump the value of the temperature into a label sitting on MIT app inventor 2 project via Bluetooth. 我的想法是,我想通过蓝牙将温度值转存到位于MIT应用程序发明家2项目上的标签中。 Anybody has an idea how to do that? 有人知道该怎么做吗?

What should I add to the following code to be able to send the data via Arduino. 我应该在以下代码中添加些什么,以便能够通过Arduino发送数据。

const int dataPin = 8;
int temperature = -1;
int humidity = -1;


void setup() {
 Serial.begin(115200);

}

int readDHT11(){
  uint8_t recvBuffer[5];
  uint8_t cnt = 7;
  uint8_t idx = 0;
  for(int i = 0; i<5; i++){
    recvBuffer[i] = 0;
  }

  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, LOW);
  delay(18);
  digitalWrite(dataPin, HIGH);

  delayMicroseconds(40);
  pinMode(dataPin, INPUT);
  pulseIn(dataPin, HIGH);

  unsigned int timeout = 10000;
  for(int i = 0; i<40; i++){
      timeout = 10000;
      while(digitalRead(dataPin) == LOW){
          if(timeout == 0) return -1;
          timeout--;
      }
  unsigned long t = micros();

  timeout = 10000;
  while(digitalRead(dataPin) == HIGH){
      if(timeout == 0) return -1;
      timeout--;

  }  

  if ((micros() - t) > 40) recvBuffer[idx] |= (1 << cnt);
  if(cnt ==0){
    cnt = 7;
    idx++;
  }else{
    cnt--;
  }



  }

  humidity = recvBuffer[0];
  temperature = recvBuffer[2];
  uint8_t sum = recvBuffer[0] + recvBuffer[2];
  if(recvBuffer[4] != sum) return -2;
return 0;  

}

void loop() {
  int ret = readDHT11();
  if(ret!=0) Serial.println(ret);
  Serial.print("Humidity: "); Serial.print(humidity); Serial.print(" %\t");

  Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" C\n");

  delay(500);
}

Thanks!! 谢谢!!

Take a look here . 在这里看看。 This tutorial was really helpful for me when I was a beginner. 当我还是初学者时,本教程对我真的很有帮助。 Hope that it will helps you too! 希望对您有帮助!

Good luck. 祝好运。

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

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