Wednesday, November 29, 2017

Using Contiki UDP client to send ON/OFF command to remote UDP server to toggle LED on CC13xx/CC26xx.

The following steps show you how to revise Contiki UDP client/server examples to allow you send UART ON/OFF command to UDP client, UDP client sends ON/OFF command over the air to UDP server, and UDP server toggles red LED. The following tests are done using two LAUNCHXL-CC1310.

1. Revise the following red lines in udp-client.c
...
#include "net/ip/uip-udp-packet.h"
#include "sys/ctimer.h"
#include "dev/leds.h"
#ifdef WITH_COMPOWER
#include "powertrace.h"
#endif
#include
#include

...
static struct uip_udp_conn *client_conn;
static uip_ipaddr_t server_ipaddr;

char cmd[16];

/*---------------------------------------------------------------------------*/
PROCESS(udp_client_process, "UDP client process");
AUTOSTART_PROCESSES(&udp_client_process);
/*---------------------------------------------------------------------------*/
static int seq_id;
static int reply;
...

/*---------------------------------------------------------------------------*/
static void
send_packet(void *ptr)
{
  char buf[MAX_PAYLOAD_LEN];

#ifdef SERVER_REPLY
  uint8_t num_used = 0;
  uip_ds6_nbr_t *nbr;

  nbr = nbr_table_head(ds6_neighbors);
  while(nbr != NULL) {
    nbr = nbr_table_next(ds6_neighbors, nbr);
    num_used++;
  }

  if(seq_id > 0) {
    ANNOTATE("#A r=%d/%d,color=%s,n=%d %d\n", reply, seq_id,
             reply == seq_id ? "GREEN" : "RED", uip_ds6_route_num_routes(), num_used);
  }
#endif /* SERVER_REPLY */

  seq_id++;
  PRINTF("CMD:%s send to %d SEQ_ID:%d\n",cmd,
         server_ipaddr.u8[sizeof(server_ipaddr.u8) - 1], seq_id);
  sprintf(buf, "%s from the client SEQ_ID:%d ",cmd, seq_id);
  uip_udp_packet_sendto(client_conn, buf, strlen(buf),
                        &server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
}
/*---------------------------------------------------------------------------*/
...

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udp_client_process, ev, data)
{
  static struct etimer periodic;
  static struct ctimer backoff_timer;
#if WITH_COMPOWER
  static int print = 0;
#endif

  PROCESS_BEGIN();

  PROCESS_PAUSE();

  cc26xx_uart_set_input(serial_line_input_byte);
 
  set_global_address();

  PRINTF("UDP client process started nbr:%d routes:%d\n",
         NBR_TABLE_CONF_MAX_NEIGHBORS, UIP_CONF_MAX_ROUTES);

  print_local_addresses();

  /* new connection with remote host */
  client_conn = udp_new(NULL, UIP_HTONS(UDP_SERVER_PORT), NULL);
  if(client_conn == NULL) {
    PRINTF("No UDP connection available, exiting the process!\n");
    PROCESS_EXIT();
  }
  udp_bind(client_conn, UIP_HTONS(UDP_CLIENT_PORT));

  PRINTF("Created a connection with the server ");
  PRINT6ADDR(&client_conn->ripaddr);
  PRINTF(" local/remote port %u/%u\n",
    UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));

#if WITH_COMPOWER
  powertrace_sniff(POWERTRACE_ON);
#endif

  etimer_set(&periodic, SEND_INTERVAL);
  while(1) {
    PROCESS_YIELD();
    if(ev == tcpip_event) {
      tcpip_handler();
    }

    if(ev == serial_line_event_message && data != NULL) {
      printf("command received:%s\n",(char *)data);
      
if(strcmp(data,"ON")==0){
         for(int i=0; i < 16 ; i=i+1) cmd[i]=0x0;
         cmd[0]=0x4F;
         cmd[1]=0x4E;
         leds_on(LEDS_RED);
      }else if(strcmp(data,"OFF")==0){
 
         for(int i=0; i < 16 ; i=i+1) cmd[i]=0x0;        
         cmd[0]=0x4F;
         cmd[1]=0x46;
         cmd[2]=0x46;
         leds_off(LEDS_RED);
      }
      send_packet(NULL); 

    }

    if(etimer_expired(&periodic)) {
      etimer_reset(&periodic);
      ctimer_set(&backoff_timer, SEND_TIME, send_packet, NULL);

#if WITH_COMPOWER
      if (print == 0) {
    powertrace_print("#P");
      }
      if (++print == 3) {
    print = 0;
      }
#endif

    }
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

2. Build udp-client.bin by "make TARGET=srf06-cc26xx BOARD=launchpad/cc1310 udp-client.bin" and download udp-client.bin to one LAUNCHXL-CC1310.


3. Revise the following red lines in udp-server.c
...
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
#include "dev/leds.h"

#define UIP_IP_BUF   ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
...
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{
  char *appdata;

  if(uip_newdata()) {
    appdata = (char *)uip_appdata;
    appdata[uip_datalen()] = 0;
    PRINTF("DATA recv '%s' from ", appdata);
    if(appdata[0]==0x4F && appdata[1]==0x4E ){
        leds_on(LEDS_RED);
    }
    if(appdata[0]==0x4F && appdata[1]==0x46 && appdata[2]==0x46){
        leds_off(LEDS_RED);
    }
   
    PRINTF("%d",
           UIP_IP_BUF->srcipaddr.u8[sizeof(UIP_IP_BUF->srcipaddr.u8) - 1]);
    PRINTF("\n");
#if SERVER_REPLY
    PRINTF("DATA sending reply\n");
    uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
    uip_udp_packet_send(server_conn, "Reply", sizeof("Reply"));
    uip_create_unspecified(&server_conn->ripaddr);
#endif
  }
}
/*---------------------------------------------------------------------------*/
...


4. Build udp-server.bin by "make TARGET=srf06-cc26xx BOARD=launchpad/cc1310 udp-server.bin" and download udp-server.bin to another LAUNCHXL-CC1310.

5. Start UDP server on one LAUNCHXL-CC1310 running udp-server.bin and start UDP client on another LAUNCHXL-CC1310 running udp-client.bin.

  

6. You can enter "ON" and "CTRL+Enter" to send ON command with end character "0x0A" to COM port of LAUNCHXL-CC1310 running UDP client and you will see red led is turned ON on another LAUNCHXL-CC1310 running UDP server.

7. You can enter "OFF" and "CTRL+Enter" to send OFF command with end character "0x0A" to COM port of LAUNCHXL-CC1310 running UDP client and you will see red led is turned OFF on another LAUNCHXL-CC1310 running UDP server.

No comments:

Post a Comment