英文:
Sending data from arduino to spring boot java
问题
以下是您提供的内容的翻译部分:
我在我的“智能垃圾箱”项目中使用了BlueCove库,我想保持代码从Arduino接收数据到我的Java代码,我只能接收1次数据...当我在代码中放入一个循环时,我得到了以下消息:
BlueCove版本2.1.0(在bluez上)
2020年8月27日上午1:26:46,com.jacobtrashcompany.Controller.TrashCanController获取数据
严重错误:null
java.io.IOException:连接失败。[112] 主机已关闭
位于com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnectionImpl(本机方法)
位于com.intel.bluetooth.BluetoothStackBlueZ.connectionRfOpenClientConnection(BluetoothStackBlueZ.java:560)
位于com.intel.bluetooth.BluetoothRFCommClientConnection.<init>(BluetoothRFCommClientConnection.java:37)
位于com.intel.bluetooth.MicroeditionConnector.openImpl(MicroeditionConnector.java:379)
位于com.intel.bluetooth.MicroeditionConnector.open(MicroeditionConnector.java:162)
位于javax.microedition.io.Connector.open(Connector.java:83)
位于com.jacobtrashcompany.Repository.HC06.go(HC06.java:33)
位于com.jacobtrashcompany.Controller.TrashCanController.getData(TrashCanController.java:53)
位于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";
// 将此处替换为您的蓝牙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.<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)
so this is my code
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){}}
this is my arduino code is working good but all the problem in my java code ,
#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();
}
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("/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);
//
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论