mqtt_interface.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //*****************************************************************************
  2. //! \file mqtt_interface.h
  3. //! \brief Paho MQTT to WIZnet Chip interface Header file.
  4. //! \details The process of porting an interface to use paho MQTT.
  5. //! \version 1.0.0
  6. //! \date 2016/12/06
  7. //! \par Revision history
  8. //! <2016/12/06> 1st Release
  9. //!
  10. //! \author Peter Bang & Justin Kim
  11. //! \copyright
  12. //!
  13. //! Copyright (c) 2016, WIZnet Co., LTD.
  14. //! All rights reserved.
  15. //!
  16. //! Redistribution and use in source and binary forms, with or without
  17. //! modification, are permitted provided that the following conditions
  18. //! are met:
  19. //!
  20. //! * Redistributions of source code must retain the above copyright
  21. //! notice, this list of conditions and the following disclaimer.
  22. //! * Redistributions in binary form must reproduce the above copyright
  23. //! notice, this list of conditions and the following disclaimer in the
  24. //! documentation and/or other materials provided with the distribution.
  25. //! * Neither the name of the <ORGANIZATION> nor the names of its
  26. //! contributors may be used to endorse or promote products derived
  27. //! from this software without specific prior written permission.
  28. //!
  29. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. //! THE POSSIBILITY OF SUCH DAMAGE.
  40. //
  41. //*****************************************************************************
  42. /* MQTT subscribe Example.... W5500 + STM32F103(IoT board)
  43. //Include: Board configuration
  44. #include "IoTEVB.h"
  45. //Include: MCU peripheral Library
  46. #include "stm32f10x_rcc.h"
  47. #include "stm32f10x.h"
  48. //Include: W5500 iolibrary
  49. #include "w5500.h"
  50. #include "wizchip_conf.h"
  51. #include "misc.h"
  52. //Include: Internet iolibrary
  53. #include "MQTTClient.h"
  54. //Include: MCU Specific W5500 driver
  55. #include "W5500HardwareDriver.h"
  56. //Include: Standard IO Library
  57. #include <stdio.h>
  58. //Socket number defines
  59. #define TCP_SOCKET 0
  60. //Receive Buffer Size define
  61. #define BUFFER_SIZE 2048
  62. //Global variables
  63. unsigned char targetIP[4] = {}; // mqtt server IP
  64. unsigned int targetPort = 1883; // mqtt server port
  65. uint8_t mac_address[6] = {};
  66. wiz_NetInfo gWIZNETINFO = { .mac = {}, //user MAC
  67. .ip = {}, //user IP
  68. .sn = {},
  69. .gw = {},
  70. .dns = {},
  71. .dhcp = NETINFO_STATIC};
  72. unsigned char tempBuffer[BUFFER_SIZE] = {};
  73. struct opts_struct
  74. {
  75. char* clientid;
  76. int nodelimiter;
  77. char* delimiter;
  78. enum QoS qos;
  79. char* username;
  80. char* password;
  81. char* host;
  82. int port;
  83. int showtopics;
  84. } opts ={ (char*)"stdout-subscriber", 0, (char*)"\n", QOS0, NULL, NULL, targetIP, targetPort, 0 };
  85. // @brief messageArrived callback function
  86. void messageArrived(MessageData* md)
  87. {
  88. unsigned char testbuffer[100];
  89. MQTTMessage* message = md->message;
  90. if (opts.showtopics)
  91. {
  92. memcpy(testbuffer,(char*)message->payload,(int)message->payloadlen);
  93. *(testbuffer + (int)message->payloadlen + 1) = "\n";
  94. printf("%s\r\n",testbuffer);
  95. }
  96. if (opts.nodelimiter)
  97. printf("%.*s", (int)message->payloadlen, (char*)message->payload);
  98. else
  99. printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
  100. }
  101. // @brief 1 millisecond Tick Timer setting
  102. void NVIC_configuration(void)
  103. {
  104. NVIC_InitTypeDef NVIC_InitStructure;
  105. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  106. SysTick_Config(72000);
  107. NVIC_InitStructure.NVIC_IRQChannel = SysTick_IRQn;
  108. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Highest priority
  109. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  110. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  111. NVIC_Init(&NVIC_InitStructure);
  112. }
  113. // @brief 1 millisecond Tick Timer Handler setting
  114. void SysTick_Handler(void)
  115. {
  116. MilliTimer_Handler();
  117. }
  118. int main(void)
  119. {
  120. led_ctrl led1,led2;
  121. int i;
  122. int rc = 0;
  123. unsigned char buf[100];
  124. //Usart initialization for Debug.
  125. USART1Initialze();
  126. printf("USART initialized.\n\r");
  127. I2C1Initialize();
  128. printf("I2C initialized.\n\r");
  129. MACEEP_Read(mac_address,0xfa,6);
  130. printf("Mac address\n\r");
  131. for(i = 0 ; i < 6 ; i++)
  132. {
  133. printf("%02x ",mac_address[i]);
  134. }
  135. printf("\n\r");
  136. //LED initialization.
  137. led_initialize();
  138. led1 = led2 = ON;
  139. led2Ctrl(led2);
  140. led1Ctrl(led1);
  141. //W5500 initialization.
  142. W5500HardwareInitilize();
  143. printf("W5500 hardware interface initialized.\n\r");
  144. W5500Initialze();
  145. printf("W5500 IC initialized.\n\r");
  146. //Set network informations
  147. wizchip_setnetinfo(&gWIZNETINFO);
  148. setSHAR(mac_address);
  149. print_network_information();
  150. Network n;
  151. MQTTClient c;
  152. NewNetwork(&n, TCP_SOCKET);
  153. ConnectNetwork(&n, targetIP, targetPort);
  154. MQTTClientInit(&c,&n,1000,buf,100,tempBuffer,2048);
  155. MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
  156. data.willFlag = 0;
  157. data.MQTTVersion = 3;
  158. data.clientID.cstring = opts.clientid;
  159. data.username.cstring = opts.username;
  160. data.password.cstring = opts.password;
  161. data.keepAliveInterval = 60;
  162. data.cleansession = 1;
  163. rc = MQTTConnect(&c, &data);
  164. printf("Connected %d\r\n", rc);
  165. opts.showtopics = 1;
  166. printf("Subscribing to %s\r\n", "hello/wiznet");
  167. rc = MQTTSubscribe(&c, "hello/wiznet", opts.qos, messageArrived);
  168. printf("Subscribed %d\r\n", rc);
  169. while(1)
  170. {
  171. MQTTYield(&c, data.keepAliveInterval);
  172. }
  173. }
  174. */
  175. #ifndef __MQTT_INTERFACE_H_
  176. #define __MQTT_INTERFACE_H_
  177. #include <stdint.h>
  178. #ifdef __cplusplus
  179. extern "C" {
  180. #endif
  181. /*
  182. * @brief MQTT MilliTimer handler
  183. * @note MUST BE register to your system 1m Tick timer handler
  184. */
  185. void MilliTimer_Handler(void);
  186. /*
  187. * @brief Timer structure
  188. */
  189. typedef struct Timer Timer;
  190. struct Timer {
  191. unsigned long systick_period;
  192. unsigned long end_time;
  193. };
  194. /*
  195. * @brief Network structure
  196. */
  197. typedef struct Network Network;
  198. struct Network
  199. {
  200. int my_socket;
  201. int (*mqttread) (Network*, unsigned char*, int, long);
  202. int (*mqttwrite) (Network*, unsigned char*, int, long);
  203. void (*disconnect) (Network*);
  204. };
  205. /*
  206. * @brief Timer function
  207. */
  208. void TimerInit(Timer*);
  209. char TimerIsExpired(Timer*);
  210. void TimerCountdownMS(Timer*, unsigned int);
  211. void TimerCountdown(Timer*, unsigned int);
  212. int TimerLeftMS(Timer*);
  213. /*
  214. * @brief Network interface porting
  215. */
  216. int w5x00_read(Network*, unsigned char*, int, long);
  217. int w5x00_write(Network*, unsigned char*, int, long);
  218. void w5x00_disconnect(Network*);
  219. void NewNetwork(Network* n, int sn);
  220. int ConnectNetwork(Network* n, uint8_t* ip, uint16_t port);
  221. #ifdef __cplusplus
  222. }
  223. #endif
  224. #endif //__MQTT_INTERFACE_H_