diff --git a/src/debug/debug.h b/src/debug/debug.h index 24544fb..2aeab83 100644 --- a/src/debug/debug.h +++ b/src/debug/debug.h @@ -3,7 +3,7 @@ #define DEBUG_H #define IF_SEVERITY 0 -#include "debug_if.h" +#include "./debug_if.h" inline UART_Handle uartHandle; // UART_control(uartHandle, UART_CMD_RXDISABLE, NULL); diff --git a/src/gsensor.c b/src/gsensor.c index 95c909b..86b4b50 100644 --- a/src/gsensor.c +++ b/src/gsensor.c @@ -21,7 +21,7 @@ typedef struct { int8_t g; } pakketje; -void * GSensorThread(void *args) { +void * GSensorTask(void *args) { I2C_Handle speedyBoy; @@ -32,7 +32,7 @@ void * GSensorThread(void *args) { uint8_t buffer_send = ACCD_X_MSB; double timer = 0; - int sleeper; + uint32_t sleeper; I2C_Transaction RW_Trans; RW_Trans.slaveAddress = SPEEDYBOI_ADDR; @@ -48,18 +48,18 @@ void * GSensorThread(void *args) { } // publish read into mailbox (defined in mqtt.c) timer += 0.1; + + pakketje pakket; + pakket.richting = (buffer_rcv < 0) ? Achteruit : Vooruit; + pakket.timestamp = timer; + pakket.g = abs(buffer_rcv); + + // MQTTPublish(pakket); + sleeper = 6000000; - - pakketje pakket; - pakket.richting = (buffer_rcv < 0) ? Achteruit : Vooruit; - pakket.timestamp = timer; - pakket.g = abs(buffer_rcv); - - // MQTTPublish(pakket); - - while(sleeper) { - sleeper--; - } + while(sleeper) { + sleeper--; + } } return 0; diff --git a/src/header.h b/src/header.h index 4478da7..d73ea59 100644 --- a/src/header.h +++ b/src/header.h @@ -1,4 +1,4 @@ -void * GSensorThread(void *args); +void * GSensorTask(void *args); void * MQTTTask(void *args); void * SocketTask(void *args); diff --git a/src/main.c b/src/main.c index dda3ee5..480720f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,19 +6,27 @@ #include #include -#include "./header.h" +#include "header.h" +//#include "debug/debug.h" +//#include "network/network.h" #define BIGOLTHREADSTACK 4096 void Hardware_init(){ +// int32_t ret; + Board_init(); GPIO_init(); I2C_init(); + +// ret = WifiInit(); +// if(ret < 0){ +// while(1); +// } } int main(void){ - pthread_t thread; pthread_attr_t attrs; struct sched_param priParam; int retc; @@ -34,19 +42,33 @@ int main(void){ retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); retc |= pthread_attr_setstacksize(&attrs, BIGOLTHREADSTACK); if (retc != 0) { - /* failed to set attributes */ +// LOG_CRITICAL("main(): failed to set tread attributes"); while(1); } - /* ============================================== === tread for G sensor ======================= ============================================== */ - retc = pthread_create(&thread, &attrs, GSensorThread, NULL); + pthread_t GSensorThread; + retc = pthread_create(&GSensorThread, &attrs, GSensorTask, NULL); if (retc != 0) { /* pthread_create() failed */ +// LOG_CRITICAL("main(): failed to create tread for G-sensor"); + while(1); + } + + + /* ============================================== + === tread for MQTT =========================== + ============================================== + */ + pthread_t MQTTThread; + retc = pthread_create(&MQTTThread, &attrs, MQTTTask, NULL); + if (retc != 0) { + /* pthread_create() failed */ +// LOG_CRITICAL("main(): failed to create tread for MQTT"); while(1); } diff --git a/src/mqtt.c b/src/mqtt.c index 2dfa33f..b6cb55f 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -1,6 +1,7 @@ #include "mqtt/mqtt.h" void MQTTTask(void* args){ + LOG_TRACE("MQTT task started."); mq_attr attr; MQTTClient_Handle mqttClientHandle; @@ -19,10 +20,10 @@ void MQTTTask(void* args){ while(1); } - ret = WifiInit(); - if(ret < 0){ - while(1); - } +// ret = WifiInit(); +// if(ret < 0){ +// while(1); +// } ret = MQTT_IF_Init(mqttInitParams); if(ret < 0){ @@ -41,8 +42,7 @@ void MQTTTask(void* args){ // ret |= MQTT_IF_Subscribe(mqttClientHandle, "cc32xx/ToggleLED3", MQTT_QOS_2, ToggleLED3CB); if(ret < 0){ while(1); - } - else{ + }else{ LOG_INFO("Subscribed to all topics successfully\r\n"); } @@ -57,6 +57,7 @@ void MQTTTask(void* args){ struct msgQueue queueElement; while(1){ + LOG_TRACE("Running MQTT loop."); mq_receive(appQueue, (char*)&queueElement, sizeof(struct msgQueue), NULL); if(queueElement.event == APP_MQTT_PUBLISH){ @@ -67,9 +68,6 @@ void MQTTTask(void* args){ "LED 1 toggle\r\n", strlen("LED 1 toggle\r\n"), MQTT_QOS_2); - - // GPIO_clearInt(CONFIG_GPIO_BUTTON_0); - // GPIO_enableInt(CONFIG_GPIO_BUTTON_0); } else if(queueElement.event == APP_MQTT_CON_TOGGLE){ @@ -94,17 +92,17 @@ void MQTTTask(void* args){ struct msgQueue queueElement; - ret = detectLongPress(); - if(ret == 0){ - - LOG_TRACE("APP_BTN_HANDLER SHORT PRESS\r\n"); - queueElement.event = APP_MQTT_CON_TOGGLE; - } - else{ - - LOG_TRACE("APP_BTN_HANDLER LONG PRESS\r\n"); - queueElement.event = APP_MQTT_DEINIT; - } +// ret = detectLongPress(); +// if(ret == 0){ +// +//// LOG_TRACE("APP_BTN_HANDLER SHORT PRESS\r\n"); +// queueElement.event = APP_MQTT_CON_TOGGLE; +// } +// else{ +// +//// LOG_TRACE("APP_BTN_HANDLER LONG PRESS\r\n"); +// queueElement.event = APP_MQTT_DEINIT; +// } ret = mq_send(appQueue, (const char*)&queueElement, sizeof(struct msgQueue), 0); if(ret < 0){ @@ -119,7 +117,7 @@ void MQTTTask(void* args){ } MQTT_IF_Deinit(); - // LOG_INFO("looping the MQTT functionality of the example for demonstration purposes only\r\n"); + LOG_INFO("looping the MQTT functionality of the example for demonstration purposes only\r\n"); // sleep(2); // goto MQTT_DEMO; } diff --git a/src/mqtt/mqtt.h b/src/mqtt/mqtt.h index d78d4ed..2ebe039 100644 --- a/src/mqtt/mqtt.h +++ b/src/mqtt/mqtt.h @@ -1,4 +1,6 @@ #include "mqtt_if.h" +#include "../debug/debug.h" +#include "../network/network.h" #include #include @@ -33,13 +35,6 @@ mqd_t appQueue; int connected; int deinit; -//Timer_Handle timer0; -int longPress = 0; - -/* Client ID */ -/* If ClientId isn't set, the MAC address of the device will be copied into */ -/* the ClientID parameter. */ -char ClientId[13] = {'\0'}; enum{ APP_MQTT_PUBLISH, diff --git a/src/network/network.h b/src/network/network.h new file mode 100644 index 0000000..03fe1e5 --- /dev/null +++ b/src/network/network.h @@ -0,0 +1,126 @@ + + +#ifndef __NETWORK__H__ +#define __NETWORK__H__ + +#include + +#include "network_if.h" + +extern char ClientId[13] = {'\0'}; + +int32_t SetClientIdNamefromMacAddress() +{ + int32_t ret = 0; + uint8_t Client_Mac_Name[2]; + uint8_t Index; + uint16_t macAddressLen = SL_MAC_ADDR_LEN; + uint8_t macAddress[SL_MAC_ADDR_LEN]; + + /*Get the device Mac address */ + ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen, + &macAddress[0]); + + /*When ClientID isn't set, use the mac address as ClientID */ + if(ClientId[0] == '\0') + { + /*6 bytes is the length of the mac address */ + for(Index = 0; Index < SL_MAC_ADDR_LEN; Index++) + { + /*Each mac address byte contains two hexadecimal characters */ + /*Copy the 4 MSB - the most significant character */ + Client_Mac_Name[0] = (macAddress[Index] >> 4) & 0xf; + /*Copy the 4 LSB - the least significant character */ + Client_Mac_Name[1] = macAddress[Index] & 0xf; + + if(Client_Mac_Name[0] > 9) + { + /*Converts and copies from number that is greater than 9 in */ + /*hexadecimal representation (a to f) into ascii character */ + ClientId[2 * Index] = Client_Mac_Name[0] + 'a' - 10; + } + else + { + /*Converts and copies from number 0 - 9 in hexadecimal */ + /*representation into ascii character */ + ClientId[2 * Index] = Client_Mac_Name[0] + '0'; + } + if(Client_Mac_Name[1] > 9) + { + /*Converts and copies from number that is greater than 9 in */ + /*hexadecimal representation (a to f) into ascii character */ + ClientId[2 * Index + 1] = Client_Mac_Name[1] + 'a' - 10; + } + else + { + /*Converts and copies from number 0 - 9 in hexadecimal */ + /*representation into ascii character */ + ClientId[2 * Index + 1] = Client_Mac_Name[1] + '0'; + } + } + } + return(ret); +} + +int WifiInit(){ + + int32_t ret; + SlWlanSecParams_t security_params; + pthread_t spawn_thread = (pthread_t) NULL; + pthread_attr_t pattrs_spawn; + struct sched_param pri_param; + + pthread_attr_init(&pattrs_spawn); + pri_param.sched_priority = 9; + ret = pthread_attr_setschedparam(&pattrs_spawn, &pri_param); + ret |= pthread_attr_setstacksize(&pattrs_spawn, 2048); + ret |= pthread_attr_setdetachstate(&pattrs_spawn, PTHREAD_CREATE_DETACHED); + ret = pthread_create(&spawn_thread, &pattrs_spawn, sl_Task, NULL); + if(ret != 0){ +// LOG_ERROR("could not create simplelink task\n\r"); + while(1); + } + + Network_IF_ResetMCUStateMachine(); + + Network_IF_DeInitDriver(); + + ret = Network_IF_InitDriver(ROLE_STA); + if(ret < 0){ +// LOG_ERROR("Failed to start SimpleLink Device\n\r"); + while(1); + } + +// DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION); + + SetClientIdNamefromMacAddress(); + + security_params.Key = (signed char*)SECURITY_KEY; + security_params.KeyLen = strlen(SECURITY_KEY); + security_params.Type = SECURITY_TYPE; + + ret = Network_IF_ConnectAP(SSID_NAME, security_params); + if(ret < 0){ +// LOG_ERROR("Connection to an AP failed\n\r"); + } + else{ + + SlWlanSecParams_t securityParams; + + securityParams.Type = SECURITY_TYPE; + securityParams.Key = (signed char*)SECURITY_KEY; + securityParams.KeyLen = strlen((const char *)securityParams.Key); + + ret = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &securityParams, NULL, 7, 0); + if(ret < 0){ +// LOG_ERROR("failed to add profile %s\r\n", SSID_NAME); + } + else{ +// LOG_INFO("profile added %s\r\n", SSID_NAME); + } + } + + return ret; +} + +#endif diff --git a/src/network/network_if.c b/src/network/network_if.c index 1eb7c21..33db3c8 100644 --- a/src/network/network_if.c +++ b/src/network/network_if.c @@ -535,7 +535,7 @@ long Network_IF_DeInitDriver(void) { long lRetVal = -1; - UART_PRINT("SL Disconnect...\n\r"); +// UART_PRINT("SL Disconnect...\n\r"); /* Disconnect from the AP */ lRetVal = Network_IF_DisconnectFromAP();