MQTT with CC3000 Wi-Fi interface
Dependencies: ADT7410 MQTToveCC3000 MbedJSONValue NVIC_set_all_priorities cc3000_hostdriver_mbedsocket2 mbed
Fork of cc3000_simple_socket_demo by
main.cpp
00001 /* MQTT Smart Sensor 00002 * Author: Kazuhiro Matsuda 00003 * Ver: 0.1 2015-01-15 00004 * Ver: 0.2 2015-01-18 00005 * Ver: 1.0 2015-01-20 00006 */ 00007 00008 //#define DEBUG 00009 00010 #include "mbed.h" 00011 #include "cc3000.h" 00012 #include "TCPSocketConnection.h" 00013 #include "PubSubClient.h" 00014 #include "MbedJSONValue.h" 00015 #include "main.h" 00016 #include "ADT7410.h" 00017 00018 #define STRINGIFY(x) #x 00019 #define TO_STRING(x) STRINGIFY(x) 00020 00021 using namespace mbed_cc3000; 00022 00023 tUserFS user_info; 00024 00025 // CC3000 fix up 00026 cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7)); 00027 00028 // serial console for debug 00029 Serial pc(USBTX, USBRX); 00030 00031 // ADT7410 fix up 00032 ADT7410 tempsens(p28, p27, 0x90); 00033 00034 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG 00035 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; 00036 #else 00037 const uint8_t smartconfigkey = 0; 00038 #endif 00039 00040 DigitalOut led1(LED1); // connected to AP 00041 DigitalOut led2(LED2); // connected to MQTT broker 00042 DigitalOut led3(LED3); // publish a message 00043 DigitalOut led4(LED4); // subscribe a message 00044 00045 //MQTT broker information 00046 char* serverIpAddr = "10.10.0.1"; 00047 int port = 1883; 00048 00049 //MQTT client information 00050 char clientID[] = "temp1"; 00051 char pub_topic[] = "home/office/environment/temp1"; 00052 char sub_topic[] = "home/office/environment/temp1"; 00053 00054 //Sensor information 00055 uint16_t idx_low = 40, idx_high = 80; 00056 #define DURATION 10 00057 00058 //create MQTT Client instanse 00059 void callback(char* topic, char* payload, unsigned int len); //callback function prototype 00060 PubSubClient mqtt(serverIpAddr, port, callback); 00061 00062 void callback(char* topic, char* payload, unsigned int len) 00063 { 00064 MbedJSONValue json_msg, res, idx; 00065 std::string res_msg; 00066 00067 // parse json object 00068 parse(json_msg, payload); 00069 00070 if(json_msg.hasMember("cmd")) { 00071 led4 = 1; 00072 // set new threshold 00073 idx_low = json_msg["setidx_low"].get<int>(); 00074 // create response 00075 idx["setidx_low"] = idx_low; 00076 res["res"] = idx; 00077 00078 // serialize JSON in oder to send via network 00079 res_msg = res.serialize(); 00080 #ifdef DEBUG 00081 printf("send response %s\r\n", res_msg); 00082 #endif 00083 // send MQTT message 00084 mqtt.publish(topic, (char*)res_msg.c_str(), strlen(res_msg.c_str())); 00085 led4 = 0; 00086 } 00087 } 00088 00089 /** 00090 * \brief Print cc3000 information 00091 * \param none 00092 * \return none 00093 */ 00094 void print_cc3000_info() { 00095 uint8_t myMAC[8]; 00096 00097 wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); 00098 wifi.get_mac_address(myMAC); 00099 #ifdef DEBUG 00100 printf("MAC address + cc3000 info \r\n"); 00101 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); 00102 printf(" FTC %i \r\n",user_info.FTC); 00103 printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]); 00104 printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]); 00105 printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]); 00106 printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]); 00107 #endif 00108 } 00109 00110 /** 00111 * \brief Connect to SSID with a timeout 00112 * \param ssid Name of SSID 00113 * \param key Password 00114 * \param sec_mode Security mode 00115 * \return none 00116 */ 00117 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) { 00118 #ifdef DEBUG 00119 printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid); 00120 #endif 00121 if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) { 00122 #ifdef DEBUG 00123 printf(" Connected. \r\n"); 00124 #endif 00125 } else { 00126 #ifdef DEBUG 00127 printf(" Connection timed-out (error). Please restart. \r\n"); 00128 #endif 00129 while(1); 00130 } 00131 } 00132 00133 /** 00134 * \brief Connect to SSID without security 00135 * \param ssid Name of SSID 00136 * \return none 00137 */ 00138 void connect_to_ssid(char *ssid) { 00139 wifi.connect_open((uint8_t *)ssid); 00140 } 00141 00142 /** 00143 * \brief First time configuration 00144 * \param none 00145 * \return none 00146 */ 00147 void do_FTC(void) { 00148 printf("Running First Time Configuration \r\n"); 00149 wifi.start_smart_config(smartconfigkey); 00150 while (wifi.is_dhcp_configured() == false) { 00151 wait_ms(500); 00152 printf("Waiting for dhcp to be set. \r\n"); 00153 } 00154 user_info.FTC = 1; 00155 wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); 00156 wifi._wlan.stop(); 00157 printf("FTC finished. \r\n"); 00158 } 00159 00160 /** 00161 * \brief Start smart config 00162 * \param none 00163 * \return none 00164 */ 00165 void start_smart_config() { 00166 wifi.start_smart_config(smartconfigkey); 00167 } 00168 00169 void measure(){ 00170 float tmp; 00171 MbedJSONValue sts, body; 00172 std::string msg; 00173 00174 // measure temperature and humidity 00175 tmp = tempsens.read_temp(); 00176 00177 // create JSON message 00178 body["temp"] = tmp; 00179 body["unit"] = "degree C"; 00180 sts["sts"] = body; 00181 00182 // serialize JSON in oder to send via network 00183 msg = sts.serialize(); 00184 led3 = 1; 00185 mqtt.publish(pub_topic, (char*)msg.c_str(), strlen(msg.c_str())); 00186 led3 = 0; 00187 } 00188 00189 int main() { 00190 printf("----- Start MQTT client\r\n"); 00191 pc.baud(9600); 00192 00193 MbedJSONValue sts, body; 00194 std::string msg; 00195 uint16_t cnt = 0; 00196 00197 // start CC3000 Wi-Fi interface 00198 wifi.start(0); 00199 00200 #ifdef DEBUG 00201 print_cc3000_info(); 00202 printf("User's AP setup: SSID: %s, Password: %s, Security: %s \r\n", TO_STRING(SSID), TO_STRING(AP_KEY), TO_STRING(AP_SECURITY)); 00203 printf("Attempting SSID Connection. \r\n"); 00204 #endif 00205 00206 // set connection policy to manual mode 00207 wifi._wlan.ioctl_set_connection_policy(0, 0, 0); 00208 00209 // connect to AP 00210 connect_to_ssid(SSID, AP_KEY, AP_SECURITY); 00211 00212 while (wifi.is_dhcp_configured() == false) { 00213 wait_ms(500); 00214 #ifdef DEBUG 00215 printf("Waiting for dhcp to be set. \r\n"); 00216 #endif 00217 } 00218 00219 #ifdef DEBUG 00220 tNetappIpconfigRetArgs ipinfo2; 00221 wifi.get_ip_config(&ipinfo2); // data is returned in the ipinfo2 structure 00222 printf("DHCP assigned IP Address = %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]); 00223 #endif 00224 led1 = 1; 00225 00226 if(!mqtt.connect(clientID)){ 00227 printf("\r\nConnect to server failed ..\r\n"); 00228 return -1; 00229 } 00230 led2 = 1; 00231 00232 // define TOPIC to read 00233 mqtt.subscribe(sub_topic); 00234 00235 while(1){ 00236 mqtt.loop(); 00237 if(cnt > 20){ 00238 measure(); 00239 cnt = 0; 00240 } 00241 cnt++; 00242 wait(0.5); 00243 } 00244 }
Generated on Fri Jul 22 2022 00:28:23 by 1.7.2