dottedquad
dottedquad

Reputation: 1391

Intel Edison Arduino breakout board: How do I recieve all of the i2c result bytes?(NodeJS)

I have an Intel Edison Arduino breakout board, Atlas Scientific EZO PH sensor and an Atlas Scientific Power Isolator.

I have the power isolator between the Intel Edison Arduino break-out board i2c bus (A4/A5) and the ph sensor.

The Atlas Scientific data sheets can be found here:
EZO Ph sensor: PH Data-sheet
Power Isolator: Power Isolator Data-sheet

NodeJS code:

var m = require('mraa');  
var i2c = new m.I2c(1);  

i2c.address(0x63);  
i2c.write("R,56.26");  

console.log("Reading I2C..");  

function readPH() {  
    var data = i2c.read();  
    console.log( ">> I2C value: " + data);  
}  
setTimeout(function (e) { readPH(); }, 1000);

I send the command R,56.26 to the ph sensor, wait 1 second and then execute i2c.read();.

I see the lights blink and change color the way they are supposed to when the command is sent and when the i2c.read() is executed, so I know I am requesting a reading and getting a result.

The EZO PH sensor data-sheet explains:
EZO PH Sensor Result response
enter image description here

I seem to only be able to store the first bit of my result instead of the full 7 bytes. I seem to always return a 1. The 1 that I am receiving corresponds to "Success". To troubleshoot further, I sent R,34.53 to the ph sensor and then prematurely executed i2c.read(); and received 254 which corresponds to "Pending". So, I believe I am receiving the result or a partial result from the EZO PH sensor.

I am at a loss as to how I to store the 7 byte result. I don't know the inner workings of the mraa nodejs library API. So, I am not sure if I should use an object, array, or a variable to store the result, or if I am missing an argument in the i2c.read();

UPDATE

I read through the mraa github example section which explains, "There is no explicit nodejs API documentation, see the examples. The API itself is very similar to the python one but with a js syntax." So I wrote a rather simple python script that gets the Ph Sensor value successfully.

Python code:

import time
import mraa

i2c = mraa.I2c(1)
i2c.address(0x63)

i2c.write("R,23.5")

time.sleep(1.3)
d = "       "
i2c.read(d)
print(d)

The python code outputs: 2.974 Getting a result proves my circuit design works and now I need to figure out what the NodeJS API is to get a successful i2c reading. Does anyone know the NodeJS API for getting an i2c reading?

UPDATE
Working example code:

var m = require('mraa');
var i2c = new m.I2c(1);
i2c.address(0x63);
i2c.write("R,56.26");
console.log("Reading I2C..");
function readPH() {
    var d = i2c.read(7);
    console.log(">> " + d);
}
setTimeout(function (e) { readPH(); }, 1000);

Upvotes: 0

Views: 2471

Answers (2)

dottedquad
dottedquad

Reputation: 1391

As @Fumu 7 explained, i2c.read() does not read series of data but read one byte of data only. i2c.read(7) will read a stream of 7 bytes.
Working example code:

var m = require('mraa');
var i2c = new m.I2c(1);
i2c.address(0x63);
i2c.write("R,56.26");
console.log("Reading I2C..");
function readPH() {
    var d = i2c.read(7);
    console.log(">> " + d);
}
setTimeout(function (e) { readPH(); }, 1000);

Upvotes: 0

Fumu 7
Fumu 7

Reputation: 1091

'i2c.read();' does not read series of data but read one byte of data only. Your code use 'i2c.read();' once, so you got one byte only.

To read all data until NULL comes, readPH function may need to modify as follows. (This code is not tested because I don't have arduino)

function readPH() {  
    char readout[7]; //
    char aChar;
    int i;
    for (i=0; i<7; i++) {
        aChar = i2c.read();
        readout[i]=aChar;
        if (aChar== NULL){ // all data have been read.
            console.log( ">> I2C value: " + readout);  
        }
    }
 }

Upvotes: 1

Related Questions