Community
cancel
Showing results for 
Search instead for 
Did you mean: 
QVanD
Novice
1,658 Views

ReferenceError: thingShadows is not defined

Jump to solution

Hello--

I'm trying to get the AWS IOT Javascript sample going for my Edison board (https://github.com/intel-iot-devkit/aws-iot-intel/tree/master/Javascript aws-iot-intel/Javascript at master · intel-iot-devkit/aws-iot-intel · GitHub)

When I go to execute the code I receive the following error:

root@edison:~/aws-iot-intel-master/Javascript# node Edison-JS-Sample.js

 

registering device: Device Name

 

 

/home/root/aws-iot-intel-master/Javascript/Edison-JS-Sample.js:156

 

thingShadows.update(Device_Name, device_state );

 

^

 

ReferenceError: thingShadows is not defined

 

at send_state

 

(/home/root/aws-iot-intel-master/Javascript/Edison-JS-Sample.js:156:4)

 

at read_sensor

 

(/home/root/aws-iot-intel-master/Javascript/Edison-JS-Sample.js:142:4)

 

at null.

 

(/home/root/aws-iot-intel-master/Javascript/Edison-JS-Sample.js:104:4)

 

at wrapper [as _onTimeout] (timers.js:261:14)

 

at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

 

I did copy the common, device, and thing directories were in the same directory as the sample code:

root@edison:~/aws-iot-intel-master/Javascript# ls -alh

 

drwxrwxrwx 5 root root 4.0K Nov 25 03:25 .

 

drwxrwxrwx 10 root root 4.0K Nov 10 14:17 ..

 

-rw-r--r-- 1 root root 3.9K Nov 25 03:24 Edison-JS-Sample.js

 

-rw-r--r-- 1 root root 3.9K Nov 25 03:03 Edison-JS-Sample.js.orig

 

-rw-r--r-- 1 root root 1.1K Nov 10 13:41 LICENSE.md

 

-rw-r--r-- 1 root root 2.1K Nov 10 13:41 README.md

 

drwxr-xr-x 3 root root 4.0K Nov 25 03:21 common

 

drwxr-xr-x 3 root root 4.0K Nov 25 03:21 device

 

-rw-r--r-- 1 root root 100.9K Nov 10 13:41 edison_setup.jpg

 

drwxr-xr-x 2 root root 4.0K Nov 25 03:21 thing

 

Any suggestions? I'm more of a Python/Java type, and relatively new to node, so forgive me if this is super trivial.

 

1 Solution
QVanD
Novice
126 Views

So in the end I decided it wasn't worth reverse the original intent.

Again, I'm not fluent in node.js, but basic dev skills suggests to me that my issue wasn't unique to me, it is a flaw in the code.

My simple solution was to unwind the nested function calls and simplify the example in order to build forward from a working shell.

I replaced:

thingShadows

.on('connect', function() {

console.log('registering device: '+ Device_Name)

//register device

thingShadows.register(Device_Name);

//read sensor values and send to AWS IoT every 5 seconds

setInterval(function(){

read_sensor(send_state);

}, 5000);

});

With:

thingShadows

.on('connect', function() {

console.log('registering device: '+ Device_Name)

//register device

thingShadows.register(Device_Name);

//read sensor values and send to AWS IoT every 5 seconds

setInterval(function(){

read_lux = light.value();

read_temp = temp.value();

reported_state ={ lux: read_lux, temp: read_temp};

device_state={state: { reported: reported_state }};

//send update payload to aws:

thingShadows.update(Device_Name, device_state );

console.log('updated device state: '+ Device_Name + ' ' + util.inspect(reported_state, false, null));

}, 5000);

});

And added:

var util = require('util');

under the UPM dependencies.

Hope that helps others.

Cheers,

-flv7a

View solution in original post

9 Replies
Pablo_M_Intel
Employee
126 Views

Hi flv7a,

Did you encounter an error or issue when setting AWS SDK on the Intel Edison? Were you following a guide or document? I just checked the code and I don't see any error. Did you install all the necessary modules (the ones mentioned in the README file)?

Regards,

PabloM_Intel

QVanD
Novice
126 Views

PabloM--

I did a git clone of the https://github.com/aws/aws-iot-device-sdk-js aws/aws-iot-device-sdk-js · GitHub, but in looking at that page more closely it looks like I really also needed to do an 'npm install' afterwards. I'll give that a shot & sorry for the newbie question.

Cheers,

-flv7a

QVanD
Novice
126 Views

PabloM--

I double checked my previous steps this evening - I do believe that I'd previously installed the SDK correctly & I'm still having the same basic issue.

The steps I followed:

1. gti clone https://github.com/intel-iot-devkit/aws-iot-intel.git https://github.com/intel-iot-devkit/aws-iot-intel.git

2. cd aws-iot-intel-master/Javascript/

3. npm install aws-iot-device-sdk

4. npm install -g inherits

5. npm install -g mqtt

6. npm install -g minimist

7. cd $HOME/node_modules/aws-iot-device-sdk

8. cp -r device ../../aws-iot-intel-master/Javascript/

9. cp -r common ../../aws-iot-intel-master/Javascript/

10. cp -r thing ../../aws-iot-intel-master/Javascript/

11. cd ../../aws-iot-intel-master/Javascript/

12. node Edison-JS-Sample.js

Any other suggestions? Thanks in advance

Pablo_M_Intel
Employee
126 Views

Hi flv7a,

How are you copying the files to the Intel Edison? Also, did you try changing the name of the device? Also, have you tried changing the name of the executable file (Edison-JS-Sample) and the paths of the folders?

Regards,

PabloM_Intel

Pablo_M_Intel
Employee
126 Views

Hi flv7a,

Do you have updates on this case? Did you try our suggestions?

Regards,

PabloM_Intel

QVanD
Novice
126 Views

PabloM--

Sorry for the delayed response.

Answers to your questions:

How are you copying the files to the Intel Edison? I am not copying files to the Edison - instead I ssh'ed an did a combination of git clone & npm installs from within the Edison's OS.

Also, did you try changing the name of the device? No I did not - does this seem logical given what I'm seeing?

Also, have you tried changing the name of the executable file (Edison-JS-Sample) and the paths of the folders? No, I did not - I will gladly try - it just seemed like the issue was more related to the code within the 'thing' library, but again my relative inexperience with node is probably a hamstring here.

I will try each of the above & report back.

Cheers,

-flv7a

QVanD
Novice
126 Views

PabloM--

After some further debugging the issue really seems to me that when the code gets to line 156:

thingShadows.update(Device_Name, device_state );

the variable 'thingShadows' is out of scope - and hence the undefined reference error.

that object is obviously created on line 81, but my node.js inexperience is leaving me short in understanding if the nested call on line 104 should make the variable in scope or not.

Long story short, I don't think this is an issue related to names, directories, file copying, etc - it seems like a basic programming challenge around variable scoping.

Am I missing something? Have others gotten this same sample to execute successfully?

Thanks,

-flv7a

CClea
Innovator
126 Views

Yes this does appear to be a scope issue. A quick fix that gets it running is to move the thingShadows declaration out of the function and make it global. Not sure what the intent was, though, of having it inside the update_state function and if making it global has any adverse side-effects - just a disclaimer.

QVanD
Novice
127 Views

So in the end I decided it wasn't worth reverse the original intent.

Again, I'm not fluent in node.js, but basic dev skills suggests to me that my issue wasn't unique to me, it is a flaw in the code.

My simple solution was to unwind the nested function calls and simplify the example in order to build forward from a working shell.

I replaced:

thingShadows

.on('connect', function() {

console.log('registering device: '+ Device_Name)

//register device

thingShadows.register(Device_Name);

//read sensor values and send to AWS IoT every 5 seconds

setInterval(function(){

read_sensor(send_state);

}, 5000);

});

With:

thingShadows

.on('connect', function() {

console.log('registering device: '+ Device_Name)

//register device

thingShadows.register(Device_Name);

//read sensor values and send to AWS IoT every 5 seconds

setInterval(function(){

read_lux = light.value();

read_temp = temp.value();

reported_state ={ lux: read_lux, temp: read_temp};

device_state={state: { reported: reported_state }};

//send update payload to aws:

thingShadows.update(Device_Name, device_state );

console.log('updated device state: '+ Device_Name + ' ' + util.inspect(reported_state, false, null));

}, 5000);

});

And added:

var util = require('util');

under the UPM dependencies.

Hope that helps others.

Cheers,

-flv7a

View solution in original post

Reply