Как получить данные от Arduino bluetooth на ioni c 4 - PullRequest
0 голосов
/ 11 апреля 2020

Я пытаюсь установить связь между Arduino Blutooth H C -05 и приложением ioni c. Это мой код Arduino:

#include <DHT.h>
#define DHTPIN 7 // Output pin to Digital #7 on the Uno
#define DHTTYPE DHT11 //Type of DHT sensor we're using
DHT dht(DHTPIN, DHTTYPE); //initializes the DHT sensor

const int xPin = A0;
const int yPin = A1;
const int zPin = A2;

// initialize minimum and maximum Raw Ranges for each axis
int RawMin = 0;
int RawMax = 1023;

float xRaw;
float yRaw;
float zRaw;
float xVal;
float yVal;
float zVal;
float xAvg;
float yAvg;
float zAvg;

float accel;

float treshhold=20.0;

int steps;

int DHTstate = 0;
int DHTflag = 0;

int flag=0;

float calories;
float distance;

// Take multiple samples to reduce noise
const int sampleSize = 10000;

void setup(){
  analogReference(EXTERNAL);
  dht.begin();
  Serial.begin(9600);
  calibrate();
}

void loop(){

 if(Serial.available()>0){
    DHTstate = Serial.read();
    Serial.write(DHTflag);
    DHTflag=0;
  }
  if (DHTstate = 0){
    Serial.write(flag);
    if(DHTflag == 0){
      DHTflag = 1;
      }
  }
  else if (DHTstate = 1){
    if(DHTflag==0){
     DHTflag = 1;
   }
 }
  //DHT loop...
  // Wait 3 seconds between measurements.
  delay(3000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("%\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print("*C ");
  Serial.print(f);
  Serial.print("*F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print("*C ");
  Serial.print(hif);
  Serial.println("*F");

  xRaw=float(analogRead(xPin));
 // Serial.println(xRaw);
  yRaw=float(analogRead(yPin));
 // Serial.println(yRaw);
  zRaw=float(analogRead(zPin));
 // Serial.println(zRaw);

  accel=sqrt(pow((xRaw-xAvg),2)+pow((yRaw-yAvg),2)+pow((zRaw-zAvg),2));
  Serial.println(accel);

  if(accel>treshhold && flag==0){
    flag=1;
    steps+=1;
  }

  if(accel<treshhold && flag==1){
    flag=0;

  }  

  Serial.println('\n');
  Serial.print("steps=");
  Serial.println(steps);

  calories= steps * 0.035;
  distance= steps * 0.0006;
  Serial.print("Distanza=");
  Serial.println(distance);
  Serial.print("Calorie=");
  Serial.println(calories);

  delay(200);
}

void calibrate(){  
  float xSum=0;
  float ySum=0;
  float zSum=0;

  //  Serial.print("Valore calibrato X:: ");
  for (int i=0;i<sampleSize;i++){
    xVal=float(analogRead(xPin));
    xSum += xVal;
  }
  delay(200);
  xAvg=xSum/sampleSize;

 // Serial.print("Valore calibrato Y:: ");
  for (int i=0;i<sampleSize;i++){
    yVal=float(analogRead(yPin));
    ySum += yVal;
  }
  delay(100);
  yAvg=ySum/sampleSize;

  //  Serial.print("Valore calibrato Z:: ");
  for (int i=0;i<sampleSize;i++){
    zVal=float(analogRead(zPin));
    zSum += zVal;
  }
  delay(100);
  zAvg=zSum/sampleSize;
}

Я использую Ionic4, и я не знаю, как увидеть в моем приложении, использующем Bluetoothserial, тот же результат, который я вижу на последовательном мониторе в IDE. Я знаю, как отправлять данные из ioni c в arduino, но не наоборот. Мне нужно это сделать приложение шагомер на android и IOS.

...