【问题标题】:Sending data from arduino to spring boot java将数据从arduino发送到spring boot java
【发布时间】:2020-08-27 11:42:19
【问题描述】:

我在我的项目“smart bin”中使用 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();

 
  }

你们能帮帮我吗?

【问题讨论】:

  • String hc06Url = "btspp://FCA87A00B212:1.."; 有效/可用?目前尚不清楚这...但是:“2020 年 8 月 27 日上午 1:26:46 com.jacobtrashcompany.Controller.TrashCanController getData SEVERE: null java.io.IOException: Failed to connect. [112] Host倒下了"
  • 我的代码正在运行,但它只能从 arduino 获取数据一次,但是当我在 go() 函数中放置一个循环时,它给了我一个 null,

标签: java spring-boot arduino


【解决方案1】:

终于解决了

@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);
          // 
        }
      }
  }

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2020-04-16
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多