389102 (8) [Avatar] Offline
#1
This is a good and instructive book,

i want to implement exercise in chapter8.2.2

i did't find "chapter 5 create HTML representations of resources" wrote in chapter8.2.2.

Do you have provided more instruction about 8.2.2 ? i am not really understand how to use the API of Pi created in Chapter 5 to implement this crawler exercise.

how could i turn on led in chapter8 like in chapter7.2.4?

Thanks a lot!
domguinard (69) [Avatar] Offline
#2
Hi,

First of all a big sorry for taking so long to reply, for some reason Vlad and I do not get the notifications lately (we need to see what's going on!).

>i did't find "chapter 5 create HTML representations of resources" wrote in chapter8.2.2.
This is a mistake on our side. It should say: chapter 7 (section 7.2.3), we corrected this now.

>Do you have provided more instruction about 8.2.2 ? i am not really understand how to use the API of Pi created in Chapter 5 >to implement this crawler exercise.
The idea is to implement it as shown in the pseudo code of Listing 8.2:
you know what the API of the Thing is as it uses REST so you can find the links to the resources such as the sensors or LEDs.

From the root HTML page of the web Thing, the crawler can find the sub-resources, for example, sensors and actuators, by discovering outgoing links and can then create a resource tree of the web Thing and all its resources. The crawler then uses the HTTP OPTIONS method to retrieve all verbs supported for each resource of the web Thing. Finally, the crawler uses content negotiation to understand which format is available for each resource.

If you want a source of inspiration have a look at Chapter 2.5 Exercise 4—Tell the world about your device
This is quite similar, we use the well-know API (links, HTTP verbs, payloads) to generate a UI automatically.

Finally, in Chapter 10 (10.1 Building a simple app—automated UI generation) you learn how to build a similar UI based on the Web Thing Model of the end of Chapter 8. It contains an implementation of a similar algorithm.

>how could i turn on led in chapter8 like in chapter7.2.4?
The way you do this is via Actions (see 8.3.4). It is much cleaner to change the state of a device via an Action rather than a Property change like we did it in chapter 7. Properties should only be changed by the device itself as they reflect the state of the device. A bit like in Java you have private interfaces and public interfaces.

In Listing 10.2 you can also see how to implement a UI that uses Actions to change the state of LEDs. The Pi receives the Action and then changes the state of the LED (which will be reflected via a Property change as well).

These are very good questions on some of the most complex parts of the book, we hope this helps a bit, let us know.

389102 (8) [Avatar] Offline
#3
WoT UI
[ 39 KB ]
I really appreciate for your detailed reply

I followed the book step use "chapter 9" code to Start a secure WoT Pi server ,open https://raspberrypi.local:8484/?token=X and replace "X" with "My API Token",i see the root page of pi but

open the ui.html file and append “?token=X”(replace "X" with "My API Token") to the URL ,i did't see the WoT UI page which was automatically generated (like figure 10.2 left),"WoT UI" is in the

attachment displayed in my computer.

Thank you in advance

389102 (8) [Avatar] Offline
#4
I input
"curl -i -H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST 'http://localhost:8484/pi/actuators/actions/ledState' \
-d '{
"ledId":"1","state":true
}' "
then turn on led1, but when i change "ledId":"2 and ALL" still only turn on led1 in chapter 8.3.4

i wanted to turn on led2 or both (led1 and led2 ) in "chapter8", should I change "ledsPlugin.js" (model = resources.pi.actuators.leds['1']; ) code like in chapter7 and turn on other led in chapter8

but i did't know where should i modify in chapter8

thanks a lot!
389102 (8) [Avatar] Offline
#5
fig2.jpg
[ 208 KB ]
fig1.jpg
[ 161 KB ]
fig3.jpg
[ 198 KB ]
I simulated a coap server in my computer and plugin copper in firefox ,i click "GET" but i didn't see any response from the browser(fig1) ,i can't get co2 value when i started wot-server(fig3 upper)

I simulated a coap server in raspberry pi 2 ,i didn't get any response about co2 value (fig2) ,i can get co2 value when i started wot-server(fig3 bottom) in chapter7.3

how should i simulate a coap server in computer and get response from copper?

thanks a lot!


domguinard (69) [Avatar] Offline
#6
389102 wrote:I input
"curl -i -H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST 'http://localhost:8484/pi/actuators/actions/ledState' \
-d '{
"ledId":"1","state":true
}' "
then turn on led1, but when i change "ledId":"2 and ALL" still only turn on led1 in chapter 8.3.4

i wanted to turn on led2 or both (led1 and led2 ) in "chapter8", should I change "ledsPlugin.js" (model = resources.pi.actuators.leds['1']; ) code like in chapter7 and turn on other led in chapter8

but i did't know where should i modify in chapter8

thanks a lot!


Indeed, the code as it is only works for one LED, adding more LEDs is left as an exercise for the reader.

To change the code to work with LED 2 you'll need:
1) To make sure the GPIO corresponding to LED 2 is setup correctly in the model (piNoLD.json)
2) change the code of ledPlugin.js:


LedsPlugin.prototype.createValue = function (data){
  return {"1" : data, "2" : false, "timestamp" : utils.isoTimestamp()};
};


to
"2" : data


and

LedsPlugin.prototype.connectHardware = function () { //#F
var Gpio = require('onoff').Gpio; //#G
var self = this;
actuator = new Gpio(self.model.values['1'].customFields.gpio, 'out');
console.info('Hardware %s actuator started!', self.model.name);
};

to load the GPIO for LED2
self.model.values['2'].customFields.gpio


If you want to make the code support several LEDs you'll need to change the code to setup an array of actuators, each corresponding to the LEDs you made available. Then you'll need to turn them on/off depending on the incoming message.

Hope that helps!
domguinard (69) [Avatar] Offline
#7
389102 wrote:I input
"curl -i -H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST 'http://localhost:8484/pi/actuators/actions/ledState' \
-d '{
"ledId":"1","state":true
}' "
then turn on led1, but when i change "ledId":"2 and ALL" still only turn on led1 in chapter 8.3.4

i wanted to turn on led2 or both (led1 and led2 ) in "chapter8", should I change "ledsPlugin.js" (model = resources.pi.actuators.leds['1']; ) code like in chapter7 and turn on other led in chapter8

but i did't know where should i modify in chapter8

thanks a lot!


Replied to this in another thread, it's best to use separate threads when you ask different questions:
https://forums.manning.com/posts/list/38019.page