简体   繁体   中英

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.

The idea is, I wanna dump the value of the temperature into a label sitting on MIT app inventor 2 project via Bluetooth. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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