Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.52 KB | None | 0 0
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief WINC1500 MQTT chat example.
  6. *
  7. * Copyright (c) 2016 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41.  
  42. /** \mainpage
  43. * \section intro Introduction
  44. * This example demonstrates the use of the WINC1500 with the SAMD21 Xplained Pro
  45. * board to implement an MQTT based chat.
  46. * It uses the following hardware:
  47. * - the SAMD21 Xplained Pro.
  48. * - the WINC1500 on EXT1.
  49. *
  50. * \section files Main Files
  51. * - main.c : Initialize the WINC1500, connect to MQTT broker and chat with the other devices.
  52. * - mqtt.h : Implementation of MQTT 3.1
  53. *
  54. * \section usage Usage
  55. * -# Configure below code in the main.h for AP information to be connected.
  56. * \code
  57. * #define MAIN_WLAN_SSID "DEMO_AP"
  58. * #define MAIN_WLAN_AUTH M2M_WIFI_SEC_WPA_PSK
  59. * #define MAIN_WLAN_PSK "12345678"
  60. * \endcode
  61. * -# Build the program and download it into the board.
  62. * -# On the computer, open and configure a terminal application as the follows.
  63. * \code
  64. * Baud Rate : 115200
  65. * Data : 8bit
  66. * Parity bit : none
  67. * Stop bit : 1bit
  68. * Flow control : none
  69. * Line-Ending style : LF or CR+LF
  70. * \endcode
  71. * -# Start the application.
  72. * -# In the terminal window, First of all enter the user name through the terminal window.
  73. * -# And after the text of the following is displayed, please enjoy the chat.
  74. * -# Initialization operations takes a few minutes according to the network environment.
  75. * \code
  76. * Preparation of the chat has been completed.
  77. * \endcode
  78. *
  79. * \section known_issue Known Issue
  80. * -# The user name cannot contain space (' ').
  81. * -# Cannot send more than 128 bytes.
  82. * -# User name must be unique. If someone uses the same user name, Which one will be disconnected.
  83. * -# USART interface has not error detection procedure. So sometimes serial input is broken.
  84. *
  85. * \section compinfo Compilation Information
  86. * This software was written for the GNU GCC compiler using Atmel Studio 6.2
  87. * Other compilers may or may not work.
  88. *
  89. * \section contactinfo Contact Information
  90. * For further information, visit
  91. * <A href="http://www.atmel.com">Atmel</A>.\n
  92. */
  93.  
  94. #include "asf.h"
  95.  
  96. #include "main.h"
  97. #include "driver/include/m2m_wifi.h"
  98. #include "iot/sw_timer.h"
  99. #include "socket/include/socket.h"
  100. #include "motor.h"
  101. #include "relay.h"
  102. #include "HDC1080.h"
  103. #include "IIS2DHTR.h"
  104.  
  105.  
  106. static struct i2c_master_module i2c_module;
  107.  
  108.  
  109. /* Application instruction phrase. */
  110. #define STRING_EOL "\r\n"
  111. #define STRING_HEADER "-- WINC1500 Wi-Fi MQTT chat example --"STRING_EOL \
  112. "-- "BOARD_NAME " --"STRING_EOL \
  113. "-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
  114.  
  115. /** UART module for debug. */
  116. static struct usart_module cdc_uart_module;
  117.  
  118. /** Instance of Timer module. */
  119. struct sw_timer_module swt_module_inst;
  120.  
  121. ///** User name of chat. */
  122. //char mqtt_user[64] = "";
  123. //
  124. ///** Password of chat. */
  125. //char mqtt_pass[64] = "";
  126.  
  127. /** User name of chat. */
  128. char mqtt_user[] = "ese680";
  129.  
  130. /** Password of chat. */
  131. char mqtt_pass[] = "ese680";
  132.  
  133. /** Publishing text. */
  134. char pub_text[64] = "";
  135.  
  136. /* Instance of MQTT service. */
  137. static struct mqtt_module mqtt_inst;
  138.  
  139. /* Receive buffer of the MQTT service. */
  140. static char mqtt_buffer[MAIN_MQTT_BUFFER_SIZE];
  141.  
  142. /** UART buffer. */
  143. static char uart_buffer[MAIN_CHAT_BUFFER_SIZE];
  144.  
  145. /** Written size of UART buffer. */
  146. static int uart_buffer_written = 0;
  147.  
  148. /** A buffer of character from the serial. */
  149. static uint16_t uart_ch_buffer;
  150.  
  151.  
  152.  
  153. volatile int status;
  154. uint8_t buttonLevel = 0;
  155.  
  156.  
  157.  
  158.  
  159. /**
  160. * \brief Callback of USART input.
  161. *
  162. * \param[in] module USART module structure.
  163. */
  164. static void uart_callback(const struct usart_module *const module)
  165. {
  166. /* If input string is bigger than buffer size limit, ignore the excess part. */
  167. if (uart_buffer_written < MAIN_CHAT_BUFFER_SIZE) {
  168. uart_buffer[uart_buffer_written++] = uart_ch_buffer & 0xFF;
  169. }
  170. }
  171.  
  172. /**
  173. * \brief Callback to get the Wi-Fi status update.
  174. *
  175. * \param[in] msg_type type of Wi-Fi notification. Possible types are:
  176. * - [M2M_WIFI_RESP_CURRENT_RSSI](@ref M2M_WIFI_RESP_CURRENT_RSSI)
  177. * - [M2M_WIFI_RESP_CON_STATE_CHANGED](@ref M2M_WIFI_RESP_CON_STATE_CHANGED)
  178. * - [M2M_WIFI_RESP_CONNTION_STATE](@ref M2M_WIFI_RESP_CONNTION_STATE)
  179. * - [M2M_WIFI_RESP_SCAN_DONE](@ref M2M_WIFI_RESP_SCAN_DONE)
  180. * - [M2M_WIFI_RESP_SCAN_RESULT](@ref M2M_WIFI_RESP_SCAN_RESULT)
  181. * - [M2M_WIFI_REQ_WPS](@ref M2M_WIFI_REQ_WPS)
  182. * - [M2M_WIFI_RESP_IP_CONFIGURED](@ref M2M_WIFI_RESP_IP_CONFIGURED)
  183. * - [M2M_WIFI_RESP_IP_CONFLICT](@ref M2M_WIFI_RESP_IP_CONFLICT)
  184. * - [M2M_WIFI_RESP_P2P](@ref M2M_WIFI_RESP_P2P)
  185. * - [M2M_WIFI_RESP_AP](@ref M2M_WIFI_RESP_AP)
  186. * - [M2M_WIFI_RESP_CLIENT_INFO](@ref M2M_WIFI_RESP_CLIENT_INFO)
  187. * \param[in] pvMsg A pointer to a buffer containing the notification parameters
  188. * (if any). It should be casted to the correct data type corresponding to the
  189. * notification type. Existing types are:
  190. * - tstrM2mWifiStateChanged
  191. * - tstrM2MWPSInfo
  192. * - tstrM2MP2pResp
  193. * - tstrM2MAPResp
  194. * - tstrM2mScanDone
  195. * - tstrM2mWifiscanResult
  196. */
  197. static void wifi_callback(uint8 msg_type, void *msg_data)
  198. {
  199. tstrM2mWifiStateChanged *msg_wifi_state;
  200. uint8 *msg_ip_addr;
  201.  
  202. switch (msg_type) {
  203. case M2M_WIFI_RESP_CON_STATE_CHANGED:
  204. msg_wifi_state = (tstrM2mWifiStateChanged *)msg_data;
  205. if (msg_wifi_state->u8CurrState == M2M_WIFI_CONNECTED) {
  206. /* If Wi-Fi is connected. */
  207. printf("Wi-Fi connected\r\n");
  208. m2m_wifi_request_dhcp_client();
  209. } else if (msg_wifi_state->u8CurrState == M2M_WIFI_DISCONNECTED) {
  210. /* If Wi-Fi is disconnected. */
  211. printf("Wi-Fi disconnected\r\n");
  212. m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID),
  213. MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
  214. /* Disconnect from MQTT broker. */
  215. /* Force close the MQTT connection, because cannot send a disconnect message to the broker when network is broken. */
  216. mqtt_disconnect(&mqtt_inst, 1);
  217. }
  218.  
  219. break;
  220.  
  221. case M2M_WIFI_REQ_DHCP_CONF:
  222. msg_ip_addr = (uint8 *)msg_data;
  223. printf("Wi-Fi IP is %u.%u.%u.%u\r\n",
  224. msg_ip_addr[0], msg_ip_addr[1], msg_ip_addr[2], msg_ip_addr[3]);
  225. /* Try to connect to MQTT broker when Wi-Fi was connected. */
  226. mqtt_connect(&mqtt_inst, main_mqtt_broker);
  227. break;
  228.  
  229. default:
  230. break;
  231. }
  232. }
  233.  
  234. /**
  235. * \brief Callback to get the Socket event.
  236. *
  237. * \param[in] Socket descriptor.
  238. * \param[in] msg_type type of Socket notification. Possible types are:
  239. * - [SOCKET_MSG_CONNECT](@ref SOCKET_MSG_CONNECT)
  240. * - [SOCKET_MSG_BIND](@ref SOCKET_MSG_BIND)
  241. * - [SOCKET_MSG_LISTEN](@ref SOCKET_MSG_LISTEN)
  242. * - [SOCKET_MSG_ACCEPT](@ref SOCKET_MSG_ACCEPT)
  243. * - [SOCKET_MSG_RECV](@ref SOCKET_MSG_RECV)
  244. * - [SOCKET_MSG_SEND](@ref SOCKET_MSG_SEND)
  245. * - [SOCKET_MSG_SENDTO](@ref SOCKET_MSG_SENDTO)
  246. * - [SOCKET_MSG_RECVFROM](@ref SOCKET_MSG_RECVFROM)
  247. * \param[in] msg_data A structure contains notification informations.
  248. */
  249. static void socket_event_handler(SOCKET sock, uint8_t msg_type, void *msg_data)
  250. {
  251. mqtt_socket_event_handler(sock, msg_type, msg_data);
  252. }
  253.  
  254. /**
  255. * \brief Callback of gethostbyname function.
  256. *
  257. * \param[in] doamin_name Domain name.
  258. * \param[in] server_ip IP of server.
  259. */
  260. static void socket_resolve_handler(uint8_t *doamin_name, uint32_t server_ip)
  261. {
  262. mqtt_socket_resolve_handler(doamin_name, server_ip);
  263. }
  264.  
  265. /**
  266. * \brief Callback to get the MQTT status update.
  267. *
  268. * \param[in] conn_id instance id of connection which is being used.
  269. * \param[in] type type of MQTT notification. Possible types are:
  270. * - [MQTT_CALLBACK_SOCK_CONNECTED](@ref MQTT_CALLBACK_SOCK_CONNECTED)
  271. * - [MQTT_CALLBACK_CONNECTED](@ref MQTT_CALLBACK_CONNECTED)
  272. * - [MQTT_CALLBACK_PUBLISHED](@ref MQTT_CALLBACK_PUBLISHED)
  273. * - [MQTT_CALLBACK_SUBSCRIBED](@ref MQTT_CALLBACK_SUBSCRIBED)
  274. * - [MQTT_CALLBACK_UNSUBSCRIBED](@ref MQTT_CALLBACK_UNSUBSCRIBED)
  275. * - [MQTT_CALLBACK_DISCONNECTED](@ref MQTT_CALLBACK_DISCONNECTED)
  276. * - [MQTT_CALLBACK_RECV_PUBLISH](@ref MQTT_CALLBACK_RECV_PUBLISH)
  277. * \param[in] data A structure contains notification informations. @ref mqtt_data
  278. */
  279. static void mqtt_callback(struct mqtt_module *module_inst, int type, union mqtt_data *data)
  280. {
  281. switch (type) {
  282. case MQTT_CALLBACK_SOCK_CONNECTED:
  283. {
  284. /*
  285. * If connecting to broker server is complete successfully, Start sending CONNECT message of MQTT.
  286. * Or else retry to connect to broker server.
  287. */
  288. if (data->sock_connected.result >= 0) {
  289. //mqtt_connect_broker(module_inst, 1, NULL, NULL, mqtt_user, NULL, NULL, 0, 0, 0);
  290. status = mqtt_connect_broker(module_inst, 1, mqtt_user, mqtt_pass, mqtt_user, NULL, NULL, 0, 0, 0);
  291. } else {
  292. printf("Connect fail to server(%s)! retry it automatically.\r\n", main_mqtt_broker);
  293. mqtt_connect(module_inst, main_mqtt_broker); /* Retry that. */
  294. }
  295. }
  296. break;
  297.  
  298. case MQTT_CALLBACK_CONNECTED:
  299. if (data->connected.result == MQTT_CONN_RESULT_ACCEPT) {
  300. /* Subscribe chat topic. */
  301. delay_ms(1000);
  302. status = mqtt_subscribe(module_inst, MAIN_CHAT_TOPIC, 2);
  303. status = mqtt_subscribe(module_inst, SENSOR_TOPIC, 2);
  304. status = mqtt_subscribe(module_inst, ACTUATOR_TOPIC, 2);
  305. /* Enable USART receiving callback. */
  306. usart_enable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED);
  307. printf("Preparation of the chat has been completed.\r\n");
  308. } else {
  309. /* Cannot connect for some reason. */
  310. printf("MQTT broker decline your access! error code %d\r\n", data->connected.result);
  311. }
  312.  
  313. break;
  314.  
  315. case MQTT_CALLBACK_RECV_PUBLISH:
  316. /* You received publish message which you had subscribed. */
  317. if (data->recv_publish.topic != NULL && data->recv_publish.msg != NULL) {
  318.  
  319.  
  320. /// Main Topic
  321. if (!strncmp(data->recv_publish.topic, MAIN_CHAT_TOPIC, strlen(MAIN_CHAT_TOPIC)) ) {
  322. /* Print Topic */
  323. printf("%s >> ", MAIN_CHAT_TOPIC);
  324.  
  325. /* Print message */
  326. for (int i = 0; i < data->recv_publish.msg_size; i++) {
  327. printf("%c", data->recv_publish.msg[i]);
  328. }
  329. printf("\r\n");
  330. }
  331.  
  332. /// Sensor Topic
  333. if (!strncmp(data->recv_publish.topic, SENSOR_TOPIC, strlen(SENSOR_TOPIC)) ) {
  334. /* Print Topic */
  335. printf("%s >> ", SENSOR_TOPIC);
  336. port_pin_toggle_output_level(LED1_PIN);
  337. /* Print message */
  338. for (int i = 0; i < data->recv_publish.msg_size; i++) {
  339. printf("%c", data->recv_publish.msg[i]);
  340. }
  341. printf("\r\n");
  342. }
  343.  
  344. /// Actuator Topic
  345. if (!strncmp(data->recv_publish.topic, ACTUATOR_TOPIC, strlen(ACTUATOR_TOPIC)) ) {
  346. /* Print Topic */
  347. printf("%s >> ", ACTUATOR_TOPIC);
  348.  
  349. /* Print message */
  350. for (int i = 0; i < data->recv_publish.msg_size; i++) {
  351. printf("%c", data->recv_publish.msg[i]);
  352. }
  353. printf("\r\n");
  354. }
  355.  
  356. }
  357.  
  358. break;
  359.  
  360. case MQTT_CALLBACK_DISCONNECTED:
  361. /* Stop timer and USART callback. */
  362. printf("MQTT disconnected\r\n");
  363. usart_disable_callback(&cdc_uart_module, USART_CALLBACK_BUFFER_RECEIVED);
  364. break;
  365. }
  366. }
  367.  
  368. /**
  369. * \brief Configure UART console.
  370. */
  371. static void configure_console(void)
  372. {
  373. struct usart_config usart_conf;
  374.  
  375. usart_get_config_defaults(&usart_conf);
  376. usart_conf.mux_setting = GPIO_UART_SERCOM_MUX_SETTING;
  377. usart_conf.pinmux_pad0 = GPIO_UART_SERCOM_PINMUX_PAD0;
  378. usart_conf.pinmux_pad1 = GPIO_UART_SERCOM_PINMUX_PAD1;
  379. usart_conf.pinmux_pad2 = GPIO_UART_SERCOM_PINMUX_PAD2;
  380. usart_conf.pinmux_pad3 = GPIO_UART_SERCOM_PINMUX_PAD3;
  381. usart_conf.baudrate = 115200;
  382.  
  383. stdio_serial_init(&cdc_uart_module, GPIO_UART_MODULE, &usart_conf);
  384. /* Register USART callback for receiving user input. */
  385. usart_register_callback(&cdc_uart_module, (usart_callback_t)uart_callback, USART_CALLBACK_BUFFER_RECEIVED);
  386. usart_enable(&cdc_uart_module);
  387. }
  388.  
  389. /**
  390. * \brief Configure Timer module.
  391. */
  392. static void configure_timer(void)
  393. {
  394. struct sw_timer_config swt_conf;
  395. sw_timer_get_config_defaults(&swt_conf);
  396. sw_timer_init(&swt_module_inst, &swt_conf);
  397. sw_timer_enable(&swt_module_inst);
  398. }
  399.  
  400. /**
  401. * \brief Configure MQTT service.
  402. */
  403. static void configure_mqtt(void)
  404. {
  405.  
  406. struct mqtt_config mqtt_conf;
  407. int result;
  408. mqtt_get_config_defaults(&mqtt_conf);
  409. /* To use the MQTT service, it is necessary to always set the buffer and the timer. */
  410. mqtt_conf.timer_inst = &swt_module_inst;
  411. mqtt_conf.recv_buffer = mqtt_buffer;
  412. mqtt_conf.recv_buffer_size = MAIN_MQTT_BUFFER_SIZE;
  413. mqtt_conf.port = MQTT_PORT;
  414. result = mqtt_init(&mqtt_inst, &mqtt_conf);
  415. if (result < 0) {
  416. printf("MQTT initialization failed. Error code is (%d)\r\n", result);
  417. while (1) {
  418. }
  419. }
  420.  
  421. result = mqtt_register_callback(&mqtt_inst, mqtt_callback);
  422. if (result < 0) {
  423. printf("MQTT register callback failed. Error code is (%d)\r\n", result);
  424. while (1) {
  425. }
  426. }
  427. }
  428.  
  429. /**
  430. * \brief Checking the USART buffer.
  431. *
  432. * Finding the new line character(\n or \r\n) in the USART buffer.
  433. * If buffer was overflowed, Sending the buffer.
  434. */
  435. static void check_usart_buffer(char *topic)
  436. {
  437. int i;
  438.  
  439. /* Publish the input string when newline was received or input string is bigger than buffer size limit. */
  440. if (uart_buffer_written >= MAIN_CHAT_BUFFER_SIZE) {
  441. mqtt_publish(&mqtt_inst, topic, uart_buffer, MAIN_CHAT_BUFFER_SIZE, 0, 0);
  442. uart_buffer_written = 0;
  443. } else {
  444. for (i = 0; i < uart_buffer_written; i++) {
  445. /* Find newline character ('\n' or '\r\n') and publish the previous string . */
  446. if (uart_buffer[i] == '\n') {
  447. mqtt_publish(&mqtt_inst, topic, uart_buffer, (i > 0 && uart_buffer[i - 1] == '\r') ? i - 1 : i, 0, 0);
  448. /* Move remain data to start of the buffer. */
  449. if (uart_buffer_written > i + 1) {
  450. memmove(uart_buffer, uart_buffer + i + 1, uart_buffer_written - i - 1);
  451. uart_buffer_written = uart_buffer_written - i - 1;
  452. } else {
  453. uart_buffer_written = 0;
  454. }
  455.  
  456. break;
  457. }
  458. }
  459. }
  460. }
  461.  
  462. void configure_I2C(void) {
  463. struct i2c_master_config i2c_conf;
  464. i2c_master_get_config_defaults(&i2c_conf);
  465. i2c_conf.baud_rate = I2C_MASTER_BAUD_RATE_400KHZ;
  466. i2c_conf.pinmux_pad0 = COMM_I2C_SERCOM_PINMUX_PAD0;
  467. i2c_conf.pinmux_pad1 = COMM_I2C_SERCOM_PINMUX_PAD1;
  468. /* Initialize and enable device with config. */
  469. i2c_master_init(&i2c_module, COMM_I2C_MODULE, &i2c_conf);
  470. i2c_master_enable(&i2c_module);
  471. }
  472.  
  473. /**
  474. * \brief Main application function.
  475. *
  476. * Application entry point.
  477. *
  478. * \return program return value.
  479. */
  480. int main(void)
  481. {
  482. tstrWifiInitParam param;
  483. int8_t ret;
  484. char topic[strlen(MAIN_CHAT_TOPIC) + MAIN_CHAT_USER_NAME_SIZE + 1];
  485.  
  486.  
  487. /* Initialize the board. */
  488. system_init();
  489.  
  490. port_pin_set_output_level(PIN_PA06, LED0_INACTIVE);
  491.  
  492.  
  493. configure_I2C();
  494.  
  495. IIS2DHTR_init(&i2c_module);
  496.  
  497. /* Initialize the UART console. */
  498. configure_console();
  499.  
  500.  
  501.  
  502. /* Initialize the Timer. */
  503. configure_timer();
  504. /* Output example information */
  505. printf(STRING_HEADER);
  506.  
  507. /* Initialize the MQTT service. */
  508. configure_mqtt();
  509.  
  510. /* Initialize the BSP. */
  511. nm_bsp_init();
  512.  
  513.  
  514. /* Setup user name first */
  515. //printf("Enter the user name (Max %d characters)\r\n", MAIN_CHAT_USER_NAME_SIZE);
  516. //scanf("%64s", mqtt_user);
  517.  
  518. //printf("Enter the password (Max %d characters)\r\n", MAIN_CHAT_PASSWORD_SIZE);
  519. //scanf("%64s", mqtt_pass);
  520.  
  521. printf("User : %s\r\n", mqtt_user);
  522. printf("Password : %s\r\n", mqtt_user);
  523. sprintf(topic, "%s", MAIN_CHAT_TOPIC);
  524. printf("Topic : %s\r\n", topic);
  525.  
  526. /* Initialize Wi-Fi parameters structure. */
  527. memset((uint8_t *)&param, 0, sizeof(tstrWifiInitParam));
  528.  
  529. /* Initialize Wi-Fi driver with data and status callbacks. */
  530. param.pfAppWifiCb = wifi_callback; /* Set Wi-Fi event callback. */
  531. ret = m2m_wifi_init(&param);
  532. if (M2M_SUCCESS != ret) {
  533. printf("main: m2m_wifi_init call error!(%d)\r\n", ret);
  534. while (1) { /* Loop forever. */
  535. }
  536. }
  537.  
  538. /* Initialize socket interface. */
  539. socketInit();
  540. registerSocketCallback(socket_event_handler, socket_resolve_handler);
  541.  
  542. /* Connect to router. */
  543. m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID),
  544. MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL);
  545.  
  546. float angle = 0;
  547. while (1) {
  548. /* Handle pending events from network controller. */
  549. sint8 wifiStatus = m2m_wifi_handle_events(NULL);
  550. /* Try to read user input from USART. */
  551. usart_read_job(&cdc_uart_module, &uart_ch_buffer);
  552. /* Checks the timer timeout. */
  553. sw_timer_task(&swt_module_inst);
  554.  
  555. angle = IIS2DHTR_angle(plane_XY);
  556. printf("XY:%d\t", (int16_t)angle);
  557. delay_ms(250);
  558. sprintf(pub_text, "%d", (int16_t)angle);
  559. mqtt_publish(&mqtt_inst, SENSOR_TOPIC, pub_text, 3, 0, 1);
  560. //if( port_pin_get_input_level(SW0_PIN) != buttonLevel )
  561. //{
  562. ////int mqtt_publish(struct mqtt_module *const module, const char *topic, const char *msg, uint32_t msg_len, uint8_t qos, uint8_t retain);
  563. //buttonLevel = port_pin_get_input_level(SW0_PIN);
  564. //sprintf(pub_text, "%d", buttonLevel);
  565. //mqtt_publish(&mqtt_inst, SENSOR_TOPIC, pub_text, 1, 0, 1);
  566. //delay_ms(300);
  567. //}
  568.  
  569.  
  570. /* Checks the USART buffer. */
  571. check_usart_buffer(MAIN_CHAT_TOPIC);
  572. }
  573. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement