Software Archive
Read-only legacy content
17061 Discussions

IoT dashboard Control/Actuator working code or tutorial?

David_B_6
Beginner
4,826 Views

Hi there,

I am new to all of this, but fairly technical, UNIX guy, some coding background but I am not a developer full time and am trying to learn Intel's Edison IoT.  I have a new Edison and an Arduino and am using the Arduino IDE and SSH to the device. When I started I re-flashed the Edison with the latest and set up an IoT Analytics account. I have the device registered with and and able to send to the analytics dashboard (https://dashboard.us.enableiot.com) and it works fine. I have sent a number of observations temp, humidity and a "seismograph" sensor I created. It all worked fine. I am also able to connect to the Arduino/Edison and read and activate a number of Grove sensors, etc. and it all works fine. I can turn lights on, flash them, read the sound, temp sensors, turn on the relay, etc. all fine.

What I am REALLY trying to do is control an LED from the Cloud. After reading all of the tutorials and getting this far I tried to run the Actuator example program and it didn't work. First it was syntax errors by I managed to correct those (conflicting libraries between the iotkit and arduino libraries. The code compiles fine and I am able to successfully send a command (LED On, value 1, component "powerswitch", command "LED.v1.0", value 1 and did this 5 times successfully tonight.

The problem is nothing happens on the board! No light, no message in the serial window, no message to the user, no error, nothing.

I read that you have to enable mgtt mode not REST so I did that but I'm at a loss. the sample code is complicated to me and I don;t udnerstand all parts of what it is doing. My specific questions are:

  • how can I view/see a log to even know if the Edison received the call from the cloud?
  • I know the agent is running on the device and can send commands directly from the terminal on my Mac and they work. 
  • In looking at the code it is unclear to me where the payload is coming in from the cloud server - I see some aJsonObject lines and assume that's them but have no idea what's supposed to be happening there and if it is

Here's the sample code that comes with the iotkit dist (I haven't edited it at all but would change "component" and command to match mine (powerswitch and LED.v1.0). (I am particularly curious about the iotkit.send("power", 1) - why would it be sending when the objective is to receive a 1 or 0 from the cloud??)

//This example reacts for default actuator component if registered on device
//LED.v1.0 command is used
//When executed from Control section on dashboard with value 0, LED light is turned off
//When executed from Control section on dashboard with value 1, LED light is turned on
//iotkit-agent must use MQTT connection (not REST) for actuation to work.
//It also sends 1 as power when agent is started.

#include <IoTkit.h>    // include IoTkit.h to use the Intel IoT Kit
#include <Ethernet.h>  // must be included to use IoTkit
#include <aJSON.h>
#include <stdio.h>

// create an object of the IoTkit class
IoTkit iotkit;        
int temp;
char buf[112];

void setup() {
  Serial.begin(115200);
  // call begin on the IoTkit object before calling any other methods
  iotkit.begin();
  delay(10500);
  iotkit.send("power", 1);
}

void loop() {
  iotkit.receive(callback);
  delay(5000);
}

void callback(char* json) {
  Serial.println(json);
  aJsonObject* parsed = aJson.parse(json);
  if (&parsed == NULL) {
    // invalid or empty JSON
    Serial.println("recieved invalid JSON");
    return;
  }
   
  aJsonObject* component = aJson.getObjectItem(parsed, "component");
  aJsonObject* command = aJson.getObjectItem(parsed, "command"); 
  aJsonObject* argv = aJson.getObjectItem(parsed, "argv");
  aJsonObject* argvArray = argv->child;
  aJsonObject* name = argvArray->child; // name : on
  aJsonObject* value = name->next; // value: 1/0
  
  if ((component != NULL)) {
    if (strcmp(component->valuestring, "actuator") == 0) {
      if ((command != NULL)) {
        if (strcmp(command->valuestring, "light") == 0 && strcmp(value->valuestring, "0") == 0) {
          Serial.println("Light Off!");
          pinMode(13, OUTPUT);
          digitalWrite(13, false);
        }
        if (strcmp(command->valuestring, "light") == 0 && strcmp(value->valuestring, "1") == 0) {
          Serial.println("Light on!");
          pinMode(13, OUTPUT);
          digitalWrite(13, true);
        }
      }
    }
  }
}

Thanks to anyone who can help - I am at a standstill at this point and appreciate any feedback that could get me sorted.

D.

 

0 Kudos
30 Replies
David_B_6
Beginner
1,542 Views

Ok, the LED on Pin 13 should turn on, not just blink.

I am not using the tail log so can't tell you what I see, but if you open the serial monitor you should see the payload arrive.

To access the serial monitor select the small magnifying glass icon on the top, right of the Arduino IDE screen. A window with white background should appear. Now, go to the IoT dashboard, select the menu then pick Control, then the name of your device (mine is called Interactio_Hub), then the Component (mine is called power) then Parameter Name (LED 0,1) and the Value (0 or 1) then Transport Type (mqtt). Once these are all selected then hit Add Action and last select Send all valid actions.

Now watch the serial monitor - it seems to take between 3-4 seconds and you should see the text appear (I have a few in here and I have extra printlns in the code, but the line you are looking for starts with "Good JSON Command from Server...":Screen Shot 2015-02-06 at 12.40.21 PM.png

and the LED should come on (and stay on). If that's not working I'm not sure what you should do, other than try rebooting everything.

Hope it helps - David

 

 

 

0 Kudos
David_B_6
Beginner
1,542 Views

Jakub - one thing I just thought of - make sure you change the PIN in my code to 13 (I used 8 because I am running a grove expansion board and have an external LED plugged into D8). Yours should also say powerswitch not power and should look like this with just the default Edison & Arduino:

        if (strcmp(value->valuestring, "1")) { //changed to LED.v1.0 from light
          Serial.println("continue, now after strcmp command LED line in loop");
          Serial.println("Light Off!");
          pinMode(13, OUTPUT);
          digitalWrite(13, 0); //should false be changed to 0?
        }
//      if (strcmp(command->valuestring, "LED") == 0 && strcmp(value->valuestring, "1") == 0) {
        if (strcmp(value->valuestring, "0")) {
          Serial.println("Light on!");
          pinMode(13, OUTPUT);
          digitalWrite(13, 1);

Also, I verified the latest Example code that Brian mentioned above. it ALMOST works! I had to comment out the first line in the nested IF loop like this (and again mine is set for pin 8 not 13):

Screen Shot 2015-02-06 at 1.03.47 PM.png

0 Kudos
Jakub_C_
Beginner
1,542 Views

Seems problem is not in the code as I cannot see payload arriving in serial monitor nor log :-/

0 Kudos
David_B_6
Beginner
1,542 Views

Hmmm, well Brian and Joanna have been very helpful to me so hopefully they'll get your sorted as well. the one thing I can twll you is every time I didn't get the package it was because of the loop, but if you run the exact code I sent you (after changing pins from 8 to 13) it should send the payload. If not seems it's on the iot-kit agent side. One last thought, are you sure the agent is running on your EDISON?

systemctl status iotkit-agent

It should look like this:

root@Interactio_Hub:/usr/lib/node_modules/iotkit-agent# 
root@Interactio_Hub:/usr/lib/node_modules/iotkit-agent# systemctl status iotkit-
agent
● iotkit-agent.service - iotkit-agent
   Loaded: loaded (/lib/systemd/system/iotkit-agent.service; disabled)
   Active: active (running) since Fri 2015-02-06 05:53:34 UTC; 15h ago
 Main PID: 280 (node)
   CGroup: /system.slice/iotkit-agent.service
           └─280 node /usr/bin/iotkit-agent
 
Feb 06 20:50:42 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:50:42.888Z - ...
Feb 06 20:50:44 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:50:44.251Z - ...
Feb 06 20:51:43 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:51:43.718Z - ...
Feb 06 20:51:45 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:51:45.329Z - ...
Feb 06 20:52:44 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:52:44.847Z - ...
Feb 06 20:52:46 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:52:46.323Z - ...
Feb 06 20:53:45 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:53:45.988Z - ...
Feb 06 20:53:47 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:53:47.475Z - ...
Feb 06 20:54:46 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:54:46.807Z - ...
Feb 06 20:54:48 Interactio_Hub iotkit-agent[280]: 2015-02-06T20:54:48.312Z - ...
Hint: Some lines were ellipsized, use -l to show in full.
root@Interactio_Hub:/usr/lib/node_modules/iotkit-agent#

if it is not you can start it like this:

root@Interactio_Hub:/usr/lib/node_modules/iotkit-agent# systemctl start iotkit-
agent

Good luck, David

0 Kudos
Jakub_C_
Beginner
1,542 Views

I get very similar output as you David :-)

systemctl status iotkit-agent
● iotkit-agent.service - iotkit-agent
   Loaded: loaded (/lib/systemd/system/iotkit-agent.service; disabled)
   Active: active (running) since Fri 2015-02-06 20:18:27 UTC; 1h 10min ago
 Main PID: 309 (node)
   CGroup: /system.slice/iotkit-agent.service
           └─309 node /usr/bin/iotkit-agent

Feb 06 21:28:41 jakub iotkit-agent[309]: 2015-02-06T21:28:41.395Z - info: Su...3
Feb 06 21:28:41 jakub iotkit-agent[309]: 2015-02-06T21:28:41.401Z - info: Su...1
Feb 06 21:28:43 jakub iotkit-agent[309]: 2015-02-06T21:28:43.441Z - info: Re...0
Feb 06 21:28:43 jakub iotkit-agent[309]: 2015-02-06T21:28:43.453Z - info: Re...0
Feb 06 21:28:50 jakub iotkit-agent[309]: 2015-02-06T21:28:50.616Z - info: Su...3
Feb 06 21:28:50 jakub iotkit-agent[309]: 2015-02-06T21:28:50.622Z - info: Su...1
Feb 06 21:28:52 jakub iotkit-agent[309]: 2015-02-06T21:28:52.285Z - info: Re...0
Feb 06 21:28:52 jakub iotkit-agent[309]: 2015-02-06T21:28:52.373Z - info: Re...0
Feb 06 21:29:01 jakub iotkit-agent[309]: 2015-02-06T21:29:01.793Z - info: Su...1
Feb 06 21:29:04 jakub iotkit-agent[309]: 2015-02-06T21:29:04.332Z - info: Re...0
Hint: Some lines were ellipsized, use -l to show in full.

 

I put other code for a momment (logging temp to intel cloud) for momment to check if there is no problem with communication and sending data to cloud works like a charm.

Receiving unfortunately don't work at all... very strange.

 

0 Kudos
Jakub_C_
Beginner
1,542 Views

Guys, great success. I can receive JSON right now, I flashed my edison and followed Brian's steps. He forgot about: iotkit-admin protocol mqtt...

BTW. there is new version of iotkit-admin 1.6.5. Maybe that solved my issue?

0 Kudos
Brian_B_Intel
Employee
1,542 Views

That's great, Jakub. I'll edit my post to add the protocol setting.

Agent 1.6.4 was needed to talk to the latest Cloud server via MQTT.
Agent 1.6.5 contained a fix for the "catalog" issue you and David identified.

0 Kudos
Brian_B_Intel
Employee
1,542 Views

Hi David/Jakob. 

While your install instructions are correct (from the Agent wiki), they are for a local install. This is for systems that don't have the agent pre-installed (i.e., non-Edison and non-Galileos). Given that most people here are probably using one of those boards, we should make that more clear in the wiki and emphasize the update process.

Jakob, your issue with the "catalog" is correct. The command currently doesn't work under MQTT for some reason.

If you don't mind losing sensor data for your device, starting from scratch - on a newly flashed Edison/Galileo. These are the steps I would follow:

  1. # npm update -g iotkit-agent
    Lots of "npm..." lines
  2. # iotkit-admin -V
    Should read "1.6.4"
  3. # iotkit-admin test
    Should return with no errors and "...Build: 0.12.0"
  4. # iotkit-admin move-data-directory ~/.data
    Saves your config files in ~/.data/
  5. # iotkit-admin set-device-id <your_device_ID>
    Should get "Device ID set to: <your_device_ID>
  6. # iotkit-admin device-id
    Should get "Device ID: <your_device_ID>
  7. # iotkit-admin activate <activation_code>
    Should get "Device ID: <your_device_ID>
  8. # iotkit-admin register power powerswitch.v1.0
    Should not get any errors (this registers the "power" actuator used in the IoTKitActuationExample example)
  9. # systemctl restart iotkit-agent
    Restart the agent process anytime you add a new component
  10. # iotkit-admin protocol mqtt
  11. # tail -f /tmp/agent.log
  12. Run your Arduino actuation example sketch (IoTKitActuationExample)
  13. Submit a control request from the IoT Analytics website

Check that you have the latest examples from https://github.com/enableiot/iotkit-samples . They were updated earlier this week and fixed some of the issues David ran into. I attached a log of my session.

Brian

0 Kudos
Zoran_R_1
Beginner
1,542 Views

Hi,

i had problem with npm -g update iotkit-agent until update npm. 

Version npm on Galileo was 1.4.28 and I couldn't update iotkit-agent. After "googling", found that probledm could be with old version npm.

npm ERR! EEXIST, open '/home/root/.npm/d5a02e65-root-npm-debug-2-2-0-package-tgz.lock'                                                                  
File exists: /home/root/.npm/d5a02e65-root-npm-debug-2-2-0-package-tgz.lock                                                                             
Move it away, and try again.   

 

problem solved with npm -g install npm@next

Verion npm is now : 2.10.1

0 Kudos
Zoran_R_1
Beginner
1,542 Views

Hi,

I would like share solution from another post [ https://software.intel.com/en-us/forums/topic/558499 ] I've got from Intel support :)

My app send/receive data from cloud during first setup until reboot...After reboot/power on not...unitl executing systemctl restart iotkit-agent

Summary from post:

  • iotkit-admin BYPASS iotkit-agent to connect to cloud.You could have situation that iotkit-admin test work perfect, but not iotkit-agent
  • iotkit-agent is Linux service, and it could be active BUT DISABLED !!!!
  • systemctl start / restart iotkit-agent work only for current session BUT after reboot iotkit-agent go to disabled status

Look at Jakub comment

systemctl status iotkit-agent
● iotkit-agent.service - iotkit-agent
   Loaded: loaded (/lib/systemd/system/iotkit-agent.service; disabled)
   Active: active (running) since Fri 2015-02-06 20:18:27 UTC; 1h 10min ago
 Main PID: 309 (node)
   CGroup: /system.slice/iotkit-agent.service
           └─309 node /usr/bin/iotkit-agent

You could see what I mean : disabled !!!!!

Don't use hammer :), just type : systemctl enable iotkit-agent and everything goes to work.!

0 Kudos
Reply