This program simply connects to a HTS221 I2C device to read Temperature & Humidity, and a WNC Cellular Module both of which are on the Avnet WNC Shield.

Dependencies:   mbed FXOS8700CQ

/media/uploads/JMF/avnet_logo.gif

AT&T Shape Hackathon QuickStart Instructions

  • One area that has been problematic is setting the the MY_SERVER_URL. When you copy the URL from the flow, you must make sure that the MY_SERVER_URL is also set to the appropirate server. It can be either "run-east.att.io" or "run-west.att.io", so be sure to check this.

Useful Links

Adding Additional Sensors

The FLOW_DEVICE_NAME field must contain the name of the instance of the Virtual Starter kit in FLOW that you will be communicating with. Usually this will be "vstarterkit001", but if you have problems communicating you can verify that this is indeed correct. Note that this device will not be created until you click the “Initialize” input on the Virtual Device tab of the Starter Kit project in FLOW. At that point it becomes available in M2X and you can see it there, as the DEVICE SERIAL field under Devices as in the image below.

Sensors: When executing, the FRDM-K64F board will upload sensor measurements to AT&T’s Flow environment every 5 seconds, using the Cellular shield board. You can adjust how often you want to do this by editing the SENSOR_UPDATE_INTERVAL_MS value in the header file. Temperature and humidity: By default the board will report readings from the HTS221 temperature and humidity sensor. These two values are sent to the HTTP IN /climate port in FLOW with field names “temp” and “humidity”. Temperature is in degrees Fahrenheit and humidity is a %. This default assignment is: iSensorsToReport = TEMP_HUMIDITY_ONLY;

Accelerometer: If you want to expand and use the on-board motion sensor, you can also send 3-axis accelerometer information from the board as “accelX”, “accelY” and “accelZ”. This is useful if you want to know the stationary position of the board with regards to gravity, or whether it is in motion. These readings are in g’s. To send these values, change the assignment to: iSensorsToReport = TEMP_HUMIDITY_ACCELEROMETER;

PMOD Sensors: If you have a Silicon Labs sensor module that can plug into the PMOD connector on the Cellular shield, you will also be able to measure proximity, UV light, ambient visible and infrared light from the Si1145 sensor. This PMOD also has a temperature and humidity sensor, but in this case it is redundant. When enabled, the fields “proximity”, “light_uv”, “light_vis” and “light_ir” will also be sent. To enable all these sensors, change the assignment to: iSensorsToReport = TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS;

Connecting up the PMOD sensors: Because the pinouts do not align, the SiLabs PMOD sensor board cannot be plugged into the J10 PMOD receptacle on the shield directly. The following wiring instructions must be used:

Signal=J10=(Shield) PMOD=Color in the image below
VCCPin 6Pin 6Red
GNDPin 5Pin 5Black
SDAPin4Pin 3Green
SCLPin3Pin 2Yellow

Link to AT&T M2X

M2X

Link to AT&T Flow

FLOW

Avnet WNC-Shield Information

Getting Started with the Avnet WNC-Shield & Hackathon software

  • This project uses Revision 119 of the MBED library because of I2C implementation differences with the tip (Revision 121)
  • This project uses Revision 4 of the FXOS8700CQ library for sensors

Easily modifiable parameters in source code

Inside the mbed “AvnetATT_shape_hackathon” project, the parameters that are needed to customize your board are in the config_me.h file.

  • FLOW parameters: This project assumes that you are using a fork of the “Starter Kit Base” project, which is a reference design that was created using AT&T’s FLOW (https://flow.att.com) that allows the creation of on-line virtualization and other IoT functionality. The default parameters in the config_me.h file are done for a specific instance of this project. When you fork the original project, you get your own instance and it will have its own base address. At the bottom of the FLOW environment, when you click on the “Endpoints” tab, you will see the URL information that is specific to your instance. Of note is the Base URL. In the example below (as in the default mbed project), the Base URL is: https://run-west.att.io/1e464b19cdcde/774c88d68202/86694923d5bf28a/in/flow You have to take note of two parts of this address. The run-west.att.io part is the server URL, and you have to make sure the
  • MY_SERVER_URL field in config_me.h matches this. Then there is the rest of the base URL, in green above, that needs to be pasted into the FLOW_BASE_URL field.

There is also a FLOW_INPUT_NAME field. This should match the name of the HTTP IN port in the FLOW project that you want to send sensor data to. The default is "/climate", as in the FLOW image below.

/media/uploads/JMF/sf.png

Where is the binary I compiled

When the COMPILE button is pressed, it will compile your project and link it. The result is placed in the DOWNLOAD folder you use when downloading files from the internet. It will be called AvnetATT_shape_hackathon_K64F.bin.

Additional information on compiling/configuring

Comprehensive instructions can be found at: Quick Start Instructions

wnc_control.cpp

Committer:
stefanrousseau
Date:
2016-07-23
Revision:
34:029e07b67a41
Parent:
33:eaf45dab650a

File content as of revision 34:029e07b67a41:

#include "mbed.h"
#include <cctype>
#include <string>
#include "config_me.h"
#include "SerialBuffered.h"
#include "wnc_control.h"

extern Serial pc;
extern Serial mdm;
extern string MyServerIpAddress;
extern string MySocketData;

int reinitialize_mdm(void);

enum WNC_ERR_e {
    WNC_OK =0,
    WNC_CMD_ERR = -1,
    WNC_NO_RESPONSE = -2
};

// Contains result of last call to send_wnc_cmd(..)
WNC_ERR_e WNC_MDM_ERR = WNC_OK;

// Contains the RAW WNC UART responses
static string wncStr;
static int socketOpen = 0;

void software_init_mdm(void)
{
  do
  {
    WNC_MDM_ERR = WNC_OK;
    at_init_wnc();
    if (WNC_MDM_ERR != WNC_OK)
      reinitialize_mdm();
  } while (WNC_MDM_ERR != WNC_OK); 
}

void resolve_mdm(void)
{
    do
    {
      WNC_MDM_ERR = WNC_OK;
      at_dnsresolve_wnc(MY_SERVER_URL, &MyServerIpAddress);
      if (WNC_MDM_ERR == WNC_NO_RESPONSE)
      {
        reinitialize_mdm();
        software_init_mdm();
      }
      else if (WNC_MDM_ERR == WNC_CMD_ERR)
      {
        pc.puts("Bad URL!!!!!!\r\n");
        MyServerIpAddress = "192.168.0.1";
        WNC_MDM_ERR = WNC_OK;
      }
    } while (WNC_MDM_ERR != WNC_OK);
    
    pc.printf("My Server IP: %s\r\n", MyServerIpAddress.c_str());
}

void sockopen_mdm(void)
{
    do
    {
      at_at_wnc();
      at_at_wnc();
      WNC_MDM_ERR = WNC_OK;
      at_sockopen_wnc(MyServerIpAddress, MY_PORT_STR);
      if (WNC_MDM_ERR == WNC_NO_RESPONSE)
      {
        reinitialize_mdm();
        software_init_mdm();
      }
      else if (WNC_MDM_ERR == WNC_CMD_ERR)
        pc.puts("Socket open fail!!!!\r\n");
      else
        socketOpen = 1;
    } while (WNC_MDM_ERR != WNC_OK);
}

void sockwrite_mdm(const char * s)
{
    if (socketOpen == 1)
    {
    do
    {
      WNC_MDM_ERR = WNC_OK;
      at_sockwrite_wnc(s);
      if (WNC_MDM_ERR == WNC_NO_RESPONSE)
      {
        reinitialize_mdm();
        software_init_mdm();
      }
      else if (WNC_MDM_ERR == WNC_CMD_ERR)
      {
        pc.puts("Socket Write fail!!!\r\n");
        // Have seen when write fails modem gets stuck in bad state, try to recover
        reinitialize_mdm();
        software_init_mdm();
      }
    } while (WNC_MDM_ERR != WNC_OK);
    }
    else
      puts("Socket is closed for write!\r\n");
}

unsigned sockread_mdm(string * sockData, int len, int retries)
{
    // Clear out any possible old data
    sockData->erase();
    if (socketOpen == 1)
    {
    do
    {
      WNC_MDM_ERR = WNC_OK;
      at_sockread_wnc(sockData, len, retries);
      if (WNC_MDM_ERR == WNC_NO_RESPONSE)
      {
        reinitialize_mdm();
        software_init_mdm();
      }
      else if (WNC_MDM_ERR == WNC_CMD_ERR)
        puts("Sock read fail!!!!\r\n");
    } while (WNC_MDM_ERR != WNC_OK);
    }
    else
      puts("Socket is closed for read\r\n");
      
    return (sockData->size());
}

void sockclose_mdm(void)
{
    do
    {
      WNC_MDM_ERR = WNC_OK;
      at_sockclose_wnc();
      // Assume close happened even if it went bad
      // going bad will result in a re-init anyways and if close
      // fails we're pretty much in bad state and not much can do
      socketOpen = 0;
      if (WNC_MDM_ERR == WNC_NO_RESPONSE)
      {
        reinitialize_mdm();
        software_init_mdm();
      }
      else if (WNC_MDM_ERR == WNC_CMD_ERR)
        puts("Sock close fail!!!\r\n");
    } while (WNC_MDM_ERR != WNC_OK);
}

/**                                                                                                                                                          
 * C++ version 0.4 char* style "itoa":                                                                                                                       
 * Written by Lukás Chmela                                                                                                                                   
 * Released under GPLv3.                                                                                                                                     
*/
 
char* itoa(int value, char* result, int base)                                                                                                          
{                                                                                                                                                        
    // check that the base if valid                                                                                                                      
    if ( base < 2 || base > 36 ) {                                                                                                                       
        *result = '\0';                                                                                                                                  
        return result;                                                                                                                                   
    }                                                                                                                                                    
 
    char* ptr = result, *ptr1 = result, tmp_char;                                                                                                        
    int tmp_value;                                                                                                                                       
 
    do {                                                                                                                                                 
        tmp_value = value;                                                                                                                               
        value /= base;                                                                                                                                   
        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];                             
    } while ( value );                                                                                                                                   
 
    // Apply negative sign                                                                                                                               
    if ( tmp_value < 0 )                                                                                                                                 
    *ptr++ = '-';                                                                                                                                    
    *ptr-- = '\0';                                                                                                                                       
 
    while ( ptr1 < ptr ) {                                                                                                                               
    tmp_char = *ptr;                                                                                                                                 
    *ptr-- = *ptr1;                                                                                                                                  
    *ptr1++ = tmp_char;                                                                                                                              
    }                                                                                                                                                    
 
    return result;                                                                                                                                       
}

extern int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len);

// Sets a global with failure or success, assumes 1 thread all the time
int send_wnc_cmd(const char * s, string ** r, int ms_timeout)
{
  static const char * rsp_lst[] = { "OK", "ERROR", NULL };
  int len;
  
  pc.printf("Send: %s\r\n",s);
  int res = mdm_sendAtCmdRsp(s, rsp_lst, ms_timeout, &wncStr, &len);
  *r = &wncStr;   // Return a pointer to the static string
      
  if (res >= 0)
  {
      pc.puts("[");
      pc.puts(wncStr.c_str());
      pc.puts("]\n\r");
      if (res > 0)
      {
          if (WNC_MDM_ERR != WNC_NO_RESPONSE)
            WNC_MDM_ERR = WNC_CMD_ERR;
          return -1;
      }
      else
          return 0;
  }
  else
  {
      WNC_MDM_ERR = WNC_NO_RESPONSE;
      pc.puts("No response from WNC!\n\r");
      return -2;
  }
}

void at_at_wnc(void)
{
    string * pRespStr;
    send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS); // Heartbeat?
}

void at_init_wnc(void)
{
  string * pRespStr;
  send_wnc_cmd("AT", &pRespStr, WNC_TIMEOUT_MS);             // Heartbeat?
  send_wnc_cmd("ATE1", &pRespStr, WNC_TIMEOUT_MS);           // Echo ON
  string cmd_str("AT%PDNSET=1,");
  cmd_str += MY_APN_STR;
  cmd_str += ",IP";
  send_wnc_cmd(cmd_str.c_str(), &pRespStr, 2*WNC_TIMEOUT_MS); // Set APN, cmd seems to take a little longer sometimes
  send_wnc_cmd("AT@INTERNET=1", &pRespStr, WNC_TIMEOUT_MS);  // Internet services enabled
  send_wnc_cmd("AT@SOCKDIAL=1", &pRespStr, WNC_TIMEOUT_MS);
}

void at_sockopen_wnc(const string & ipStr, const char * port )
{
  string * pRespStr;
  send_wnc_cmd("AT@SOCKCREAT=1", &pRespStr, WNC_TIMEOUT_MS);
  string cmd_str("AT@SOCKCONN=1,\"");
  cmd_str += ipStr;
  cmd_str += "\",";
  cmd_str += port;
  send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
}

void at_sockclose_wnc(void)
{
  string * pRespStr;
  send_wnc_cmd("AT@SOCKCLOSE=1", &pRespStr, WNC_TIMEOUT_MS);
}

int at_dnsresolve_wnc(const char * s, string * ipStr)
{
  string * pRespStr;
  string str(s);
  str = "AT@DNSRESVDON=\"" + str;
  str += "\"\r\n";
  if (send_wnc_cmd(str.c_str(), &pRespStr, WNC_TIMEOUT_MS) == 0)
  {
    size_t pos_start = pRespStr->find(":\"") + 2;
    if (pos_start !=  string::npos)
    {
      size_t pos_end = pRespStr->rfind("\"") - 1;
      if (pos_end != string::npos)
      {
        if (pos_end > pos_start)
        {
          // Make a copy for use later (the source string is re-used)
          *ipStr = pRespStr->substr(pos_start, pos_end - pos_start + 1);
          return 1;
        }
        else
          pc.puts("URL Resolve fail, substr Err\r\n");
      }
      else
        pc.puts("URL Resolve fail, no 2nd quote\r\n");
    }
    else
      pc.puts("URL Resolve fail, no quotes\r\n");
  }
  else
    pc.puts("URL Resolve fail, WNC cmd fail\r\n");
  
  return -1;
}

void at_sockwrite_wnc(const char * s)
{
  string * pRespStr;
  char num2str[6];
  size_t sLen = strlen(s);
  if (sLen <= 99999)
  {
    string cmd_str("AT@SOCKWRITE=1,");
    itoa(sLen, num2str, 10);
    cmd_str += num2str;
    cmd_str += ",\"";
    while(*s != '\0')
    {
      itoa((int)*s++, num2str, 16);
      // Always 2-digit ascii hex:
      if (strlen(num2str) == 1)
      {
        num2str[2] = '\0';
        num2str[1] = num2str[0];
        num2str[0] = '0';
      }
      cmd_str += num2str;
    }
    cmd_str += "\"";
    send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
  }
  else
    pc.puts("sockwrite Err, string to long\r\n");
}

unsigned at_sockread_wnc(string * pS, unsigned n, unsigned retries = 0)
{
  unsigned i;
  string * pRespStr;
  string cmd_str("AT@SOCKREAD=1,");
  
  // Don't assume clean slate:
  pS->erase();
  
  if (n <= 1500)
  {
    char num2str[6];
    
    itoa(n, num2str, 10);
    cmd_str += num2str;
    retries += 1;
    while (retries--)
    {
      // Assuming someone is sending then calling this to receive response, invoke
      // a pause to give the response some time to come back and then also
      // between each retry.
      wait_ms(10);
      
      send_wnc_cmd(cmd_str.c_str(), &pRespStr, WNC_TIMEOUT_MS);
      size_t pos_start = pRespStr->find("\"")  + 1;
      size_t pos_end   = pRespStr->rfind("\"") - 1;
      i = pos_end - pos_start + 1;
      if (i > 0)
      {
        retries = 0;  // If any data found stop retrying
        string byte;
        while (pos_start < pos_end)
        {
          byte = pRespStr->substr(pos_start, 2);
          *pS += (char)strtol(byte.c_str(), NULL, 16);
          pos_start += 2;
        }
        return i;
      }
    }
  }
  else
    pc.puts("sockread Err, to many to read\r\n");
  
  return 0;
}