繁体   English   中英

将数据从arduino发送到spring boot java

[英]Sending data from arduino to spring boot java

我在我的项目“智能垃圾箱”中使用了 BlueCove 库,我想将代码从 arduino 接收到的数据保留到我的 java 代码中,我只能接收 1 次数据......当我在代码中放置一个循环时,我得到了那条信息

    BlueCove version 2.1.0 on bluez
Aug 27, 2020 1:26:46 AM com.jacobtrashcompany.Controller.TrashCanController getData
SEVERE: null
java.io.IOException: Failed to connect. [112] Host is down
        at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl(Native Method)
        at com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnection(BluetoothStackBlueZ.java:560)
        at com.intel.bluetooth.BluetoothRFCommClientConnection.<init>(BluetoothRFCommClientConnection.java:37)
        at com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:379)
        at com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:162)
        at javax.microedition.io.Connector.open(Connector.java:83)
        at com.jacobtrashcompany.Repository.HC06.go(HC06.java:33)
        at com.jacobtrashcompany.Controller.TrashCanController.getData(TrashCanController.java:53)
        at com.jacobtrashcompany.Controller.TrashCanController.main(TrashCanController.java:88)

所以这是我的代码

 public class HC06 {
boolean scanFinished = false;
int x = 1 ;
String hc06Url =
"btspp://FCA87A00B212:1;authenticate=false;encrypt=false;master=false";


 //Replace this with your bluetooth URL

public String go() throws Exception {

StreamConnection streamConnection = (StreamConnection)
Connector.open(hc06Url);
OutputStream os = streamConnection.openOutputStream();
InputStream is = streamConnection.openInputStream();
//os.write("1".getBytes()); //'1' means ON and '0' means OFF
os.close();
byte[] b = new byte[200];
 //Thread.sleep(1000);
is.read(b);
is.close();
 streamConnection.close();
String A = new String(b);

return new String(b) ;}




public static void main(String[] args) throws Exception {
HC06 hc = new HC06();
try {
    String a = hc.go();
    System.out.println(a);
}catch(Exception e){}}

这是我的 arduino 代码运行良好,但我的 java 代码中的所有问题,

#include <Servo.h> 
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to


dht DHT;


Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo

int pos = 0;  // variable to store the servo position
int receivedData;
int flag =0;
int analog_IN = A0;  // This is our input pin humidity


const unsigned int TRIG_PIN=13;
const unsigned int ECHO_PIN=12;


const unsigned int TRIG_PIN1=9;
const unsigned int ECHO_PIN1=8;


const unsigned int BAUD_RATE=9600;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

   pinMode(TRIG_PIN1, OUTPUT);
  pinMode(ECHO_PIN1, INPUT);
  Serial.begin(BAUD_RATE);

   myservo1.attach(9);   
  myservo2.attach(10); 

  // humidity
 Serial.begin(9600); 
}

void loop() {
  if(Serial.available()>0){
  receivedData = Serial.read();
  flag = 1;
}




  
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  

 const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
 int distance= duration/29/2;

 const unsigned long duration1= pulseIn(ECHO_PIN1, HIGH);
 int distance1= duration1/29/2;




 if (distance < 30 ){

    // sweeps from 0 degrees to 180 degrees
  
  for(pos = 0; pos<=180; pos+=6)
  {
    myservo1.write(180);
    myservo2.write(180);
    delay(200);
  }

 }
  else {
  
  for(pos = 180; pos >= 0; pos -= 3) 
  {
    myservo1.write(0);
    myservo2.write(0);
    delay(15);
  
  
  }

    
  }

  myservo1.detach();
   myservo2.detach();
   delay(200);
   myservo1.attach(9);
   myservo2.attach(10);
    
    
    // humidity
   DHT.read11(dht_apin);
     int Temp = DHT.temperature ;
     int Humidity = DHT.humidity ;
     Serial.begin(9600);

      Serial.print(Temp);
      Serial.print("-");
      Serial.print(Humidity);
      Serial.print("-");
      Serial.println(distance1);
      Serial.println("-");

  Serial.flush();

 
  }

所以你们能帮帮我吗?

我终于得到了解决方案

@PostMapping("/DataTrashCan")
public void getAllinfo() throws Exception{
    final boolean scanFinished = false;
    final int x = 1;
    final String hc06Url = "btspp://FCA871A00B212:1;authenticate=false;encrypt=false;master=false";

    final StreamConnection streamConnection = (StreamConnection) Connector.open(hc06Url);
    final BufferedReader input = new BufferedReader(new InputStreamReader(streamConnection.openInputStream()));
    while (true) {

      String inputLine = input.readLine();
      while (inputLine.length() == 1) {
        inputLine = input.readLine();
      }
        if(inputLine.length()>=4&&inputLine.length()<=13){
          final String[] data = inputLine.split(",", 3);
          TrashCanResponse response = new TrashCanResponse(Integer.parseInt(data[0]), Integer.parseInt(data[1]),Integer.parseInt(data[2]));
          trashCanRepository.save(response);
          // 
        }
      }
  }

暂无
暂无

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

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