将数据从Arduino发送到Spring Boot Java。

huangapple go评论78阅读模式
英文:

Sending data from arduino to spring boot java

问题

以下是您提供的内容的翻译部分:

我在我的智能垃圾箱项目中使用了BlueCove库我想保持代码从Arduino接收数据到我的Java代码我只能接收1次数据...当我在代码中放入一个循环时我得到了以下消息

BlueCove版本2.1.0在bluez上
2020年8月27日上午1:26:46com.jacobtrashcompany.Controller.TrashCanController获取数据
严重错误null
java.io.IOException连接失败[112] 主机已关闭
位于com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl本机方法
位于com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionBluetoothStackBlueZ.java:560
位于com.intel.bluetooth.BluetoothRFCommClientConnection.<init>BluetoothRFCommClientConnection.java:37
位于com.intel.bluetooth.MicroeditionConnector.openImplMicroeditionConnector.java:379
位于com.intel.bluetooth.MicroeditionConnector.openMicroeditionConnector.java:162
位于javax.microedition.io.Connector.openConnector.java:83
位于com.jacobtrashcompany.Repository.HC06.goHC06.java:33
位于com.jacobtrashcompany.Controller.TrashCanController.getDataTrashCanController.java:53
位于com.jacobtrashcompany.Controller.TrashCanController.mainTrashCanController.java:88

这是我的代码

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

    // 将此处替换为您的蓝牙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'表示开,'0'表示关
        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 // 传感器连接的模拟引脚

dht DHT;

Servo myservo1;  // 创建控制舵机的舵机对象
Servo myservo2;  // 创建控制舵机的舵机对象

int pos = 0;  // 存储舵机位置的变量
int receivedData;
int flag = 0;
int analog_IN = A0;  // 这是湿度的输入引脚

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); 

    // 湿度
    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 ){

        // 从0度扫到180度
        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);

    // 湿度
    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();
}

如果您需要进一步帮助,请随时提问。

英文:

I'am using library of BlueCove in my project "smart bin" and i want to keep code received the data from arduino to my java code , i can receive data just for 1 time .. when i put a loop in the code i got that msgs

    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.&lt;init&gt;(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)

so this is my code

 public class HC06 {
boolean scanFinished = false;
int x = 1 ;
String hc06Url =
&quot;btspp://FCA87A00B212:1;authenticate=false;encrypt=false;master=false&quot;;
//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(&quot;1&quot;.getBytes()); //&#39;1&#39; means ON and &#39;0&#39; 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){}}

this is my arduino code is working good but all the problem in my java code ,

#include &lt;Servo.h&gt; 
#include &lt;dht.h&gt;
#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()&gt;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 &lt; 30 ){
// sweeps from 0 degrees to 180 degrees
for(pos = 0; pos&lt;=180; pos+=6)
{
myservo1.write(180);
myservo2.write(180);
delay(200);
}
}
else {
for(pos = 180; pos &gt;= 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(&quot;-&quot;);
Serial.print(Humidity);
Serial.print(&quot;-&quot;);
Serial.println(distance1);
Serial.println(&quot;-&quot;);
Serial.flush();
}

so can you help me guys ?

答案1

得分: 0

我终于找到了解决方案

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

i got solution finally

@PostMapping(&quot;/DataTrashCan&quot;)
public void getAllinfo() throws Exception{
final boolean scanFinished = false;
final int x = 1;
final String hc06Url = &quot;btspp://FCA871A00B212:1;authenticate=false;encrypt=false;master=false&quot;;
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()&gt;=4&amp;&amp;inputLine.length()&lt;=13){
final String[] data = inputLine.split(&quot;,&quot;, 3);
TrashCanResponse response = new TrashCanResponse(Integer.parseInt(data[0]), Integer.parseInt(data[1]),Integer.parseInt(data[2]));
trashCanRepository.save(response);
// 
}
}
}

huangapple
  • 本文由 发表于 2020年8月27日 19:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63615238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定