Git Product home page Git Product logo

bean-serial's Introduction

Hi there ๐Ÿ‘‹

  • ๐ŸงŠ Iโ€™m currently running iced.dev

  • ๐Ÿ”ญ Iโ€™m currently working on diyAPIs

  • ๐Ÿฆ€ Iโ€™m currently learning Rust

  • ๐Ÿค– Ask me about nodebots

  • ๐Ÿ“ซ How to reach me: twitter.com/monteslu

  • ๐Ÿ˜„ Pronouns: he/him

  • โšก Fun fact: A human head weighs eight pounds.

  • I'm on mastodon: @[email protected]

bean-serial's People

Contributors

deadprogram avatar jacobrosenthal avatar kennethlimcp avatar monteslu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bean-serial's Issues

Program receiving Serial Data AFTER sending serial data to Bean

So I am trying to use Node.js and the ble-bean library to send and receive serial data to the Bean. I am able to send Serial data just fine but receiving serial data AFTER writing serial data is a problem. I have written a basic Arduino sketch and Node.js program that essentially does an echo. The javascript program sends a value to the Bean and the Bean should send the same data back. Example code is below. The Arduino sketch works just fine on the bean when I use the Virtual Serial Monitor to send data to and receive data from the Bean. However, when using my Node.js program, I cannot receive data AFTER successfully sending data to the Bean. I have tried both on Mac OS X and Linix and get the same result, so I believe the issue may be with the ble-bean and/or the noble-device libraries.

Has anyone had any success reading data AFTER sending data to the Bean using the ble-bean Node.js module?

I believe that the issue may be related to the fact that the ble-bean library does not set the "\n" character as a delimiter/parser of the data from the bean, but I am not sure. With that said, when I use the PacketLogger bluetooth sniffer on Mac OS X, I do not see the notification come from the bean. However, when I use the Serial Monitor in the Arduino IDE, I do see the notification for the serial characteristic.

I have tried playing around with Node-RED and I am able to get send and receive serial data that way but I was hoping to write my own native Node.js code because I want to use some node modules that are not supported by Node-RED (and I was hoping to avoid porting them to Node-RED).

Any help would be greatly appreciated.

ble-bean library: https://github.com/jacobrosenthal/ble-bean
bean-serial library: https://github.com/monteslu/bean-serial/blob/master/index.js

Sample Code

Arduino sketch:

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0) {
int readValue = Serial.parseInt();
Serial.println(readValue);
}
}

Node.js program

/*jslint node: true */
"use strict";

/*

  • This script requests the general BLE characteristics from the bean every second.
  • Requires a sketch on the Arduino to do a Serial.print() just like you were plugged
  • in over a serial cable. Any sketch will do.
    */

var Bean = require('ble-bean');
var SerialPort = require('bean-serial').SerialPort;
var serialPort;
var options = {
logging: true, // default to false
};

var intervalId;
var connectedBean;

Bean.discover(function(bean){
connectedBean = bean;
process.on('SIGINT', exitHandler.bind(this));

Firmata digitalRead fails after a couple of reads

While playing around with digitalRead, it appears that after a couple of successful read operations, the Bean stops sending the digitalRead updates.

Using the following modified version of the example code:

"use strict";

var SerialPort = require('../../').SerialPort;
var firmata = require('firmata');
var Bean = require('ble-bean');

var serialPort, firm;
var pinState = 1;
var PIN_TO_TOGGLE = 13;
var PIN_TO_READ = 12;


var intervalId;
var connectedBean;

Bean.discover(function(bean){
  connectedBean = bean;
  process.on('SIGINT', exitHandler.bind(this));

  bean.on("disconnect", function(){
    process.exit();
  });

  bean.connectAndSetup(function(){

    //set color to the built in LED so we know we're connected
    connectedBean.setColor(new Buffer([255, 255, 0]), function(err){
      console.log('set color', err);
    });

    bean.unGate(function(){
      serialPort = new SerialPort(connectedBean);

      firm = new firmata.Board(serialPort, {skipHandshake: true, samplingInterval:100}, function (err, ok) {
        if (err){
          console.log('could node connect to board----' , err);
        }

        console.log("board loaded", ok);
        togglePin();
        readPin();
      });
    });

  });

});

function togglePin(){
  console.log('toggling', pinState);
  if(pinState){
    pinState = 0;
  }else{
    pinState = 1;
  }
  firm.digitalWrite(PIN_TO_TOGGLE, pinState);

  setTimeout(togglePin, 750);
}

function readPin(){
  firm.pinMode(PIN_TO_READ, firm.MODES.INPUT);
  firm.digitalRead(PIN_TO_READ, function(val) {
    console.log('reading', val);
  });
}

process.stdin.resume();//so the program will not close instantly
var triedToExit = false;

//turns off led before disconnecting
var exitHandler = function exitHandler() {

  var self = this;
  if (connectedBean && !triedToExit) {
    triedToExit = true;
    console.log('Turning off led...');
    clearInterval(intervalId);
    connectedBean.setColor(new Buffer([0x0,0x0,0x0]), function(){});
    //no way to know if succesful but often behind other commands going out, so just wait 2 seconds
    console.log('Disconnecting from Device...');
    setTimeout(connectedBean.disconnect.bind(connectedBean, function(){}), 2000);
  } else {
    process.exit();
  }
};

Yields the following output:

$ sudo node index.js 
set color undefined
board loaded undefined
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
reading 1
reading 0
toggling 0
toggling 1
reading 1
reading 0
toggling 0
toggling 1
toggling 0
reading 1
toggling 1
reading 0
reading 1  <- this is where the reading stops...
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0
toggling 1
toggling 0

Has anyone else noticed this behavior? Note that analogRead does not appear to have this problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.