简体   繁体   中英

How to send data from raspberry pi 3 to my cordova app?

First of all, I'm not a very experienced coder but I'm learning and practicing! Currently, I'm trying to develop a hybrid app with cordova which is a framework for creating web apps which are able to be installed on ios or android like a native app.

So, I have a private project at home. I want to create an app in order to read the temperature of my bluetooth heater thermostat (comet blue). Thanks to a great tutorial I've found online, I managed to get a connection between my raspberry pi 3 and the bluetooth heater thermostat with gatttool . Now i can read the temperature which is great, but of course I want to display the number on my iphone, in my app to be precise. How would you implement this? How can I send data from my raspberry to my phone? Preferably over wi-fi? If anyone is aware of a good tutorial for this issue, i would be very grateful.

Thanks! Em

You could use plain and simple netcat to send temperature readings over UDP. Save the following on the Raspberry Pi in a file called $HOME/transmit . It runs forever reading the temperature every second with gattool and then transmitting it over UDP to your phone:

#!/bin/bash

# Change these to suit your local setup
PHONE_IP=192.168.0.8
PHONE_PORT=5005

while :; do
   # Read value from thermometer with "gattool" - modify as appropriate
   reading=$(gattool -b BC:6A:29:AE:CC:23 --char-read -a 0x25)
   echo "DEBUG: Read with gattool: $reading"

   # Send to phone with netcat
   nc -w0 -u $PHONE_IP $PHONE_PORT <<< "$reading"

   # Wait a second so we don't flood network
   sleep 1
done

Then you can make the script executable with:

chmod +x $HOME/transmit

And you can run it with:

$HOME/transmit

Then on your phone, you need to read from the corresponding UDP port. I don't know what your environment is like, but you could do that with netcat by saving this on your phone as $HOME/recv :

#!/bin/bash

PORT=5005

while :; do
   received=$(/usr/bin/nc -w 0 -u -l $PORT)
   echo $received
done

Or, if your phone runs Python, this will do the same:

#!/usr/local/bin/python3
import socket

UDP_IP   = "0.0.0.0"
UDP_PORT = 5005

# Create UDP socket and bind to any/all interfaces
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

# Display all messages received
while True:
    data, addr = sock.recvfrom(1024)
    print("Received:",data)

There's a few ways you could do this.

On the Raspberry Pi you can write script that uses gatttool to read the temperature and writes the value to a file like /tmp/temperature. Runs this script cron to run this every x minutes to update the value.

Create a 2nd program that's a web server to serve this data over http. You can use any language to do this. Here's how I'd do it with Node.js .

// serve_temp.js
const fs = require('fs');
const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {

  fs.readFile('/tmp/temperature', (err, data) => {
    if (err) throw err;
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end(data);
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

To test it out, write some test data to /tmp/temperature file.

echo 70 > /tmp/temperature

Run the program

node serve_temp.js

On your phone open Safari and go to http://raspberrypi.local:3000 . You should see "70". (You might need to use the ip address instead of the name. Something like http://192.168.1.123:3000 .) Your Cordova app can make this same http call to get data.

You could combine the two programs into one. With Node.js you could use the noble library to use Bluetooth and get the temperature from the thermostat.

Another option is to write a Cordova app that talks directly to the thermostat using Bluetooth. You can do this using cordova-plugin-ble-central .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM