Avatar for Thijsmans

Thijsmans

Member since Apr 2018 • Last active Nov 2018
  • 2 conversations
  • 7 comments

Most recent activity

  • in Projects
    Avatar for Thijsmans

    I got past the 8-bits vs 16-bits issue and I was just listening to my first song on the si4703; Phil Collings I believe...

    
        /*
            si4703 begins reading from register upper register of 0x0A and reads 
            to 0x0F, then loops to 0x00. Since register 0x0A comes in first 
            we have to shuffle the array around a bit.
        */
    
        si4703.prototype.readReg = function ( verb ) {
            //this.i2c.writeTo( this.addr, 0 );
            data = this.i2c.readFrom( this.addr, 32 );
    
            for( var i=0; i<data.length; i+=2 )
            {
                bits = data [ i ] << 8;
                bits |= data[ i+1 ];
    
                reg = ( i < 12 ? i+(10-i/2) : (i/2)-6 );
                this.registers[reg] = bits;
            }
    
            if( verb !== false )
                this.printReg();
        };
    
        /*
            Write the current 9 control registers (0x02 to 0x07) to the Si4703. 
            It's a little weird, you don't write an I2C addres, The Si4703 assumes 
            you are writing to 0x02 first, then increments.
        */
        si4703.prototype.writeReg = function () {
            bytes = [];
            for( i=0x02; i<0x08; i++ )
            {
                var high = this.registers[ i ] >> 8;
                var low  = this.registers[ i ] & 0x00FF;
                bytes.push(high, low);
            }
            this.i2c.writeTo( this.addr, bytes );
        };
    
        si4703.prototype.printReg = function () {
            console.log( 'Register    | Dec     | Hex    | Bits');
            fill   = String('.');
    
            for( var i in this.r )
            {
                label = i;
                dec = this.registers[ this.r[i] ];
                hex = dec.toString(16);
                bits = dec.toString(2);
    
                console.log( label + fill.repeat(10-label.length )      + '  | ' +
                             dec   + fill.repeat(6-String(dec).length)  + '  | ' +
                             hex   + fill.repeat(5-hex.length)          + '  | ' + 
                             fill.repeat(18-bits.length) + bits
                    );
            }
            console.log(" ");
        };
    

    This is working just fine:

    Register    | Dec     | Hex    | Bits
    DEVIDEID..  | 4674..  | 1242.  | .....1001001000010
    CHIPID....  | 2640..  | a50..  | ......101001010000
    POWERCFG..  | 50433.  | c501.  | ..1100010100000001
    CHANNEL...  | 0.....  | 0....  | .................0
    SYSCONFIG1  | 6144..  | 1800.  | .....1100000000000
    SYSCONFIG2  | 21....  | 15...  | .............10101
    SYSCONFIG3  | 0.....  | 0....  | .................0
    TEST1.....  | 48132.  | bc04.  | ..1011110000000100
    TEST2.....  | 6.....  | 6....  | ...............110
    BOOTCONFIG  | 1.....  | 1....  | .................1
    STATUSRSSI  | 28693.  | 7015.  | ...111000000010101
    READCHAN..  | 205...  | cd...  | ..........11001101
    RDSA......  | 255...  | ff...  | ..........11111111
    RDSB......  | 0.....  | 0....  | .................0
    RDSC......  | 0.....  | 0....  | .................0
    RDSD......  | 0.....  | 0....  | .................0
    

    So now onwards to an Espruino-port of the Arduino sketch!

  • in Projects
    Avatar for Thijsmans

    @allObjects: Perhaps I should have mentioned that I left the volume control out for the moment, since there is no sound to control yet :)

    @Gordon: you make some excellent points on 8 vs 16-bits. I'll have a look at DataView, and some YouTube tutorials on bitwise operators ;-)

  • in Projects
    Avatar for Thijsmans

    Okay, I've been breaking my head over this. There is a datasheet and an extensive programming guide available, but both assume you are able to interface with the si4703 :/

    I'm using the Arduino-sketch as a guide line:

        function si4703 ( option ) {
            this.addr  = 0x10;
            this.reset_pin = option.res;
            this.data_pin  = option.dat;
            this.clock_pin = option.clk;
            this.sen_pin   = option.sen;
            this.registers = new Uint16Array(16);
        }
    
        si4703.prototype.init = function () {
            pinMode(this.reset_pin, 'output');
            digitalWrite( this.reset_pin, false );
    
            pinMode(this.data_pin, 'output');
            digitalWrite( this.data_pin, false );
    
            digitalWrite( this.reset_pin, true );
    
            this.i2c = I2C1;
            this.i2c.setup( { scl: this.clock_pin, sda: this.data_pin } );
    
            this.readReg();
    
            this.registers[ 0x02 ] = 0x4001; // enable IC
            this.registers[ 0x07 ] = 0x8100; // enable oscillator
    
            this.writeReg();
    
            this.readReg();
        };
    
        si4703.prototype.readReg = function () {
            this.i2c.writeTo( this.addr, 0 );
            data = this.i2c.readFrom( this.addr, 32 );
    
            for( var x=0x0A; ; x++ )
            {
                if( x == 0x10 ) x=0;
                this.registers[x] = data[x] << 8;
                this.registers[x] |= data[x];
                if( x == 0x09 ) break;
            }
    
            console.log( this.registers );
        };
    
        si4703.prototype.writeReg = function () {
            for( var i in this.registers )
            {
                if( i > 0x02 )
                    this.i2c.writeTo( this.addr, i, this.registers[i] );
            }
        };
    

    So,the init-method should read the registers, update a few things (mainly the ENABLE-bit), and read the registers again. However, there is no change in the actual registers (first line is the output at startup, second line is after attempting to write the new values):

    new Uint16Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4626, 16962, 2056, 0])
    new Uint16Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4626, 16962, 2056, 0])
    

    What am I missing?

  • in Projects
    Avatar for Thijsmans

    Yes I'm aware of that. I thought the datasheet stated an input rise and fall time of 10ms. That's why I chose the digitalPulse anyway - creating a cueue would be too much of a hassle. However, reading the datasheet again, I see it says 10 ns :) I guess a double-digitalWrite should be fine.

    I guess the first step for interfacing with the si4703 is getting the registers out one by one, using the subaddress-examples found here. I'll let you know when I get lost...

  • in Projects
    Avatar for Thijsmans

    Our kitchen radio recently died, so I bought a si4703 i2c FM receiver. In about two years time, we'll be able to get some tunes in our kitchen again ;) I don't know a thing or two about bitwise JS, so reading the registers from the si4703 and overwriting them will be a pain... Reading tips are much appreciated!

    However, the good news is that I got the rotary encoder and digital potentiometers for tuning and volume working! Both needed a new module: the existing Encoder-module doesn't cover the push-button my encoders have built into the shaft, and there was no existing module for my potmeters (X9C103).

    I'm leaving them here in case anyone finds it useful. Also, remarks about coding is welcome, since I'm not a pro coder. Full modules are attached, here is a usage example:

        var pot = new X9C103( { cs:PIN_CS, ud: PIN_UD, inc: PIN_INC } );
    
        var volume = new Rotary( PIN_A, PIN_B, PIN_SW );
        volume.rotateCallback = function () {
            if( this.position < 0 )
                this.position = 0;
    
            if( this.position > 100 )
                this.position = 100;
            
            pot.jumpTo( this.position );
        };
    
  • in ESP8266
    Avatar for Thijsmans

    Turns out the problem was not with the ESP01 or with Espruino, but with my malformed request :) The debugging methods from this thread made that clear (it showed a HTTP-400 header coming back).

    For whoever finds it useful, this is how Espruino connects to Home Assistant:

      
      var wifi   = require("Wifi");
      var entity = 'smoke_kitchen';
    
      wifi.connect("SSID", {password:"PASS"}, function (e) { 
        if( ! e )
        {
          wifi.setHostname("SmokeAlarm-"+entity+"_­"+getSerial() );
          
          var http = require("http");
          var data = JSON.stringify( {state:'on'} );
          
          var options = {
            host: 'HASS-HOST',
            port: 8123,
            path: '/api/states/input_boolean.' + entity,
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'Content-Length': data.length,
              'x-ha-access': 'Token if needed',
            },
          };
          
          var req = http.request( options, function (res) {
            console.log('Status: ' + res.statusCode);
            console.log( res );
          });
          
          req.write( data );
          req.end();
          
        } else
        { 
          console.log("Error connecting to wifi: ", e );
        }
      });
    
  • in ESP8266
    Avatar for Thijsmans

    I am trying to hook up an ESP01 to my smoke alarm like in this video

    and making it report to home-assistant, running on my pi. Home-assistant has an API which should make it possible for the ESP01 to alter a variable (input_boolean) and trigger other alarms.

    However, the http-request only seems to be able to reach the pi on port 80. Which is a problem, since home-assistant listens to port 8123 (in my case, pihole listens to port 80).

      var wifi = require("Wifi");
    
      wifi.connect("SSID", {password:"PASS"}, function(e) { 
        if( ! e )
        {
          wifi.setHostname("SmokeAlarm-"+getSerial­() );
          wifi.stopAP();
          
          var options = {
            host: 'xd-1',
            port: 80,
            path: '/api/',
            method: 'GET'
          };
          
          require("http").request( options, function (res) {
            res.on('data', function (data) { console.log(data); });
          }).end();
        }
      });
    

    The path "/api/" does not exist on port 80 (pihole only uses /admin/*) and the ESP01 outputs the right error page, served by pihole. The script functions as it should.

    The path "/api/" DOES exist on port 8123 - I can see it in any browser on any device in my network. When changing the port from 80 to 8123, the ESP01 shows nothing, except "=undefined".

    I'm starting to lose it. Why is the request not coming through? Any thoughts?

Actions