smkhk_mail
Dependencies: Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI PWM_Tone_Library
main.cpp
00001 /** 00002 * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses 00003 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library. 00004 */ 00005 00006 #include "MQTTTimer.h" 00007 #include "CayenneMQTTClient.h" 00008 #include "MQTTNetworkIDW01M1.h" 00009 #include "SpwfInterface.h" 00010 #include "pwm_tone.h" 00011 00012 // WiFi network info. 00013 char* ssid = "iPhone"; 00014 char* wifiPassword = "abcd1234"; 00015 00016 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. 00017 char* username = "4f3fbcb0-3796-11e9-ad96-c15442ccb423"; 00018 char* password = "9e099f3d9aaedd7b76ca94044c6bb488c3999e3c"; 00019 char* clientID = "49143ea0-60c8-11e9-888e-af3db3cb8c63"; 00020 00021 SpwfSAInterface interface(D8, D2); // TX, RX 00022 MQTTNetwork<SpwfSAInterface> network(interface); 00023 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID); 00024 00025 DigitalOut led1(LED1); 00026 DigitalOut led2(D12); 00027 DigitalOut led3(D11); 00028 DigitalIn irsensor(D13); 00029 //DigitalOut buzzer(D7); 00030 00031 int iotvalue; 00032 /** 00033 * Print the message info. 00034 * @param[in] message The message received from the Cayenne server. 00035 */ 00036 void outputMessage(CayenneMQTT::MessageData& message) 00037 { 00038 switch (message.topic) { 00039 case COMMAND_TOPIC: 00040 printf("topic=Command"); 00041 break; 00042 case CONFIG_TOPIC: 00043 printf("topic=Config"); 00044 break; 00045 default: 00046 printf("topic=%d", message.topic); 00047 break; 00048 } 00049 printf(" channel=%d", message.channel); 00050 if (message.clientID) { 00051 printf(" clientID=%s", message.clientID); 00052 } 00053 if (message.type) { 00054 printf(" type=%s", message.type); 00055 } 00056 for (size_t i = 0; i < message.valueCount; ++i) { 00057 if (message.getValue(i)) { 00058 printf(" value=%s", message.getValue(i)); 00059 } 00060 if (message.getUnit(i)) { 00061 printf(" unit=%s", message.getUnit(i)); 00062 } 00063 } 00064 if (message.id) { 00065 printf(" id=%s", message.id); 00066 } 00067 printf("\n"); 00068 } 00069 00070 /** 00071 * Handle messages received from the Cayenne server. 00072 * @param[in] message The message received from the Cayenne server. 00073 */ 00074 void messageArrived(CayenneMQTT::MessageData& message) 00075 { 00076 int error = 0; 00077 00078 // Add code to process the message. Here we just ouput the message data. 00079 outputMessage(message); 00080 00081 if (message.topic == COMMAND_TOPIC) { 00082 switch(message.channel) { 00083 case 0: 00084 // Set the onboard LED state 00085 iotvalue = atoi(message.getValue()); 00086 printf("From Cayenne = %d\n",iotvalue); 00087 // Publish the updated LED state 00088 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) { 00089 printf("Publish LED state failure, error: %d\n", error); 00090 } 00091 break; 00092 } 00093 00094 // If this is a command message we publish a response. Here we are just sending a default 'OK' response. 00095 // An error response should be sent if there are issues processing the message. 00096 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) { 00097 printf("Response failure, error: %d\n", error); 00098 } 00099 } 00100 } 00101 00102 /** 00103 * Connect to the Cayenne server. 00104 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise. 00105 */ 00106 int connectClient(void) 00107 { 00108 int error = 0; 00109 // Connect to the server. 00110 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT); 00111 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) { 00112 printf("TCP connect failed, error: %d\n", error); 00113 wait(2); 00114 } 00115 00116 if ((error = mqttClient.connect()) != MQTT::SUCCESS) { 00117 printf("MQTT connect failed, error: %d\n", error); 00118 return error; 00119 } 00120 printf("Connected\n"); 00121 00122 // Subscribe to required topics. 00123 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { 00124 printf("Subscription to Command topic failed, error: %d\n", error); 00125 } 00126 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { 00127 printf("Subscription to Config topic failed, error:%d\n", error); 00128 } 00129 00130 // Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed. 00131 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION); 00132 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice"); 00133 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model"); 00134 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000"); 00135 00136 return CAYENNE_SUCCESS; 00137 } 00138 00139 /** 00140 * Main loop where MQTT code is run. 00141 */ 00142 void loop(void) 00143 { 00144 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. 00145 MQTTTimer timer(3000); 00146 00147 while (true) { 00148 00149 // Yield to allow MQTT message processing. 00150 mqttClient.yield(1000); 00151 00152 // Check that we are still connected, if not, reconnect. 00153 if (!network.connected() || !mqttClient.connected()) { 00154 network.disconnect(); 00155 mqttClient.disconnect(); 00156 printf("Reconnecting\n"); 00157 while (connectClient() != CAYENNE_SUCCESS) { 00158 wait(2); 00159 printf("Reconnect failed, retrying\n"); 00160 } 00161 } 00162 00163 int ir; 00164 00165 ir = !irsensor; 00166 if (ir == 1) led2 = 1; else led2 = 0; 00167 if (iotvalue == 1) led3 = 1; else led3 = 0; 00168 00169 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. 00170 if (timer.expired()) { 00171 int error = 0; 00172 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) { 00173 printf("Publish temperature failed, error: %d\n", error); 00174 } 00175 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_VOLTS, ir)) != CAYENNE_SUCCESS) { 00176 printf("Publish sensor failed, error: %d\n", error); 00177 } 00178 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. 00179 timer.countdown_ms(5000); 00180 } 00181 } 00182 } 00183 00184 /** 00185 * Main function. 00186 */ 00187 int main() 00188 { 00189 // Initialize the network interface. 00190 printf("Initializing interface\n"); 00191 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2); 00192 00193 // Set the default function that receives Cayenne messages. 00194 mqttClient.setDefaultMessageHandler(messageArrived); 00195 00196 // Connect to Cayenne. 00197 if (connectClient() == CAYENNE_SUCCESS) { 00198 // Run main loop. 00199 loop(); 00200 } 00201 else { 00202 printf("Connection failed, exiting\n"); 00203 } 00204 00205 if (mqttClient.connected()) 00206 mqttClient.disconnect(); 00207 if (network.connected()) 00208 network.disconnect(); 00209 00210 return 0; 00211 } 00212
Generated on Fri Jul 15 2022 19:23:00 by 1.7.2