java - 将数据从arduino发送到spring boot java
问题描述
我在我的项目“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();
}
所以你们能帮帮我吗?
解决方案
我终于得到了解决方案
@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);
//
}
}
}
推荐阅读
- google-cloud-platform - 缺少 OAuth 同意屏幕 URL 栏
- c++ - 如果设置了 processthreadsapi::STARTUPINFO::hStdError,则 fileapi::WriteFile() 不发送输入 (ffmpeg)
- jquery-select2 - Select2 v4.1 - 如果 option:selected 存在,则更改选择框的背景
- php - 在同一模型上加载所有与父子关系的递归关系
- reactjs - 反应错误:找不到模块'core-util-is'
- rest - 在 {} 速记中定义的 Coldfusion CFC 行为怪异
- django - 从models.py和Django下面的这个函数开始获取登录的用户IP
- node.js - 更新 NgFor 表中的行
- shopify - Shopify Grid 产品图片消失
- reactjs - i18next-react 和自定义格式