【问题标题】:Raspberry Pi: send bluetooth readings to local serverRaspberry Pi:将蓝牙读数发送到本地服务器
【发布时间】:2014-09-15 22:58:27
【问题描述】:

我是 Raspberry Pi 开发的新手,我参与了一个项目,在该项目中,我们需要获取从之前与我们的 Pi 配对的不同传感器(例如温度传感器、可穿戴健康传感器等)发送的蓝牙读数,然后发送它们到使用 Pi 作为网关的服务器。

如何访问接收蓝牙读数的端口?从那里开始,我想它就像编写一个脚本一样简单,该脚本获取重要信息,如设备 ID 和测量值,将它们放入格式化消息中并将其发送到服务器,但我再次需要建议。

任何帮助,即使是提供论坛或类似网站的链接,都将不胜感激。

更新:

我现在可以读取 BLE 设备中的每个 handle 并使用 bash script 将数据解析为 JSON 文件。但是,我不知道如何告诉Node 我需要每 5 秒更新一次信息。这是我正在使用的代码:

// required modules
var http = require('http');
var fs = require('fs');
var exec = require('child_process').exec;

// for executing bash/shell scripts
function execute(command, callback){
    exec(command, function(error, stdout, stderr){ callback(stdout); });
};

// executes script that updates JSON file with new readings
module.exports.getJSONFile = function(callback){
    execute("./Scripts/BLEreadingsJSON.sh");
};

// creates HTTP server
http.createServer(function(request, response){
    response.writeHead(200,{
            'Content-Type': 'text/json' });
    setInterval(function(){
            var contents = fs.readFile('Scripts/nodeJS/readings.json', function(err, contents){
                    response.write(contents);
                    response.end();
            });
    }, 5*1000);
}).listen(8080); // listen for connections on this port
console.log('Listening to port 8080...');

执行此操作时出现以下错误:

pi@raspberrypi ~/Scripts $ node nodeJS/sendFileToServer.js 
Listening to port 8080...

http.js:851
    throw new TypeError('first argument must be a string or Buffer');
              ^
TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.write (http.js:851:11)
    at /home/pi/Scripts/nodeJS/sendFileToServer.js:22:13
    at fs.js:207:20
    at Object.oncomplete (fs.js:107:15)

我想这是因为它试图在文件创建之前访问它。我怎样才能使用callback 做到这一点?

【问题讨论】:

  • 这里有一些关于 pi 和蓝牙的好信息instructables.com/id/…
  • @Greycon 这很有趣,但我有一个问题。这条线是否遍历通过蓝牙连接到 Pi 的 每一个 设备? for dev in $(find /sys/devices/virtual/input/ -name input*)。我知道在这种情况下他们只连接一个设备,但如果我们有多个传感器通过蓝牙连接到 Pi,这会起作用吗?
  • 对我来说当然是这样,但我没有任何可以测试的蓝牙设备。您是否可以自己尝试一下?
  • 还没有,我已经订购了一些,但它们还在路上。如果这有帮助,我会在他们到达时通知您
  • @Greycon 我更新了我的问题并添加了一些代码,你能看一下吗?

标签: bluetooth raspberry-pi bluetooth-lowenergy gateway


【解决方案1】:

我让它工作了!对于那些仍然感兴趣的人,我最终使用了socket.io。我有一个脚本可以用新的 BLE 读数更新数据。这个node.js 代码监听这些更新并将文件发送到客户端:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs')

app.get('/', function(req, res){
    var index = "index.html";
    res.sendFile(index, {root: "absolute_path_to_index"});
});

var text;
fs.readFile('absolute_path_to_file', function(err, data){
    text = data;
});

io.on('connection', function(socket){
    console.log("User connected");
    socket.emit('news', text);
    socket.on('disconnect', function(){
        console.log("User disconnected");
    });
    // this line waits for changes in the file and uploads the new data
    fs.watchFile('absolute_path_to_file', function(curr, prev){
        fs.readFile('absolute_path_to_file', function(err, data){
            socket.emit('news', data.toString());
        });
    });
});

http.listen(4321, function(){
    console.log("Listening on port 4321...");
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多