Connecting Rabbit uP (RCM6700) to Geeknesia



//
Posted on November 15, 2015 at 1:19 PM

//

Selama ini platform hardware popular yang digunakan sebagai device untuk koneksi IoT ke geeknesia adalah ESP8266, Arduino, Raspberry Pi dan Intel Galileo. Penulis menggunakan Rabbit uP sebagai alternatif untuk IoT terutama untuk solusi industrial.




Dalam pemilihan hardware harus disesuaikan dengan kebutuhan user. Banyak platform hardware dapat digunakan untuk proyek IoT namun ketika hardware tersebut dipasang 24 jam secara penuh faktor reliabilitas merupakan pertimbangan khusus.

Selain platform yang telah ada Rabbit uP menawarkan kemudahan fungsi-fungsi multitasking dan koneksi ethernet untuk dalam sebuah solusi IoT. Prosesor Rabbit telah digunakan pada proyek-proyek infrastruktur dan teruji di laboratorium lulus uji EMC (EMS dan EMI), power disturb dan lain-lain sesuai dengan standar internasional.

Basic tutorial Rabbit uP dapat di baca di : http://www.rantonic.com/page/1/rabbit-community 

Skenario proyek dasar ini adalah konsep komunikasi machine to machine (M2M) satu arah antar processor Rabbit melalui geeknesia dimana apabila kondisi Led menyala pada satu Rabbit harus diikuti oleh prosesor Rabbit lainnya nun jauh di sana melalui cloud geeknesia. Pengantar proyek ini merupakan langkah awal penulis untuk membuat sebuah industrial IoT gateway dan remote I/O controller berbasis IoT. Komunikasi menggunakan http method 'GET' json format.

Program Rabbit menggunakan Dynamic-C. 

PERHATIAN : Ada beberapa list program yang tidak digunakan, karena di-edit dari koleksi program yang ada...:P...list sebenarnya mungkin lebih pendek namun agak malas membersihkannya....

Program Transmit ke geeknesia :


#class auto
#use "RCM67xx.lib"
#use "sflash.lib"
#define TCPCONFIG 6   //TCPCONFIG for runtime setting

/*
#define TCPCONFIG           1
#define _PRIMARY_STATIC_IP  "192.168.2.39"
#define _PRIMARY_NETMASK    "255.255.255.0"
#define MY_GATEWAY          "192.168.2.1"
*/
#define MY_NAMESERVER       "192.168.0.1"


#define REMOTE_IP        "api.geeknesia.com"
 

#memmap xmem
#use "dcrtcp.lib"
#define PORT 80    //http port

#define SERF_TXPORT PDDR
#define SERF_RXPORT PDDR

#define FINBUFSIZE 31
#define FOUTBUFSIZE 31

#ifndef _232BAUD
#define _232BAUD 9600L
#endif

#define LED 0

char flash_buf[1056];
tcp_Socket sockslot[7];
char outbuf[512],append[16],routbuf[6],loutbuf[6],proving;

//Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)


void postponed()
{
  long int times;
  for (times=0L; times<=400L; times++)
  {
    #asm
    nop
    nop
    nop
    #endasm
  }
}

void sleep()
{
  long t;

  t = MS_TIMER;                   //Warming up delay...
    while((long)(MS_TIMER - t) <= 75);

}

//==============================================================================
main()

{
////////////////////////////////////////////////////////////////////////////////
   auto char buffer[255];
   auto int status;
    auto longword remoteip[7];
   auto char ip_user[15],msk[15],gtw[15];
   unsigned int sockslotflag[7]={0, 0, 0, 0, 0, 0, 0};

   char ascii_buffer[17];
    char fbyte, inchar;
    int i, j, k, pagenum, value, start, end, fc=0;
    char linebuf[80], *p, *buf, ch;
   char left,right,lShadow=0x00,rShadow=0x00,detect,dShadow=0;
   int leftflag=0,rightflag=0,ldflag=0,rdflag=0,digflag=0;
   char lproving=0x80,rproving=0x08;
   char cdig='0';

//Watchdog timer disabled

#asm
ld a,0x51
ioi ld (WDTTR),a
ld a,0x54
ioi ld (WDTTR),a
#endasm

   brdInit();
    sfspi_init();
    if (sf_init()) {
        printf("Flash init failed\n");
        exit(-1);
    }
    else {
        printf("Flash init OK\n");
        printf("# of blocks: %d\n", sf_blocks);
        printf("size of block: %d\n\n", sf_blocksize);
    }

   sf_pageToRAM(0);                  //Loading IP Address
   sf_readRAM(flash_buf, 0, 16);
   strcpy(ip_user,flash_buf);

    i=0;
   while(flash_buf[i]!='*')
       {
        ip_user[i]=flash_buf[i];
        i++;
      }

   sf_pageToRAM(1);              //Loading Netmask
   sf_readRAM(flash_buf, 0, 16);
   strcpy(msk,flash_buf);

   i=0;
   while(flash_buf[i]!='*')
       {
        msk[i]=flash_buf[i];
        i++;
      }

   sf_pageToRAM(2);
   sf_readRAM(flash_buf, 0, 16); //Loading Gateway
   strcpy(gtw,flash_buf);

   i=0;
   while(flash_buf[i]!='*')
       {
        gtw[i]=flash_buf[i];
        i++;
      }

   strcpy(gtw,"192.168.0.1*");

   printf("\nMy IP Address is : %s", ip_user);
   printf("\nMy Netmask is : %s", msk);
   printf("\nMy Gateway is : %s\n", gtw);
   //strcpy(ip_user,"192.168.2.39");
   //strcpy(msk,"255.255.255.0");
   //strcpy(gtw,"192.168.2.1");

   //Set internal port A 75K ohm pullup resistor

   WrPortI(PA0CR, NULL, 0x01);
   WrPortI(PA1CR, NULL, 0x01);
   WrPortI(PA2CR, NULL, 0x01);
   WrPortI(PA3CR, NULL, 0x01);
   WrPortI(PA4CR, NULL, 0x01);
   WrPortI(PA5CR, NULL, 0x01);
   WrPortI(PA6CR, NULL, 0x01);
   WrPortI(PA7CR, NULL, 0x01);

   //Assigned Port A as Input
   WrPortI(SPCR, &SPCRShadow, 0x080);


   //Port B assignment
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 2); //Assigned Pin B.2 as Output (Amplifier)
   BitWrPortI(PBDR, &PBDRShadow, 0, 2); //PB.2 init
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 3); //Assigned Pin B.3 as Output (Speaker)
   BitWrPortI(PBDR, &PBDRShadow, 0, 3); //PB.3 init
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 7); //Assigned Pin B.7 as Output (Logik)
   BitWrPortI(PBDR, &PBDRShadow, 0, 7); //PB.7 init

   //Assigned Port D as Output
   BitWrPortI(PDDDR, &PDDDRShadow, 1, LED); //Assigned Pin D.0 as Output (LED)
   BitWrPortI(PDDR, &PDDRShadow, 1, LED); //Set PD0 = OFF

   //Assigned Port E as Output
   WrPortI(PEDDR, &PEDDRShadow, 0xFF);
   WrPortI(PEDR, &PEDRShadow, 0x00);  //First Init


   serFopen(_232BAUD);


   sock_init();

   ifconfig(IF_ETH0,
    IFS_DOWN,
    //IFS_DHCP,0,
    IFS_IPADDR, aton(ip_user),// setup ip address
    IFS_NETMASK,aton(msk),    // setup netmask
    IFS_ROUTER_SET, aton(gtw),// default router
    IFS_UP,
    IFS_END);

   while(1)
   {


//TCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCP

//------------------------------------------------------------------------------
costate
    {
          if (sockslotflag[0]==0)
             {
               waitfor(DelaySec(4));
                    tcp_tick(NULL);
                    remoteip[0]=resolve(REMOTE_IP);
                    tcp_open(&sockslot[0],0,remoteip[0],PORT,NULL);
                    printf("Connecting to %s\n",REMOTE_IP);
                while(!sock_established(&sockslot[0]) && sock_bytesready(&sockslot[0])==-1)
                     {
                      tcp_tick(NULL);
                  fc++;
                  if (fc>=32750)
                      {
                         fc=0;
                        printf("Connection cannot be established....\n");
                        abort;
                     }
                     yield;
                     }
         sockslotflag[0]=1; //socket slot 0 is occupied
         printf("Socket Slot 0 is Connected...\n");
          sock_mode(&sockslot[0],TCP_MODE_ASCII);

          while(tcp_tick(&sockslot[0]))
         yield;

//------------------------------------------------------------------------------
            status=sock_alive(&sockslot[0]);
         if (status==0)
            {
               printf("User menutup koneksi socket slot 0... \n");
               waitfor(DelaySec(4));
               printf("Koneksi socket slot 0 tertutup... \n");
               sockslotflag[0]=0; //socket slot 0 is released
            }
            else
            {
                printf("Koneksi socket 0 tidak tertutup, koneksi hardware terputus...\n");
            }
 //-----------------------------------------------------------------------------
             }


    }

//000000000000000000000000000000000000000000000000000000000000000000000000000000

costate
    {


        if (sockslotflag[0]==1)
      {

         //printf("\n\nREQUEST : \n");

         if (digflag==0)
         {
           BitWrPortI(PDDR, &PDDRShadow, 1, LED);
           digflag=1;
           cdig='1';
           strcpy(outbuf,"GET http://api.geeknesia.com/api/data?api_key=api_key&attributes={%22FromRabbit%22:");
           append[0]=cdig;
           append[1]='}';
           append[2]='\n';
           append[3]='\r';
           append[4]= 0x00;
           strcat(outbuf,append);
           printf("Data dikirim : %s \n",outbuf);

         }
         else if (digflag==1)
         {
           BitWrPortI(PDDR, &PDDRShadow, 0, LED);
           digflag=0;
           cdig='0';
           strcpy(outbuf,"GET http://api.geeknesia.com/api/data?api_key=api_key={%22FromRabbit%22:");
           append[0]=cdig;
           append[1]='}';
           append[2]='\n';
           append[3]='\r';
           append[4]= 0x00;
           strcat(outbuf,append);
           printf("Data dikirim : %s \n",outbuf);
         }

         sock_flushnext(&sockslot[0]);
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf," HTTP/1.1\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"Host: api.geeknesia.com\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"X-Auth-Token: My Token\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"Connection: close\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));

         //printf("%s",outbuf); //verbose only

         //printf("RESPONSE : \n");
         waitfor(DelaySec(6));

      }

   }


////////////////////////////////////////////////////////////////////////////////
costate
    {

  if ((sock_bytesready(&sockslot[0])>=0)  && (sockslotflag[0]==1))
             {
             sock_gets(&sockslot[0],buffer,sizeof(buffer));
            printf("%s\n",buffer);

               }
  }

//------------------------------------------------------------------------------

//EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)
   }

serFclose();
////////////////////////////////////////////////////////////////////////////////

}


Program untuk receive data dari geeknesia :


#class auto
#use "RCM67xx.lib"
#use "sflash.lib"
#define TCPCONFIG 6   //TCPCONFIG for runtime setting

/*
#define TCPCONFIG           1
#define _PRIMARY_STATIC_IP  "192.168.2.39"
#define _PRIMARY_NETMASK    "255.255.255.0"
#define MY_GATEWAY          "192.168.2.1"
*/
#define MY_NAMESERVER       "192.168.0.1"


#define REMOTE_IP        "api.geeknesia.com"
 

#memmap xmem
#use "dcrtcp.lib"
#define PORT 80    //http port

#define SERF_TXPORT PDDR
#define SERF_RXPORT PDDR

#define FINBUFSIZE 31
#define FOUTBUFSIZE 31

#ifndef _232BAUD
#define _232BAUD 9600L
#endif

#define LED 0

char flash_buf[1056];
tcp_Socket sockslot[7];
char outbuf[512],append[16],routbuf[6],loutbuf[6],proving;

//Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)Routine(s)


void postponed()
{
  long int times;
  for (times=0L; times<=400L; times++)
  {
    #asm
    nop
    nop
    nop
    #endasm
  }
}

void sleep()
{
  long t;

  t = MS_TIMER;                   //Warming up delay...
    while((long)(MS_TIMER - t) <= 75);

}

//==============================================================================
main()

{
////////////////////////////////////////////////////////////////////////////////
   auto char buffer[255];
   auto int status;
    auto longword remoteip[7];
   auto char ip_user[15],msk[15],gtw[15];
   unsigned int sockslotflag[7]={0, 0, 0, 0, 0, 0, 0};

   char ascii_buffer[17];
    char fbyte, inchar;
    int i, j, k, pagenum, value, start, end, fc=0;
    char linebuf[80], *p, *buf, ch;
   char left,right,lShadow=0x00,rShadow=0x00,detect,dShadow=0;
   int leftflag=0,rightflag=0,ldflag=0,rdflag=0,digflag=0;
   char lproving=0x80,rproving=0x08;
   char cdig='0';

//Watchdog timer disabled

#asm
ld a,0x51
ioi ld (WDTTR),a
ld a,0x54
ioi ld (WDTTR),a
#endasm

   brdInit();
    sfspi_init();
    if (sf_init()) {
        printf("Flash init failed\n");
        exit(-1);
    }
    else {
        printf("Flash init OK\n");
        printf("# of blocks: %d\n", sf_blocks);
        printf("size of block: %d\n\n", sf_blocksize);
    }

   sf_pageToRAM(0);                  //Loading IP Address
   sf_readRAM(flash_buf, 0, 16);
   strcpy(ip_user,flash_buf);

    i=0;
   while(flash_buf[i]!='*')
       {
        ip_user[i]=flash_buf[i];
        i++;
      }

   sf_pageToRAM(1);              //Loading Netmask
   sf_readRAM(flash_buf, 0, 16);
   strcpy(msk,flash_buf);

   i=0;
   while(flash_buf[i]!='*')
       {
        msk[i]=flash_buf[i];
        i++;
      }

   sf_pageToRAM(2);
   sf_readRAM(flash_buf, 0, 16); //Loading Gateway
   strcpy(gtw,flash_buf);

   i=0;
   while(flash_buf[i]!='*')
       {
        gtw[i]=flash_buf[i];
        i++;
      }

   strcpy(gtw,"192.168.0.1*");

   printf("\nMy IP Address is : %s", ip_user);
   printf("\nMy Netmask is : %s", msk);
   printf("\nMy Gateway is : %s\n", gtw);
   //strcpy(ip_user,"192.168.2.39");
   //strcpy(msk,"255.255.255.0");
   //strcpy(gtw,"192.168.2.1");

   //Set internal port A 75K ohm pullup resistor

   WrPortI(PA0CR, NULL, 0x01);
   WrPortI(PA1CR, NULL, 0x01);
   WrPortI(PA2CR, NULL, 0x01);
   WrPortI(PA3CR, NULL, 0x01);
   WrPortI(PA4CR, NULL, 0x01);
   WrPortI(PA5CR, NULL, 0x01);
   WrPortI(PA6CR, NULL, 0x01);
   WrPortI(PA7CR, NULL, 0x01);

   //Assigned Port A as Input
   WrPortI(SPCR, &SPCRShadow, 0x080);


   //Port B assignment
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 2); //Assigned Pin B.2 as Output (Amplifier)
   BitWrPortI(PBDR, &PBDRShadow, 0, 2); //PB.2 init
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 3); //Assigned Pin B.3 as Output (Speaker)
   BitWrPortI(PBDR, &PBDRShadow, 0, 3); //PB.3 init
   BitWrPortI(PBDDR, &PBDDRShadow, 1, 7); //Assigned Pin B.7 as Output (Logik)
   BitWrPortI(PBDR, &PBDRShadow, 0, 7); //PB.7 init

   //Assigned Port D as Output
   BitWrPortI(PDDDR, &PDDDRShadow, 1, LED); //Assigned Pin D.0 as Output (LED)
   BitWrPortI(PDDR, &PDDRShadow, 1, LED); //Set PD0 = OFF

   //Assigned Port E as Output
   WrPortI(PEDDR, &PEDDRShadow, 0xFF);
   WrPortI(PEDR, &PEDRShadow, 0x00);  //First Init


   serFopen(_232BAUD);


   sock_init();

   ifconfig(IF_ETH0,
    IFS_DOWN,
    //IFS_DHCP,0,
    IFS_IPADDR, aton(ip_user),// setup ip address
    IFS_NETMASK,aton(msk),    // setup netmask
    IFS_ROUTER_SET, aton(gtw),// default router
    IFS_UP,
    IFS_END);

   while(1)
   {


//TCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCPIPTCP
//PASSIVECONNECTION(SERVER)PASSIVECONNECTION(SERVER)PASSIVECONNECTION(SERVER)PAS


//------------------------------------------------------------------------------
costate
    {
          if (sockslotflag[0]==0)
             {
               waitfor(DelaySec(1));
                    tcp_tick(NULL);
                    remoteip[0]=resolve(REMOTE_IP);
                    tcp_open(&sockslot[0],0,remoteip[0],PORT,NULL);
                    printf("Connecting to %s\n",REMOTE_IP);
                while(!sock_established(&sockslot[0]) && sock_bytesready(&sockslot[0])==-1)
                     {
                      tcp_tick(NULL);
                  fc++;
                  if (fc>=32750)
                      {
                         fc=0;
                        printf("Connection cannot be established....\n");
                        abort;
                     }
                     yield;
                     }
         sockslotflag[0]=1; //socket slot 0 is occupied
         printf("Socket Slot 0 is Connected...\n");
          sock_mode(&sockslot[0],TCP_MODE_ASCII);

          while(tcp_tick(&sockslot[0]))
         yield;

//------------------------------------------------------------------------------
            status=sock_alive(&sockslot[0]);
         if (status==0)
            {
               printf("User menutup koneksi socket slot 0... \n");
               waitfor(DelaySec(4));
               printf("Koneksi socket slot 0 tertutup... \n");
               sockslotflag[0]=0; //socket slot 0 is released
            }
            else
            {
                printf("Koneksi socket 0 tidak tertutup, koneksi hardware terputus...\n");
            }
 //-----------------------------------------------------------------------------
             }


    }

//000000000000000000000000000000000000000000000000000000000000000000000000000000

costate
    {


        if (sockslotflag[0]==1)
      {

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"GET http://api.geeknesia.com/api/attribute/FromRabbit?api_key=api_key\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf," HTTP/1.1\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"Host: api.geeknesia.com\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"X-Auth-Token: My Token\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"Connection: close\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));
         //printf("%s",outbuf); //verbose only

         sock_flushnext(&sockslot[0]);
         strcpy(outbuf,"\n\r\0");
         sock_awrite(&sockslot[0],outbuf,strlen(outbuf));

         //printf("%s",outbuf); //verbose only


         waitfor(DelayMs(500));

      }

   }


////////////////////////////////////////////////////////////////////////////////
costate
    {

  if ((sock_bytesready(&sockslot[0])>=0)  && (sockslotflag[0]==1))
             {
             sock_gets(&sockslot[0],buffer,sizeof(buffer));
            printf("%s\n",buffer);

            if (buffer[37] == '0')
            {
              BitWrPortI(PDDR, &PDDRShadow, 0, LED);
            }

            else if (buffer[37] == '1')
            {
             BitWrPortI(PDDR, &PDDRShadow, 1, LED);
            }

               }
  }

//------------------------------------------------------------------------------

//EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)EOFWHILE(1)
   }

serFclose();
////////////////////////////////////////////////////////////////////////////////

}


Info lebih lanjut dapat menghubungi : info@rantonic.com


Leave a Comment:

Please Sign in First