1 2 Previous Next 16 Replies Latest reply on Jan 18, 2017 9:50 AM by Intel Corporation

    Using the Iotkit-comm library with Eclipse and Iot Analytics

    PandaBanda

      Good Day

       

      I'm working on a project involving uploading data from the edison board to the iot analytics site from eclipse using the iotkit-comm library. Doing it this way seems to bypass or work seperately  from the iotkit-admin used in the command line. I'm currently testing my code working towards integration of two parts and every time I run it, it creates an new component element. So I get to a point where I cant display data on my graph because I have too many elements. Is there anyway to delete or reset components using iotkit-comm or any other means, since I cant use iotkit-admin commands?

       

      My code so far looks as follows:

       

      #include <stdio.h>
      #include <signal.h>
      #include <unistd.h>
      #include <mraa/aio.h>
      #include <stdbool.h>
      #include <sys/types.h>
      #include "iotkit-comm.h"
      #include "util.h"
      
      ServiceSpec *srvSpec = NULL;
      ServiceSpec *srvSpec1 = NULL;
      int msgnumber = 13; // sensor value
      sig_atomic_t volatile isrunning = 1;
      void sig_handler(int signum);
      
      mraa_aio_context adc_a3, adc_a0;
      uint16_t adc_value = 0, adc_value2 = 0;
      float Volt, Curr;
      
      void callback(void *handle, int32_t error_code, void *serviceHandle) {
          Context context;
          char msg[256];
          int i = 0;
          CommHandle *commHandle = NULL;
      
          if(serviceHandle != NULL) {
              commHandle = (CommHandle *) serviceHandle;
      
              int (**publish) (char *message,Context context);
      
              publish = commInterfacesLookup(commHandle, "publish");
              if(publish == NULL) {
                  fprintf(stderr, "Function \'publish\' is not available; please verify the Plugin documentation !!\n");
                  return;
              }
      
              context.name = "topic";
              context.value = "data";
      
              while(i < 10) {  // Event Loop
              adc_value = mraa_aio_read(adc_a3);
              Volt=((float)adc_value/1023)*(float)5.0;
                  sprintf(msg, "{\"name\": \"Volt\", \"value\": %.2f}", Volt);
                  printf("Publishing msg:%s\n", msg);
      
                  (*publish)(msg, context);
                  usleep(50000);
      
                  adc_value2 = mraa_aio_read(adc_a0);
                  Curr=((float)adc_value2/1023)*(float)5.0;
                  sprintf(msg, "{\"name\": \"Curr\", \"value\": %.2f}", Curr);
                  printf("Publishing msg:%s\n", msg);
      
                  (*publish)(msg, context);
                  usleep(50000);
      
                 i++;
      
              }
          }
          // clean the objects
          cleanUpService(&srvSpec, &commHandle);
          exit(0);
      }
      
      void callback1(void *handle, int32_t error_code, void *serviceHandle) {
          Context context;
          char msg[256];
          int i = 0;
          CommHandle *commHandle = NULL;
      
          if(serviceHandle != NULL) {
              commHandle = (CommHandle *) serviceHandle;
      
              int (**publish) (char *message,Context context);
      
              publish = commInterfacesLookup(commHandle, "publish");
              if(publish == NULL) {
                  fprintf(stderr, "Function \'publish\' is not available; please verify the Plugin documentation !!\n");
                  return;
              }
      
              context.name = "topic";
              context.value = "data";
      
              while(i < 10) {  // Event Loop
              adc_value2 = mraa_aio_read(adc_a0);
              Curr=((float)adc_value2/1023)*(float)5.0;
                  sprintf(msg, "{\"name\": \"Curr\", \"value\": %.2f}", Curr);
                  printf("Publishing msg:%s\n", msg);
      
                  (*publish)(msg, context);
                  sleep(1);
      
                 i++;
      
              }
          }
          // clean the objects
          cleanUpService(&srvSpec, &commHandle);
          exit(0);
      }
      
      int main(void) {
          puts("Sample program to publish data to IoT Cloud !!");
      
          signal(SIGINT, &sig_handler);
      
          adc_a3 = mraa_aio_init(3);
          adc_a0 = mraa_aio_init(0);
          if(adc_a3 == NULL && adc_a0 == NULL)
          {
          return 1;
          }
          mraa_aio_set_bit(adc_a3,10);
          mraa_aio_set_bit(adc_a0,10);
      
          while(1)
          {
           srvSpec = (ServiceSpec *) parseServiceSpec("/usr/share/iotkit-comm/examples/c/serviceSpecs/InstaPower.json");
           srvSpec1 = (ServiceSpec *) parseServiceSpec("/usr/share/iotkit-comm/examples/c/serviceSpecs/InstaCurrent.json"), srvSpec;
      
      
           if (srvSpec1){
               advertiseServiceBlocking(srvSpec, callback);
           }
      
           if (srvSpec1){
                  advertiseServiceBlocking(srvSpec1, callback1);
              }
      
           return 0;
          }
      }
      
      void sig_handler(int signum)
      {
        if(signum == SIGINT)
        isrunning = 0;
      }
      

       

      It's kind off a mess since I'm still working on it but basically running it as is, it takes two readings from analog inputs each with their own component service specification. Right now only one callback function is invoked but I'm publishing both values. The code is based off of tutorials found here: Tutorials: src/lib/iotkit-comm/iotkit-comm.c File Reference

      The resource material is limited so I'm pretty much going at it on my own at the moment but If anyone knows more about it, help would be very much appreciated.

       

      Thanks

        1 2 Previous Next