test

Dependencies:   mbed-STM32F103C8T6 mbed-rtos mbed-dev

Committer:
hankzhang
Date:
Sat Apr 18 13:02:03 2020 +0000
Revision:
4:864bb8bde75c
Parent:
3:30d61fa10b98
Child:
6:a9cc2c424cf9
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
techneo 0:217105958c2d 1 #include "mbed.h"
bbw 1:0fe432e5dfc4 2 #include "stm32f103c8t6.h"
hankzhang 3:30d61fa10b98 3 #include "string.h"
hankzhang 3:30d61fa10b98 4 #include "main.h"
hankzhang 4:864bb8bde75c 5 #include "stdio.h"
hankzhang 4:864bb8bde75c 6 #include "stdlib.h"
bbw 1:0fe432e5dfc4 7
hankzhang 2:f48b0967b6cc 8 DigitalOut led1(PC_13);
techneo 0:217105958c2d 9
hankzhang 3:30d61fa10b98 10 //Serial pc(PB_10,PB_11);
hankzhang 3:30d61fa10b98 11
hankzhang 3:30d61fa10b98 12 UARTSerial *_serial;
bbw 1:0fe432e5dfc4 13
hankzhang 3:30d61fa10b98 14 UARTSerial debug_uart(PB_10, PB_11, 115200);
hankzhang 3:30d61fa10b98 15 UARTSerial wifi_uart(PA_2, PA_3, 115200);
hankzhang 3:30d61fa10b98 16
hankzhang 4:864bb8bde75c 17 //Serial wifi_uart(PA_2, PA_3, 115200);
hankzhang 4:864bb8bde75c 18 //Serial debug_uart(PB_10, PB_11, 115200);
hankzhang 4:864bb8bde75c 19
hankzhang 4:864bb8bde75c 20 char rxBuf[32];
hankzhang 4:864bb8bde75c 21 char wifi_rxBuf[32]; //receive msg from xiaomi cloud
hankzhang 4:864bb8bde75c 22 short g_isCloud = 0; //flag for connected xiaomi cloud
hankzhang 4:864bb8bde75c 23
hankzhang 4:864bb8bde75c 24 //char set_property[] = {"down set_properties "}
hankzhang 4:864bb8bde75c 25 //char get_property[] = {"down get_properties "}
hankzhang 3:30d61fa10b98 26 int i = 0;
hankzhang 3:30d61fa10b98 27
hankzhang 3:30d61fa10b98 28 ATCmdParser *_parser;
hankzhang 4:864bb8bde75c 29
hankzhang 4:864bb8bde75c 30 void led1_thread() {
hankzhang 4:864bb8bde75c 31 int length;
hankzhang 4:864bb8bde75c 32 char len[20];
hankzhang 4:864bb8bde75c 33 char l;
hankzhang 4:864bb8bde75c 34 int position = 0;
hankzhang 4:864bb8bde75c 35 int error = 0;
hankzhang 4:864bb8bde75c 36
hankzhang 4:864bb8bde75c 37 while (true)
hankzhang 4:864bb8bde75c 38 {
hankzhang 4:864bb8bde75c 39 wifi_uart.write("get_down\r", 9);
hankzhang 4:864bb8bde75c 40 if(wifi_uart.readable())
hankzhang 4:864bb8bde75c 41 {
hankzhang 4:864bb8bde75c 42 length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf));
hankzhang 4:864bb8bde75c 43 if(!(strncmp(wifi_rxBuf,"down none",9)))
hankzhang 4:864bb8bde75c 44 {
hankzhang 4:864bb8bde75c 45 //if return "down none"
hankzhang 4:864bb8bde75c 46 debug_uart.write("--- none",8);
hankzhang 4:864bb8bde75c 47 }
hankzhang 4:864bb8bde75c 48 else if(!(strncmp(wifi_rxBuf,"down set_properties ",20)))
hankzhang 4:864bb8bde75c 49 {
hankzhang 4:864bb8bde75c 50 //if return "down set_properties"
hankzhang 4:864bb8bde75c 51 debug_uart.write("--- set:", 8);
hankzhang 4:864bb8bde75c 52 debug_uart.write(wifi_rxBuf, length);
hankzhang 4:864bb8bde75c 53
hankzhang 4:864bb8bde75c 54 //set properties
hankzhang 4:864bb8bde75c 55 if(wifi_rxBuf[22] == '7')
hankzhang 4:864bb8bde75c 56 {
hankzhang 4:864bb8bde75c 57 //set target-position
hankzhang 4:864bb8bde75c 58 position = atoi(&wifi_rxBuf[24]);
hankzhang 4:864bb8bde75c 59 sprintf(len, "position:%d", position);
hankzhang 4:864bb8bde75c 60 debug_uart.write(len, sizeof(len));
hankzhang 4:864bb8bde75c 61 }
hankzhang 4:864bb8bde75c 62
hankzhang 4:864bb8bde75c 63 //report result to cloud
hankzhang 4:864bb8bde75c 64 wifi_uart.write("result 2 7 0\r", 13);
hankzhang 4:864bb8bde75c 65 if(wifi_uart.readable())
hankzhang 4:864bb8bde75c 66 {
hankzhang 4:864bb8bde75c 67 length = wifi_uart.read(wifi_rxBuf, sizeof(wifi_rxBuf));
hankzhang 4:864bb8bde75c 68 debug_uart.write(wifi_rxBuf, length);
hankzhang 4:864bb8bde75c 69 }
hankzhang 4:864bb8bde75c 70 }
hankzhang 4:864bb8bde75c 71 else if(!(strncmp(wifi_rxBuf,"down get_properties ",20)))
hankzhang 4:864bb8bde75c 72 {
hankzhang 4:864bb8bde75c 73 //if return "down get_properties"
hankzhang 4:864bb8bde75c 74 debug_uart.write("--- get:", 8);
hankzhang 4:864bb8bde75c 75 debug_uart.write(wifi_rxBuf, length);
hankzhang 4:864bb8bde75c 76
hankzhang 4:864bb8bde75c 77 //report result to cloud
hankzhang 4:864bb8bde75c 78 }
hankzhang 4:864bb8bde75c 79 else if(!(strncmp(wifi_rxBuf,"down MIIO_net_change ",21)))
hankzhang 4:864bb8bde75c 80 {
hankzhang 4:864bb8bde75c 81 //if return "down MIIO_net_change"
hankzhang 4:864bb8bde75c 82 debug_uart.write(wifi_rxBuf, length);
hankzhang 4:864bb8bde75c 83 debug_uart.write("--- net:",8);
hankzhang 4:864bb8bde75c 84 if((!strncmp(&wifi_rxBuf[21], "offline", 7)))
hankzhang 4:864bb8bde75c 85 {
hankzhang 4:864bb8bde75c 86 //连接中
hankzhang 4:864bb8bde75c 87 debug_uart.write("offline\r", 8);
hankzhang 4:864bb8bde75c 88 }
hankzhang 4:864bb8bde75c 89 else if((!strncmp(&wifi_rxBuf[21], "local", 5)))
hankzhang 4:864bb8bde75c 90 {
hankzhang 4:864bb8bde75c 91 //连上路由器但未连上服务器
hankzhang 4:864bb8bde75c 92 debug_uart.write("local\r", 6);
hankzhang 4:864bb8bde75c 93 }
hankzhang 4:864bb8bde75c 94 else if((!strncmp(&wifi_rxBuf[21], "cloud", 5)))
hankzhang 4:864bb8bde75c 95 {
hankzhang 4:864bb8bde75c 96 //连上小米云服务器
hankzhang 4:864bb8bde75c 97 debug_uart.write("cloud\r", 6);
hankzhang 4:864bb8bde75c 98 g_isCloud = 1;
hankzhang 4:864bb8bde75c 99 }
hankzhang 4:864bb8bde75c 100 }
hankzhang 4:864bb8bde75c 101 else
hankzhang 4:864bb8bde75c 102 {
hankzhang 4:864bb8bde75c 103 debug_uart.write(wifi_rxBuf, length);
hankzhang 4:864bb8bde75c 104 }
hankzhang 4:864bb8bde75c 105
hankzhang 4:864bb8bde75c 106 }
hankzhang 4:864bb8bde75c 107 thread_sleep_for(400);
techneo 0:217105958c2d 108 }
techneo 0:217105958c2d 109 }
techneo 0:217105958c2d 110
bbw 1:0fe432e5dfc4 111
hankzhang 2:f48b0967b6cc 112 void led0_thread() {
hankzhang 3:30d61fa10b98 113 int length;
hankzhang 3:30d61fa10b98 114 while (1) {
hankzhang 3:30d61fa10b98 115 if(debug_uart.readable())
hankzhang 3:30d61fa10b98 116 {
hankzhang 3:30d61fa10b98 117 length = debug_uart.read(rxBuf, sizeof(rxBuf));
hankzhang 3:30d61fa10b98 118 debug_uart.write(rxBuf, length);
hankzhang 3:30d61fa10b98 119 wifi_uart.write(rxBuf, length);
hankzhang 3:30d61fa10b98 120 debug_uart.write("111\r",4);
hankzhang 3:30d61fa10b98 121 }
hankzhang 3:30d61fa10b98 122 if(wifi_uart.readable())
hankzhang 3:30d61fa10b98 123 {
hankzhang 3:30d61fa10b98 124 length = wifi_uart.read(rxBuf, sizeof(rxBuf));
hankzhang 3:30d61fa10b98 125 debug_uart.write(rxBuf, length);
hankzhang 3:30d61fa10b98 126 debug_uart.write("222\r",4);
hankzhang 3:30d61fa10b98 127
hankzhang 3:30d61fa10b98 128 }
hankzhang 3:30d61fa10b98 129 wait(0.5);
bbw 1:0fe432e5dfc4 130 }
bbw 1:0fe432e5dfc4 131 }
hankzhang 3:30d61fa10b98 132
hankzhang 3:30d61fa10b98 133
hankzhang 2:f48b0967b6cc 134 int main() {
hankzhang 3:30d61fa10b98 135 int length;
hankzhang 3:30d61fa10b98 136 led1 = 1;
hankzhang 3:30d61fa10b98 137
hankzhang 4:864bb8bde75c 138 wait(1);
hankzhang 3:30d61fa10b98 139
hankzhang 3:30d61fa10b98 140 debug_uart.write("hello world",11);
hankzhang 2:f48b0967b6cc 141
hankzhang 4:864bb8bde75c 142 WIFI_PWREN = 1;
hankzhang 3:30d61fa10b98 143
hankzhang 4:864bb8bde75c 144 //Thread thread0(osPriorityNormal, 512, nullptr, nullptr);
hankzhang 4:864bb8bde75c 145 Thread thread1(osPriorityNormal, 512, nullptr, nullptr);
hankzhang 3:30d61fa10b98 146 //Thread thread2(osPriorityNormal, 512, nullptr, nullptr);
hankzhang 2:f48b0967b6cc 147
hankzhang 4:864bb8bde75c 148 //thread0.start(led0_thread);
hankzhang 4:864bb8bde75c 149 thread1.start(led1_thread);
hankzhang 2:f48b0967b6cc 150
hankzhang 3:30d61fa10b98 151 //wifi_serial.printf("model\r\n");
hankzhang 3:30d61fa10b98 152 //thread1.start(led1_thread);
hankzhang 3:30d61fa10b98 153 //thread2.start(led2_thread);
hankzhang 3:30d61fa10b98 154 while(1)
hankzhang 3:30d61fa10b98 155 {
hankzhang 3:30d61fa10b98 156 debug_uart.write("--main--\r\n",10);
hankzhang 4:864bb8bde75c 157 if(g_isCloud)
hankzhang 4:864bb8bde75c 158 {
hankzhang 4:864bb8bde75c 159 debug_uart.write("connect to xiaomi cloud\r\n",25);
hankzhang 4:864bb8bde75c 160 }
hankzhang 3:30d61fa10b98 161 wait(3);
bbw 1:0fe432e5dfc4 162 }
hankzhang 3:30d61fa10b98 163
hankzhang 2:f48b0967b6cc 164 }