/*****************************************************************************
*
*  checkWiFiConnected
*
*  \param  None
*
*  \return TRUE if connected, FALSE if not
*
*  \brief  Checks to see that WiFi is still connected
*
*****************************************************************************/
unsigned char
checkWiFiConnected(int reinit)
{
  unsigned char status = AtLibGs_IsNodeAssociated();

  if (!status)
  {
    if (reinit)
    {
      WIFI_init(1);
    }
    WIFI_Associate();
    status = AtLibGs_IsNodeAssociated();
  }
  RSSIReading();

  return status;
}
Example #2
0
void RSSIReading(void)
{
  int16_t rssi;
  char line[20];
  int rssiFound = 0;

  if (AtLibGs_IsNodeAssociated()) {
    if (AtLibGs_GetRssi() == ATLIBGS_MSG_ID_OK) {
      if (AtLibGs_ParseRssiResponse(&rssi)) {
        sprintf(line, "wl_rssi: %d", rssi);
        DisplayLCD(LCD_LINE5, (const uint8_t *)line);
        rssiFound = 1;
      }
    }
  }

  if (!rssiFound) {
    DisplayLCD(LCD_LINE5, "RSSI: ----");
  }
}
/*---------------------------------------------------------------------------*
 * Routine:  App_RSSIReading
 *---------------------------------------------------------------------------*
 * Description:
 *      Take a reading of the RSSI level with the WiFi and show it on
 *      the LCD display.
 * Inputs:
 *      bool updateLCD (if true, LCD is updated)
 * Outputs:
 *      int16_t rssi (return value)
 *---------------------------------------------------------------------------*/
int16_t App_RSSIReading(int16_t * rssi, bool updateLCD)
{
    char line[20];
    int rssiFound = 0;

    if (AtLibGs_IsNodeAssociated()) {
        if (AtLibGs_GetRssi() == ATLIBGS_MSG_ID_OK) {
            if (AtLibGs_ParseRssiResponse(rssi)) {
                sprintf(line, "RSSI: %d", (*rssi));
                DisplayLCD(LCD_LINE6, (const uint8_t *)line);
                rssiFound = 1;
            }
        }
    }
    if (!rssiFound) {
        DisplayLCD(LCD_LINE5, "RSSI: ----");
        rssi = 0;
    }
    return *rssi;
}
/*---------------------------------------------------------------------------*/
void App_TCPClientDemo(void)
{
    ATLIBGS_MSG_ID_E rxMsgId = ATLIBGS_MSG_ID_NONE;
    static char content[512];
    uint8_t cid = 0;
    static int16_t G_adc_int[2] = { 0, 0 };
    static char G_temp_int[2] = { 0, 0 };
    uint8_t remoteTcpSrvIp[20];
    
    bool connected = false; // when connected to TCP server this is true
    uint32_t time = MSTimerGet();
    ATLIBGS_TCPMessage msg;
    ATLIBGS_NetworkStatus networkStatus;
    
    AtLibGs_GetNetworkStatus(&networkStatus);
    
    AppTCPSetIPMenu(&networkStatus);

    sprintf((char*)remoteTcpSrvIp, "%d.%d.%d.%d",
            networkStatus.addr.ipv4[0],
            networkStatus.addr.ipv4[1],
            networkStatus.addr.ipv4[2],
            G_nvsettings.webprov.tcpIPClientHostIP);
    
    App_PrepareIncomingData();
    while (1) {
        if (!AtLibGs_IsNodeAssociated()) {
            App_Connect(&G_nvsettings.webprov);
            connected = false;
        } else if (!connected) {
            DisplayLCD(LCD_LINE7, "Connecting");
            // Start a TCP client
            rxMsgId = AtLibGs_TCPClientStart((char *)remoteTcpSrvIp,
                    TCP_DEMO_REMOTE_TCP_SRVR_PORT, &cid);
            if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                DisplayLCD(LCD_LINE7, "No Connect!");
                MSTimerDelay(2000);
                DisplayLCD(LCD_LINE7, "");
                continue;
            }
            if (cid == ATLIBGS_INVALID_CID) {
                DisplayLCD(LCD_LINE7, "No CID!");
                MSTimerDelay(2000);
                DisplayLCD(LCD_LINE7, "");
                continue;
            }
            DisplayLCD(LCD_LINE7, "");
            App_PrepareIncomingData();
            connected = true;
        } else {
            App_TemperatureReadingUpdate(G_temp_int, true);
            App_PotentiometerUpdate(G_adc_int, true);

            // Look to see if there is a message
            if ((G_receivedCount) || (AtLibGs_WaitForTCPMessage(250)
                    == ATLIBGS_MSG_ID_DATA_RX)) {
                // Got data!  Its sitting in G_received, but in a <CID> <data> format
                // We need to send it back
                AtLibGs_ParseTCPData(G_received, G_receivedCount, &msg);

                // Prepare for the next batch of incoming data
                App_PrepareIncomingData();

                // Copy the data out of the receive message (its sitting in G_recieved)
                memcpy(content, msg.message, msg.numBytes);

                // Now send this back over the TCP/IP connection
                rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content,
                        msg.numBytes);
                if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                    DisplayLCD(LCD_LINE7, " Send Fail!");
                    MSTimerDelay(2000);
                    DisplayLCD(LCD_LINE7, "");
                    continue;
                }
            } else if (MSTimerDelta(time) >= TCP_DEMO_UPDATE_INTERVAL) {
                time = MSTimerGet();
                // send temp and ADC to last received TCP client every X seconds

                DisplayLCD(LCD_LINE7, " Sending");
                sprintf(content, "Temp: %d.%d, Pot: %d.%d%%\r\n", G_temp_int[0],
                        G_temp_int[1], G_adc_int[0], G_adc_int[1]);
                // send data to server
                rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, strlen(
                        content));
                if (rxMsgId != ATLIBGS_MSG_ID_OK) {
                    DisplayLCD(LCD_LINE7, " Send Fail!");
                    MSTimerDelay(2000);
                    DisplayLCD(LCD_LINE7, "");
                    connected = false;
                    continue;
                } // end if
                DisplayLCD(LCD_LINE7, "");
            } // end else if
        } // end else
    } // end while
}
Example #5
0
void App_WhiskerGW()
{
    char pubTopic[100];
    char pubMsg[250];
    unsigned char msgType;
    int counter=0;
    char cntStr[20];
    char commandBuffer[64];
    int commandBufferPointer=0;
    
    led_all_off();
    // Give the unit a little time to start up
    // (300 ms for GS1011 and 1000 ms for GS1500)
    MSTimerDelay(1000);

    NVSettingsLoad(&GNV_Setting);
    led_on(4);
    WIFI_init(1);  // Show MAC address and Version
    led_on(5);
    WIFI_Associate();
    led_on(6);
    DisplayLCD(LCD_LINE8, "Demo starting.");
    DisplayLCD(LCD_LINE3, (const uint8_t *)WifiMAC);
    
    
    
    // UART
    if(spiUartInitialize()!=0)
    {
        DisplayLCD(LCD_LINE7, "!! SPIUART_ERROR !!");
        while(1)
        {
              led_all_on();
              MSTimerDelay(250);
              led_all_off();
              MSTimerDelay(250);
        }
    }
    
    while (1) 
    {
        // Do we need to connect to the AP?
        if (!AtLibGs_IsNodeAssociated())
        {
            led_off(6);
            WIFI_Associate();
            led_on(6);
        }
        else
        {    
            if(mqttConnected==0)
                App_ConnectMqtt();
            int charCount = spiUartRxBytesAvailable();
            while(charCount>0)
            {
                commandBuffer[commandBufferPointer++] = spiUartGetByte();
                charCount--;
                if(charCount==0)
                  commandBufferPointer=0;
                if(commandBufferPointer>4)
                {
                  if(strstr(commandBuffer,"RMPU")==commandBuffer)
                  {
                    if(commandBufferPointer>24)
                    {
                      // process response
                      char macStr[9];
                      char lenStr[5];
                      memcpy(macStr,&commandBuffer[6],8);
                      macStr[8] = 0;
                      memcpy(lenStr,&commandBuffer[4],2);
                      lenStr[2]=0;
                      int len = (int)strtol(lenStr,0,16);
                      unsigned long mac = strtoul(macStr,NULL,16);
                      WhiskerModule *wm = findModule(mac);
                      if(wm!=0)
                      {
                          if(commandBufferPointer>len+16)
                          {
                            char rssiStr[3];
                            memcpy(rssiStr,&commandBuffer[commandBufferPointer-3],2);
                            rssiStr[2]=0;
                            int rssi = (int)strtol(rssiStr,0,16);
                            if((rssi & 0x80) == 0x80)
                              rssi-=256;
                            int puMsgPointer=14;
                            mqtt_initJsonMsg(pubMsg);
                            mqtt_addStringValToMsg("Name",wm->Name,pubMsg,0);
                            mqtt_addStringValToMsg("Mac",macStr,pubMsg,1);
                            char rstr[8];
                            sprintf(rstr,"%d dbm",rssi);
                            mqtt_addStringValToMsg("Rssi",rstr,pubMsg,1);
                            sprintf(pubMsg+strlen(pubMsg),",\"Values\":{");
                            puMsgPointer=14;
                            int comma=0;
                            while(puMsgPointer < (commandBufferPointer-3))
                            {
                              char cidStr[3];
                              memcpy(cidStr,&commandBuffer[puMsgPointer],2);
                              puMsgPointer+=2;
                              cidStr[2]=0;
                              unsigned char cid=(unsigned char)strtol(cidStr,0,16);
                              unsigned char channel = cid & 0x1f;
                              char valStr[9];
                              long valInt;
                              switch(cid)
                              {
                              case 0x21:
                                //digital input
                                memcpy(valStr,&commandBuffer[puMsgPointer],2);
                                valStr[2]=0;
                                puMsgPointer+=2;
                                valInt = (int)strtol(valStr,0,16);
                                if(valInt)
                                    mqtt_addStringValToMsg("DIN1","True",pubMsg,comma);
                                else
                                    mqtt_addStringValToMsg("DIN1","False",pubMsg,comma);
                                break;
                              case 0x22:
                                //digital input
                                memcpy(valStr,&commandBuffer[puMsgPointer],2);
                                valStr[2]=0;
                                puMsgPointer+=2;
                                valInt = (int)strtol(valStr,0,16);
                                if(valInt)
                                    mqtt_addStringValToMsg("DIN2","True",pubMsg,comma);
                                else
                                    mqtt_addStringValToMsg("DIN2","False",pubMsg,comma);
                                break;                              
                              case 0x43:
                                //battery analog in
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("Battery",valInt,pubMsg,comma);
                                break;          
                              case 0x44:
                                //battery analog in
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("Temperature",valInt,pubMsg,comma);
                                break;          
                              case 0x45:
                                //battery analog in
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("RH",valInt,pubMsg,comma);
                                break;  
                              case 0x5d:
                                //internal temperature
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("IntTemp",valInt,pubMsg,comma);
                                break;
                              case 0x57:                                
                                //air quality
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("AirQual",valInt,pubMsg,comma);
                                break;
                              case 0x58:
                                //air quality
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("AirQual",valInt,pubMsg,comma);
                                break;
                                
                              case 0x61:
                                // digital counter input
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("Count1",valInt,pubMsg,comma);
                                break;
                                
                              case 0x62:
                                // digital counter input
                                memcpy(valStr,&commandBuffer[puMsgPointer],4);
                                valStr[4]=0;
                                puMsgPointer+=4;                                
                                valInt = (int)strtol(valStr,0,16);
                                mqtt_addIntValToMsg("Count2",valInt,pubMsg,comma);
                                break;
                              }                              
                              comma=1;
                            }                            
                            sprintf(pubMsg+strlen(pubMsg),"}");
                            mqtt_finishJsonMsg(pubMsg);
                            sprintf(pubTopic, "%s/%s", WIO_DOMAIN, macStr);
                            int res=mqtt_publish(&broker, pubTopic, pubMsg,0)<0;
                            if(res<0)
                              mqttConnected=0;
                            led_on(8);
                            //spiUartResetFIFO();
                          }
                      }
                    }
                  }
                }
            }
            // Send data if
            RSSIReading();
            AtLibGs_WaitForTCPMessage(1000);
            led_off(7);
            led_off(8);
            if(G_receivedCount>0)
            {
                AtLibGs_ParseTCPData(G_received,G_receivedCount,&rxm);
                msgType = MQTTParseMessageType(rxm.message);
                switch(msgType)
                {
                case MQTT_MSG_SUBACK:
                  // todo: display subscription acknowledgement
                  break;
                case MQTT_MSG_PUBLISH:
                  App_MQTTMsgPublished();
                  break;
                case MQTT_MSG_PUBACK:
                  // todo: display publish acknowledgement
                  break;
                default:
                  break;
                }
            }
            counter++;
            sprintf(cntStr,"Counter=%d",counter);
            DisplayLCD(LCD_LINE6,cntStr);
            if(counter>30)
            {
                counter=0;
                sprintf(pubTopic, "%s/%s", WIO_DOMAIN, WifiMAC);
                mqtt_initJsonMsg(pubMsg);
                mqtt_addStringValToMsg("msg","status ok",pubMsg,0);
                mqtt_finishJsonMsg(pubMsg);
                int res1=mqtt_publish(&broker, pubTopic, pubMsg,0)<0;
                if(res1<0)
                    mqttConnected=0;
                led_on(7);
            }
        }       
    // Send data if END
    }
}