Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Position of LET Definition, and JSON.parse

I try to improve the DS18b20 example to easily use it with Thingspeak.

I defined a "TP_Config.json" file:
{
    "TP_Channel": "12345",
    "API_Key": "12345679890",
    "fields": [{
        "28ffdcdd5315029b": "field1",
        "28ff269bb5160374": "field2",
        "28ff66c153150243aa": "field3",
        "28ffeec3531502b1aa": "field4",
        "28ff1e1d51150312": "field5",
        "28ff77c1531502f2": "field6",
        "28ff4fc554150362": "field7",
        "28fffffb53150254": "field8"
    }]
}

I read this file to an object with:
let myConf = JSON.parse(File.read('TPConfig.json'));

In init.js, I load the file load('ds18b20.js'); with the DS18b20 implementation before the JSON.parse code.

The following WORKS:
in DS18B20.js

let readTemp = function(rom) {
    let DATA = {
        TEMP_LSB: 0,
        TEMP_MSB: 1,
        REG_CONF: 4,
        SCRATCHPAD_SIZE: 9
    };
    let REG_CONF = {
        RESOLUTION_9BIT: 0x00,
        RESOLUTION_10BIT: 0x20,
        RESOLUTION_11BIT: 0x40,
        RESOLUTION_MASK: 0x60
    };
    let CMD = {
        CONVERT_T: 0x44,
        READ_SCRATCHPAD: 0xBE
    };
    let data = [];
    let raw;
    let cfg;
    ow.reset();
    ow.select(rom);
    ow.write(CMD.READ_SCRATCHPAD);
    for (let i = 0; i < DATA.SCRATCHPAD_SIZE; i++) {
        data[i] = ow.read();
...
....
};

The following code is NOT WORKING:

let DATA = {
        TEMP_LSB: 0,
        TEMP_MSB: 1,
        REG_CONF: 4,
        SCRATCHPAD_SIZE: 9
    };
    let REG_CONF = {
        RESOLUTION_9BIT: 0x00,
        RESOLUTION_10BIT: 0x20,
        RESOLUTION_11BIT: 0x40,
        RESOLUTION_MASK: 0x60
    };
    let CMD = {
        CONVERT_T: 0x44,
        READ_SCRATCHPAD: 0xBE
    };
let readTemp = function(rom) {
....
....

I want to use the definiions as global code.

Error:

[Jul 12 23:34:34.248]   at init.js:23
[Jul 12 23:34:34.248] MJS error: invalid JSON string

What is my mistake ?

Regards
Holger

Comments

  • CapsicumCapsicum Germany

    Hello,
    I continued testing:

    in ds18b20.js the following library is loaded:
    load('api_arduino_onewire.js');

    If I put the code before this line it works.

    If I put the code behind this line it will cause the error.

    I will check now api_arduino_onewire.js

    Holger

Sign In or Register to comment.