Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Any sample client side code? #2

Closed
wyx2000 opened this issue Nov 6, 2018 · 15 comments
Closed

Any sample client side code? #2

wyx2000 opened this issue Nov 6, 2018 · 15 comments

Comments

@wyx2000
Copy link

wyx2000 commented Nov 6, 2018

I started with RCSwitchFirmata, could not get it work. I use a client code with firmata lib like below:

var board = new firmata.Board('COM4', function () {
  console.log('ready!')
});
 
  board.on("ready", function() {
    const a = 6;
    const b = 7;
.....

neither ready above fires. But if I try it with the StandardFirmata code, it works fine.

Any idea? Is there special client side code for RCSwitchFirmata?

Thanks

@git-developer
Copy link
Owner

Is there special client side code for RCSwitchFirmata?

I don't know of any. The initialisation code is not part of RCSwitchFirmata but ConfigurableFirmata; so to track down your problem, you could start using ConfigurableFirmata and update to RCSwitchFirmata when communication works.

Testing was done with the perl-firmata client, so whatever needs to be done on initialization should be available there.

@wyx2000
Copy link
Author

wyx2000 commented Nov 7, 2018

The perl-firmata is for connec to firmata. Can we have a simple code to show how to pass message to RCSwitchFirmata?

For example, in the document you describe what need send to firmata

All values described here are 8-bit-based (i.e., after unpacking from 7-bit sysex transfer format). A common pattern of all queries is that they echo the query message as response. This pattern allows for detection of unsupported or wrong messages.

Message format
byte 0: Feature ID (RCOUTPUT_DATAor RCINPUT_DATA)
byte 1: Subcommand
byte 2: pin
following bytes: subcommand specific parameters

Can we have a sample code in any client that send a message to control RCSwitch like the below sketch code does?

mySwitch.enableTransmit(10);
mySwitch.setPulseLength(320);
mySwitch.setProtocol(1);
mySwitch.send(5393, 24);

I searched quite a lot today but still cannot make it work, the message byte sequence, the method to call etc. all mixed, not sure which part went wrong.

@wyx2000
Copy link
Author

wyx2000 commented Nov 7, 2018

Could you point out how do I link to the result from the report in RCInputFirmata?

void RCInputFirmata::report()

@git-developer
Copy link
Owner

Could you point out how do I link to the result from the report in RCInputFirmata?

As explained in #3, received rf signals will be reported automatically to your host whenever they arrive.

@git-developer
Copy link
Owner

Please consider that only a few Firmata client libraries currently support ConfigurableFirmata. It seems to me that you try to use ConfigurableFirmata without such a client, which means you're about to develop such a client by yourself. I cannot give you detailed information on this because I never created a firmata client; I used FHEM instead. FHEM is a perl-based smart home application that integrates perl-firmata. Most of the initialization and configuration code is part of perl-firmata and FHEM. In FHEM, the following configuration represents your code above:

define firmata FRM /dev/ttyUSB0@57600
define rc_sender FRM_RCOUT 10
attr   rc_sender IODev firmata
attr   rc_sender pulseLength 320
attr   rc_sender protocol 1
set rc_sender longCode 5393

@git-developer
Copy link
Owner

Maybe this trace log is helpful for you, it shows all messages between FHEM and the arduino at initialization time:

2018.11.07 20:21:54 3: Opening firmata device /dev/ttyUSB0
2018.11.07 20:21:54 3: Setting firmata serial parameters to 57600,8,N,1
2018.11.07 20:21:54 5: firmata FRM_DoInit
2018.11.07 20:21:54 5: firmata FRM:>ff
2018.11.07 20:21:54 3: firmata device opened
2018.11.07 20:21:57 3: firmata querying Firmata versions
2018.11.07 20:21:57 5: firmata FRM:>f90000
2018.11.07 20:21:57 5: firmata FRM:>f079f7
2018.11.07 20:21:58 5: firmata FRM:<f90206f0790206520043005300770069007400630068004600690072006d006100740061002e0069006e006f00f7e05503e11203e26802e35302e43a02e51d02e60e02e71002e86f01e97001ea7701eb6701ec6601ed6001ee4b01ef5901f90206f0790206520043005300770069007400630068004600690072006d006100740061002e0069006e006f00f7
2018.11.07 20:21:58 3: firmata Firmata Firmware Version: RCSwitchFirmata.ino V_2_06 (using Protocol Version: V_2_06)
2018.11.07 20:21:58 5: firmata FRM:>f069f7
2018.11.07 20:21:58 5: firmata FRM:>f06bf7
2018.11.07 20:21:58 5: firmata FRM:<f06a7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f000102030405060708090a0b0c0d0e0ff7f06c7f7f0001010103080a010b01091c7f0001010103080a010b01091c7f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f0001010103080a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a010b01091c7f000101010a010b01091c7f000101010a010b01091c7f000101010a010b01091c7f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f0001010103080a017f0001010103080a017f0001010103080a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f000101010a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f
2018.11.07 20:21:58 5: firmata FRM:<00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017f00010101020a0a017ff7
2018.11.07 20:21:58 5: firmata FRM:>f07a6807f7
2018.11.07 20:21:58 5: firmata initializing 'receiver'
2018.11.07 20:21:58 5: firmata FRM:>f05d0102f7
2018.11.07 20:21:58 5: firmata initializing 'sender'
2018.11.07 20:21:58 5: firmata FRM:>f05c010af7
2018.11.07 20:21:58 5: firmata FRM:<f071
2018.11.07 20:21:58 5: firmata FRM:<55006e00680061006e0064006c0065006400200073007900
2018.11.07 20:21:58 5: firmata FRM:<730065007800200063006f006d006d0061006e006400f7f07155006e00680061006e0064006c0065006400200073007900730065007800200063006f006d006d0061006e006400f7
2018.11.07 20:21:58 3: received String_data: Unhandled sysex command
2018.11.07 20:21:58 3: received String_data: Unhandled sysex command

@git-developer
Copy link
Owner

git-developer commented Nov 9, 2018

@wyx2000 Did you work it out? If you've been successful, I'd appreciate if you could post some sample code so that other users may participate from your work.

@wyx2000
Copy link
Author

wyx2000 commented Nov 9, 2018

@wyx2000 Did you work it out? If you've been successful, I'd appreciate if you could post some sample code so that other users may participate from your work.

Yes, I can post what I found out, will do that tomorrow

@wyx2000
Copy link
Author

wyx2000 commented Nov 10, 2018

Ok, here is the code I am using now.
I am using firmata.js, the js library for firmata.
To send message to arduino, the important part is the need to encode 8 bits to 7 bits

RC315.rcOutput = function(subcommand, pin, val) {
    var data = [];
    data.push(START_SYSEX);
    data.push(RCOUTPUT_DATA);
    data.push(subcommand);
    data.push(pin);
    if(val){
      if (Array.isArray(val)) {
        for (var i = 0; i < val.length ; i++) {
          data.push(val[i]);   
        }        
      }else{
        data.push(val & 0x7F);
        val = val >> 7 ;
        data.push(val & 0x7F);        
        val = val >> 7 ;
        data.push(val & 0x7F);
      }
    }
    data.push(END_SYSEX);    
    this.board.sp.write(data);
};

RC315.rcControl = function(code, pulseLength) {
    this.rcOutput(RCOUTPUT_DETACH, outPin);   
    this.rcOutput(RCOUTPUT_ATTACH, outPin);
    if(pulseLength)
      this.rcOutput(RCOUTPUT_PULSE_LENGTH, outPin, pulseLength);
  
    var bytes = Encoder7Bit.to7BitArray([0x18, 0x00].concat(longToByteArray(code)));
 
    this.rcOutput(RCOUTPUT_CODE_LONG, outPin,  bytes);
}

To receive message from arduino side,

// set firmata.SYSEX_RESPONSE[RCINPUT_DATA] to receive message
RC315.init=function(){
  this.board = new firmata.Board('COM4');
  firmata.SYSEX_RESPONSE[RCINPUT_DATA] = function(board) {
    console.log('RC315 in command (%s): %s, pin: %s ', new Date(), board.buffer[2], board.buffer[3]);
    if(board.buffer[2]==RCINPUT_MESSAGE){
      var data=Encoder7Bit.from7BitArray(board.buffer.slice(4));
      console.log('value: %s, bitcount: %s, delay: %s, protocal: %s ', 
        (data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3],
        (data[4]<<8)+data[5], (data[6]<<8)+data[7], (data[8]<<8)+data[9]);   
    }
 }
}
// the data bytes for RCINPUT
RC315.rcinput = function(subcommand, pin, val) {
    var data = [];
    data.push(START_SYSEX);
    data.push(RCINPUT_DATA);
    data.push(subcommand);
    data.push(pin);
    data.push(END_SYSEX);
    this.board.sp.write(data);
};
// attach to input pin and message will be forward to firmata.SYSEX_RESPONSE[RCINPUT_DATA]
RC315.rcinput(RCINPUT_ATTACH, inPin)

@git-developer
Copy link
Owner

Thanks for sharing, @wyx2000. Is it OK for you when I copy this into the documentation?

One remark on your markdown syntax: single words may formatted as code using a single backtick (`), code blocks need three backticks (```) before and after or an indentation of at least four spaces.

Please close this issue when you think that your problem is solved.

@wyx2000
Copy link
Author

wyx2000 commented Nov 10, 2018

Sure, you can copy to document

@wyx2000 wyx2000 closed this as completed Nov 10, 2018
@git-developer
Copy link
Owner

I have a question on the following code of rcOutput:

    if (Array.isArray(val)) {
      for (var i = 0; i < val.length ; i++) {
        data.push(val[i]);   
      }        
    }

Is it correct that the array val must come 7-bit encoded from the caller?

@wyx2000
Copy link
Author

wyx2000 commented Nov 10, 2018 via email

@git-developer
Copy link
Owner

git-developer commented Nov 10, 2018

@wyx2000, I've integrated your code into the README. I'd appreciate if you could proofread it. If it's OK, I will merge it from branch develop into master.

Does RCSwitchFirmata work for your project, have you been successful?

@wyx2000
Copy link
Author

wyx2000 commented Nov 10, 2018

Yes, looks good

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants