• Some of you may be familiar with my love of 433mhz OOK/ASK RF data transmission. Of course, doing the receiving on the Espruino is still tricky. I haven't managed to get it working with the latest version of my protocol.

    But I had long ago decided that I'd probably want to use another chip dedicated solely to processing the RF, so the Espruino could do that high level stuff that it's so good at. Some of you may have seen the AzzyRF boards I've been building - they consist of an ATtiny1634 microcontroller clocked at 8mhz, and an on-board RF transmitter and receiver, based on the Synoxo SYN115 and SYN470 chips respectively (both of these are single-chip, "just add passives" RF chips). The microcontroller on the AzzyRF board talks to the Espruino board over serial.

    The firmware on the board uses a crude parody of the at-commands. Commands are terminated with a CR/LF. If the command output data, a > or # is then send back, and waits for the necessary amount of data, or until 10 seconds have gone by with nothing. Messages received are sent with the data prefixed by a +, binary data from a command with an =. I wound up with two input and two output modes - either hex (for human readability) or raw - the above mentioned prompt is > if in hex mode, # if in raw mode (a recent addition, because I kept forgetting what I had on which board, and getting surprised by the results). I set it for raw input, hex output.

    I thought this might be an interesting example for some methods of interfacing with a serial device using a custom protocol, and reacting to feedback from it.

    Espruino side:

    Code (just the AzzyRF stuff):
    https://github.com/SpenceKonde/AzzyProje­cts/blob/master/AzzyRF_demo.js
    In context:
    https://github.com/SpenceKonde/AzzyProje­cts/blob/master/VoiceController.js

    Module:
    https://github.com/SpenceKonde/AzzyProje­cts/blob/master/433mhz/txrxtoserial22/Az­zyRF.js

    AzzyRF firmware (Arduino sketch):
    https://github.com/SpenceKonde/AzzyProje­cts/blob/master/433mhz/txrxtoserial22/tx­rxtoserial22.ino

  • Looks really cool - are you putting the AzzyRF boards on Tindie at all? I didn't see them when I looked just now.

    I just had a quick peek at the code - I like the idea of waiting for a prompt before sending datastring, it makes it nice and tidy.

    Only thing I'd say is it looks like the Serial.on('data' handler is expecting single characters. It'd work fine normally, but if the Espruino gets busy it starts calling the data handler with multiple characters, so you might find you get "SOMEDATA\n", which would trip it up?

  • Hm, how do I work around this without making it into a total mess?

    On Arduino it's easy, because Serial.read() always returns one character. But here, I could be getting anything...

    Is there anything more graceful than

    
    AzzyRF.prototype.onData = function(data) {
    for (var x=0; x < data.length; x++) {
    	if (data.charAt(x)=="#" && this.datastring) {
    		if (this.datastring) {
    			this.Serial.print(this.datastring);
    			this.datastring="";
    		}
    	} else if (data.charAt(x)==">" && this.datastring) {
    		//TODO
    	} else if (data.charAt(x)=="\n" || data.charAt(x)=="\r"){
    		if (this.inString!="") {
    			this.outputFormat(this.inString);
    			this.inString="";
    		}
    	} else {
    		this.inString+=data.charAt(x);
        if (this.timeout > 0) {
          clearTimeout(this.timeout);
    		}
      }
    }
        this.timeout=setTimeout(function() {this.outputFormat(this.inString);this.i­nString='';this.timeout=0;}.bind(this),1­000);
    };
    
    
  • Well, there's:

    SerialX.on('data', function(s) {
      s.split("").forEach(obj.onData);
    });
    

    But what I tend to do is handle a line at a time, which I think is easier?

    var line = "";
    SerialX.on('data', function(s) {
      line += s;
      var i = line.indexOf("\n");
      while (i>=0) {
        handleLine(line.substr(0,i));
        line = line.substr(i+1);
        i = line.indexOf("\n");
      }
    });
    
  • Handling a line at a time doesn't work, because the prompt (the > or # after a command that it needs data for) is not followed by a linebreak (isn't that how ESP8266's AT interface works?)

  • I don't think so... I think ESP8266 is all line-delimited.

    I guess given the lack of regex it's a bit difficult. You could search for > or # separately I guess

  • ...Regex is missing... yes... but with var aStringsElements = aString.split(">") gets you concise and quickly quite far.

  • If normal AT interfaces put a newline after the >, I guess my firmware should too...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Serial communication between Espruino and Arduino for 433mhz OOK transmit/receive.

Posted by Avatar for DrAzzy @DrAzzy

Actions