/***************************************************************************** * * 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; }
void WIFI_IMPL_init(void) { memset(&wi, 0, sizeof(wi)); WIFI_init(wifi_impl_cfg_cb, wifi_impl_data_cb, wifi_impl_data_silence_cb); }
INT32 mtk_wcn_wmt_wifi_soc_init(VOID) { return WIFI_init(); }
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 } }
/***************************************************************************** * * App_Exosite * * \param None * * \return None * * \brief Takse a reading of temperature and potentiometer and sends to * Exosite cloud using a TCP connection * *****************************************************************************/ void App_Exosite(void) { int loop_time = 1000; unsigned char loopCount = 0; int wifi_init = 0; int badcik = 1; static const uint8_t geoCert[] = { 0x30, 0x82, 0x03, 0x54, 0x30, 0x82, 0x02, 0x3c, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x02, 0x34, 0x56, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x42, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0d, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x12, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x32, 0x30, 0x35, 0x32, 0x31, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x35, 0x32, 0x31, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x42, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0d, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x12, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0xcc, 0x18, 0x63, 0x30, 0xfd, 0xf4, 0x17, 0x23, 0x1a, 0x56, 0x7e, 0x5b, 0xdf, 0x3c, 0x6c, 0x38, 0xe4, 0x71, 0xb7, 0x78, 0x91, 0xd4, 0xbc, 0xa1, 0xd8, 0x4c, 0xf8, 0xa8, 0x43, 0xb6, 0x03, 0xe9, 0x4d, 0x21, 0x07, 0x08, 0x88, 0xda, 0x58, 0x2f, 0x66, 0x39, 0x29, 0xbd, 0x05, 0x78, 0x8b, 0x9d, 0x38, 0xe8, 0x05, 0xb7, 0x6a, 0x7e, 0x71, 0xa4, 0xe6, 0xc4, 0x60, 0xa6, 0xb0, 0xef, 0x80, 0xe4, 0x89, 0x28, 0x0f, 0x9e, 0x25, 0xd6, 0xed, 0x83, 0xf3, 0xad, 0xa6, 0x91, 0xc7, 0x98, 0xc9, 0x42, 0x18, 0x35, 0x14, 0x9d, 0xad, 0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0x0c, 0xc2, 0xb2, 0x99, 0x45, 0x00, 0xc7, 0xce, 0x11, 0x51, 0x22, 0x22, 0xe0, 0xa5, 0xea, 0xb6, 0x15, 0x48, 0x09, 0x64, 0xea, 0x5e, 0x4f, 0x74, 0xf7, 0x05, 0x3e, 0xc7, 0x8a, 0x52, 0x0c, 0xdb, 0x15, 0xb4, 0xbd, 0x6d, 0x9b, 0xe5, 0xc6, 0xb1, 0x54, 0x68, 0xa9, 0xe3, 0x69, 0x90, 0xb6, 0x9a, 0xa5, 0x0f, 0xb8, 0xb9, 0x3f, 0x20, 0x7d, 0xae, 0x4a, 0xb5, 0xb8, 0x9c, 0xe4, 0x1d, 0xb6, 0xab, 0xe6, 0x94, 0xa5, 0xc1, 0xc7, 0x83, 0xad, 0xdb, 0xf5, 0x27, 0x87, 0x0e, 0x04, 0x6c, 0xd5, 0xff, 0xdd, 0xa0, 0x5d, 0xed, 0x87, 0x52, 0xb7, 0x2b, 0x15, 0x02, 0xae, 0x39, 0xa6, 0x6a, 0x74, 0xe9, 0xda, 0xc4, 0xe7, 0xbc, 0x4d, 0x34, 0x1e, 0xa9, 0x5c, 0x4d, 0x33, 0x5f, 0x92, 0x09, 0x2f, 0x88, 0x66, 0x5d, 0x77, 0x97, 0xc7, 0x1d, 0x76, 0x13, 0xa9, 0xd5, 0xe5, 0xf1, 0x16, 0x09, 0x11, 0x35, 0xd5, 0xac, 0xdb, 0x24, 0x71, 0x70, 0x2c, 0x98, 0x56, 0x0b, 0xd9, 0x17, 0xb4, 0xd1, 0xe3, 0x51, 0x2b, 0x5e, 0x75, 0xe8, 0xd5, 0xd0, 0xdc, 0x4f, 0x34, 0xed, 0xc2, 0x05, 0x66, 0x80, 0xa1, 0xcb, 0xe6, 0x33}; bool isTimeSync = false; // wait 1 sec for LCD messages display MSTimerDelay(1000); NVSettingsLoad(&GNV_Setting); #ifdef SHOW_VERSION DisplayLCD(LCD_LINE2, (const uint8_t *)ExositeAppVersion); #endif // must initialize one time for mac address prepare.. WIFI_init(1); if (!Exosite_Init("renesas", "rl78g14", IF_WIFI, 0)) { show_status(); while(1); } while(AtLibGs_AddCert( EXOSITE_CA_NAME, true, geoCert, sizeof(geoCert)) != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE4, "Add CA FAILED"); } while (1) { if (!checkWiFiConnected(wifi_init)) { wifi_init = 0; } else { if(!isTimeSync) { if(Exosite_SyncTime() == 0) isTimeSync = true; } UpdateReadings(); int code = Exosite_StatusCode(); if (code == EXO_STATUS_OK) { badcik = 0; wifi_init = 1; ReadCloudCommands(); if (loopCount++ >= WRITE_INTERVAL) { // POST the Sensor and templature values ReportReadings(); loopCount = 0; } loop_time = 500; //delay 0.5 seconds before next turn.. } else if (1 == badcik || EXO_STATUS_BAD_CIK == code || EXO_STATUS_NOAUTH == code) { DisplayLCD(LCD_LINE6, " Exosite "); DisplayLCD(LCD_LINE7, " Connecting"); DisplayLCD(LCD_LINE8, " "); if (!Exosite_Activate()) { badcik = 1; loop_time = 3000; // delay 3 seconds } else { DisplayLCD(LCD_LINE7, " Connected "); } } show_status(); } MSTimerDelay(loop_time); //delay before looping again } }
int mtk_wcn_wmt_wifi_init(void) { return WIFI_init(); }