keyboard input, serial com test
Dependencies: mbed DISCO_L475VG_IOT01A_wifi TextLCD USBHost
Revision 6:9d975a9d2728, committed 2019-08-15
- Comitter:
- duchonic
- Date:
- Thu Aug 15 19:33:04 2019 +0000
- Parent:
- 5:0466445897b8
- Commit message:
- first
Changed in this revision
diff -r 0466445897b8 -r 9d975a9d2728 DISCO_L475VG_IOT01A_wifi.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DISCO_L475VG_IOT01A_wifi.lib Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/ST/code/DISCO_L475VG_IOT01A_wifi/#c61a93635433
diff -r 0466445897b8 -r 9d975a9d2728 inc/config.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/config.h Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,7 @@ +#pragma once +#include <string> + +void setPause(std::string p); +void setCycles(std::string c); +std::string getPause(void); +std::string getCycles(void); \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 inc/display.h --- a/inc/display.h Sat Aug 10 08:34:41 2019 +0000 +++ b/inc/display.h Thu Aug 15 19:33:04 2019 +0000 @@ -1,8 +1,11 @@ #pragma once - +#include "mbed.h" #include <string> void displayInit(void); void displayScreen(uint8_t nr); void displayKey(char key); -void printLine(std::string text, uint8_t line); \ No newline at end of file +void printLine(std::string text, uint8_t line); + +const uint8_t FIRST_LINE=0; +const uint8_t SECOND_LINE=1; \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 inc/massStorage.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/massStorage.h Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,3 @@ +#pragma once + +void massStorage_task(void const *); \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 inc/server.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/server.h Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,3 @@ +#pragma once + +void server_task(void const *); \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 main.cpp --- a/main.cpp Sat Aug 10 08:34:41 2019 +0000 +++ b/main.cpp Thu Aug 15 19:33:04 2019 +0000 @@ -1,26 +1,42 @@ #include "mbed.h" -#include "inc/display.h" -#include "inc/input.h" +#include "rtos.h" +#include "inc/server.h" +#include "inc/massStorage.h" +#include "inc/config.h" + +#include <string> DigitalOut led1(LED1); Serial pc(SERIAL_TX, SERIAL_RX); +const int TASK_PROGRAMM_DELAY_MS = 4000; +const int TASK_LED_DELAY_MS = 100; +void program_task(void const *){ + while(1){ + Thread::wait(TASK_PROGRAMM_DELAY_MS); + printf("prgramm running\r\n"); + } +} + +void led_task(void const *) { + while(1){ + led1 = !led1; + Thread::wait(TASK_LED_DELAY_MS); + } +} int main() -{ - displayInit(); - inputInit(); - +{ + pc.baud(115200); pc.printf("start main()\n\r"); + + Thread msdTask(massStorage_task, NULL, osPriorityNormal, 1024 * 4); + Thread ledTask(led_task, NULL, osPriorityNormal, 1024 * 4); + Thread programTask(program_task, NULL, osPriorityNormal, 1024 * 4); + Thread serverTask(server_task, NULL, osPriorityNormal, 1024 * 4); + while(1) { - - - pc.printf("last key:%d \n\r", getKey()); - displayScreen(1); - wait(1); - led1 = !led1; - displayScreen(0); - wait(1); + wait(0.5); } }
diff -r 0466445897b8 -r 9d975a9d2728 src/config.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/config.cpp Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,21 @@ +#include "config.h" + +std::string pause{"default"}; +std::string cycles{"default"}; + + +void setPause(std::string p){ + pause = p; +} + +void setCycles(std::string c){ + cycles = c; +} + +std::string getPause(void){ + return pause; +} + +std::string getCycles(void){ + return cycles; +} \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 src/display.cpp --- a/src/display.cpp Sat Aug 10 08:34:41 2019 +0000 +++ b/src/display.cpp Thu Aug 15 19:33:04 2019 +0000 @@ -1,3 +1,5 @@ +#include "inc/display.h" + #include "TextLCD.h" #include "mbed_assert.h" #include <string> @@ -12,8 +14,7 @@ v y */ -const uint8_t FIRST_LINE=0; -const uint8_t SECOND_LINE=1; + void setCursor(uint8_t x, uint8_t y){
diff -r 0466445897b8 -r 9d975a9d2728 src/massStorage.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/massStorage.cpp Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,82 @@ +#include "inc/massStorage.h" +#include "rtos.h" +#include "USBHostMSD.h" +#include "inc/config.h" +#include <string> + +void massStorage_task(void const *){ + bool readConfigs = false; + USBHostMSD msd("usb"); + int i = 0; + + while(1) { + + // try to connect a MSD device + while(!msd.connect()) { + Thread::wait(500); + } + + // in a loop, append a file + // if the device is disconnected, we try to connect it again + while(1) { + + if(!readConfigs){ + // read file + FILE *fp = fopen("/usb/config.txt", "r"); + if (fp != NULL){ + printf("===> found config\r\n"); + + std::string pause{""}; + std::string cycles{""}; + + while(1) { + char c = fgetc(fp); + if( feof(fp) || c == '\n') { + break ; + } + if(c != '\n' && c != '\r'){ + pause += c; + } + } + while(1) { + char c = fgetc(fp); + if( feof(fp) || c == '\n' ){ + break; + } + if(c != '\n' && c != '\r'){ + cycles += c; + } + } + fclose (fp); + setPause(pause); + setCycles(cycles); + printf("\r\n===> read the configs %s %s \r\n", pause.c_str(), cycles.c_str() ); + readConfigs = true; + } + else{ + printf("fp == NULL\r\n"); + } + } + else{ + // append a file + FILE *fp = fopen("/usb/day.log", "a"); + + if (fp != NULL) { + fprintf(fp, "log entry nr: %d!\r\n", i++); + printf("writing to logfile!\r\n"); + fclose(fp); + } else { + printf("FILE == NULL\r\n"); + } + } + + // if device disconnected, try to connect again + if (!msd.connected()){ + readConfigs = false; + break; + } + + Thread::wait(10000); + } + } +} \ No newline at end of file
diff -r 0466445897b8 -r 9d975a9d2728 src/server.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server.cpp Thu Aug 15 19:33:04 2019 +0000 @@ -0,0 +1,261 @@ +#include "mbed.h" +#include "wifi.h" +#include "rtos.h" +#include "inc/config.h" + +/*------------------------------------------------------------------------------ +Hyperterminal settings: 115200 bauds, 8-bit data, no parity + +This example + - connects to a wifi network (SSID & PWD to set in mbed_app.json) + - displays the IP address and creates a web page + - then connect on its IP address on the same wifi network with another device + - Now able to change the led status and read the temperature + +This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13), +wifi_dataready pin (PE_1), wifi reset pin (PE_8) +------------------------------------------------------------------------------*/ + +/* Private defines -----------------------------------------------------------*/ +#define WIFI_WRITE_TIMEOUT 10000 +#define WIFI_READ_TIMEOUT 10000 +#define PORT 80 + +/* Private typedef------------------------------------------------------------*/ +typedef enum +{ + WS_IDLE = 0, + WS_CONNECTED, + WS_DISCONNECTED, + WS_ERROR, +} WebServerState_t; + +/* Private macro -------------------------------------------------------------*/ +static int wifi_sample_run(void); +static void WebServerProcess(void); +static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature); +/* Private variables ---------------------------------------------------------*/ + +static uint8_t http[1024]; +static uint8_t resp[1024]; +uint16_t respLen; +uint8_t IP_Addr[4]; +uint8_t MAC_Addr[6]; +int32_t Socket = -1; +static WebServerState_t State = WS_ERROR; +char ModuleName[32]; + +DigitalOut led(LED2); +AnalogIn adc_temp(ADC_TEMP); + +void server_task(void const *) { + int ret = 0; + led = 0; + + printf("************************************************************\n"); + printf("*** STM32 IoT Discovery kit for STM32L475 MCU ***\n"); + printf("*** WIFI Web Server demonstration ***\n"); + printf("************************************************************\n"); + + /* Working application */ + ret = wifi_sample_run(); + + if (ret == 0) { + while(1) { + WebServerProcess (); + Thread::wait(1); + } + } +} + +int wifi_sample_run(void) +{ + + /*Initialize and use WIFI module */ + if(WIFI_Init() == WIFI_STATUS_OK) { + printf("WIFI Initialized.\n"); + + if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { + printf("> wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", + MAC_Addr[0], + MAC_Addr[1], + MAC_Addr[2], + MAC_Addr[3], + MAC_Addr[4], + MAC_Addr[5]); + } else { + printf("> ERROR : CANNOT get MAC address\n"); + } + + if( WIFI_Connect("nur-41416", "5j5b-6jx3-gclk-keew", WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { + printf("> wifi module connected \n"); + + if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { + printf("> wifi module got IP Address : %d.%d.%d.%d\n", + IP_Addr[0], + IP_Addr[1], + IP_Addr[2], + IP_Addr[3]); + + printf(">Start HTTP Server... \n"); + printf(">Wait for connection... \n"); + State = WS_IDLE; + } else { + printf("> ERROR : wifi module CANNOT get IP address\n"); + return -1; + } + } else { + printf("> ERROR : wifi module NOT connected\n"); + return -1; + } + } else { + printf("> ERROR : WIFI Module cannot be initialized.\n"); + return -1; + } + return 0; +} + +/** + * @brief Send HTML page + * @param None + * @retval None + */ +static void WebServerProcess(void) +{ + uint8_t LedState = 0; + float temp; + switch(State) + { + case WS_IDLE:{ + Socket = 0; + WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT); + + if(Socket != -1) + { + printf("> HTTP Server Started \n"); + State = WS_CONNECTED; + } + else + { + printf("> ERROR : Connection cannot be established.\n"); + State = WS_ERROR; + } + break; + } + case WS_CONNECTED:{ + + WIFI_ReceiveData(Socket, resp, 1200, &respLen, WIFI_READ_TIMEOUT); + + if( respLen > 0) + { + if(strstr((char *)resp, "GET")) /* GET: put web page */ + { + printf("get\n"); + temp = (adc_temp.read()*100); + if(SendWebPage(LedState, temp) != WIFI_STATUS_OK) + { + printf("> ERROR : Cannot send web page\n"); + State = WS_ERROR; + } + } + else if(strstr((char *)resp, "POST"))/* POST: received info */ + { + printf("post\n"); + if(strstr((char *)resp, "radio")) + { + if(strstr((char *)resp, "radio=0")) + { + LedState = 0; + led = 0; + } + else if(strstr((char *)resp, "radio=1")) + { + LedState = 1; + led = 1; + } + + temp = (adc_temp.read()*100); + if(SendWebPage(LedState, temp) != WIFI_STATUS_OK) + { + printf("> ERROR : Cannot send web page\n"); + State = WS_ERROR; + } + } + } + else{ + printf("unknown\n"); + } + } + + if(WIFI_StopServer(Socket) == WIFI_STATUS_OK) + { + WIFI_StartServer(Socket, WIFI_TCP_PROTOCOL, "", PORT); + } + else + { + State = WS_ERROR; + printf("some error\n"); + } + break; + } + case WS_ERROR: + default:{ + printf("error\n"); + break; + } + } +} + + +/** + * @brief Send HTML page + * @param None + * @retval None + */ +static WIFI_Status_t SendWebPage(uint8_t ledIsOn, float temperature) +{ + uint8_t pause[50]; + uint8_t cycles[50]; + uint8_t temp[50]; + uint16_t SentDataLength; + WIFI_Status_t ret; + + /* construct web page content */ + strcpy((char *)http, (char *)"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"); + strcat((char *)http, (char *)"<html>\r\n<body>\r\n"); + strcat((char *)http, (char *)"<title>STM32 Web Server</title>\r\n"); + strcat((char *)http, (char *)"<h2>Web Server</h2>\r\n"); + strcat((char *)http, (char *)"<br /><hr>\r\n"); + sprintf((char *)pause, "<h2>config: %s </h2>\r\n" "", getPause().c_str()); + strcat((char *)http, (char *)pause); + sprintf((char *)cycles, "<h2>config: %s </h2>\r\n" "", getCycles().c_str()); + strcat((char *)http, (char *)cycles); + strcat((char *)http, (char *)"<br /><hr>\r\n"); + strcat((char *)http, (char *)"<p><form method=\"POST\"><strong>Temp: <input type=\"text\" size=2 value=\""); + sprintf((char *)temp, "%f", temperature); + strcat((char *)http, (char *)temp); + strcat((char *)http, (char *)"\"> <sup>O</sup>C"); + + if (ledIsOn) + { + strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" >LED off"); + strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" checked>LED on"); + } + else + { + strcat((char *)http, (char *)"<p><input type=\"radio\" name=\"radio\" value=\"0\" checked>LED off"); + strcat((char *)http, (char *)"<br><input type=\"radio\" name=\"radio\" value=\"1\" >LED on"); + } + + strcat((char *)http, (char *)"</strong><p><input type=\"submit\"></form></span>"); + strcat((char *)http, (char *)"</body>\r\n</html>\r\n"); + + ret = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, WIFI_WRITE_TIMEOUT); + + if((ret == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http))) + { + ret = WIFI_STATUS_ERROR; + } + + return ret; +}