Manometr mierzący ciśnienie w kołach

Dependencies:   Cayenne-MQTT-mbed mbed TMP36 X_NUCLEO_IDW01M1v2 NetworkSocketAPI

Committer:
jburhenn
Date:
Tue Nov 01 18:48:43 2016 +0000
Revision:
0:4d7a64104b46
Child:
1:06d715293bc5
Example sending data from a TMP36 sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:4d7a64104b46 1 /**
jburhenn 0:4d7a64104b46 2 * Example app for using the Cayenne MQTT mbed library to send data from a TMP36 sensor. This example uses
jburhenn 0:4d7a64104b46 3 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library.
jburhenn 0:4d7a64104b46 4 */
jburhenn 0:4d7a64104b46 5
jburhenn 0:4d7a64104b46 6 #include "MQTTTimer.h"
jburhenn 0:4d7a64104b46 7 #include "CayenneMQTTClient.h"
jburhenn 0:4d7a64104b46 8 #include "MQTTNetworkIDW01M1.h"
jburhenn 0:4d7a64104b46 9 #include "SpwfInterface.h"
jburhenn 0:4d7a64104b46 10 #include "TMP36.h"
jburhenn 0:4d7a64104b46 11
jburhenn 0:4d7a64104b46 12 // WiFi network info.
jburhenn 0:4d7a64104b46 13 char* ssid = "ssid";
jburhenn 0:4d7a64104b46 14 char* wifiPassword = "wifiPassword";
jburhenn 0:4d7a64104b46 15
jburhenn 0:4d7a64104b46 16 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
jburhenn 0:4d7a64104b46 17 char* username = "MQTT_USERNAME";
jburhenn 0:4d7a64104b46 18 char* password = "MQTT_PASSWORD";
jburhenn 0:4d7a64104b46 19 char* clientID = "CLIENT_ID";
jburhenn 0:4d7a64104b46 20
jburhenn 0:4d7a64104b46 21 SpwfSAInterface interface(D8, D2); // TX, RX
jburhenn 0:4d7a64104b46 22 MQTTNetwork<SpwfSAInterface> network(interface);
jburhenn 0:4d7a64104b46 23 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:4d7a64104b46 24
jburhenn 0:4d7a64104b46 25 DigitalOut led1(LED1);
jburhenn 0:4d7a64104b46 26
jburhenn 0:4d7a64104b46 27 /**
jburhenn 0:4d7a64104b46 28 * Print the message info.
jburhenn 0:4d7a64104b46 29 * @param[in] message The message received from the Cayenne server.
jburhenn 0:4d7a64104b46 30 */
jburhenn 0:4d7a64104b46 31 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:4d7a64104b46 32 {
jburhenn 0:4d7a64104b46 33 switch (message.topic) {
jburhenn 0:4d7a64104b46 34 case COMMAND_TOPIC:
jburhenn 0:4d7a64104b46 35 printf("topic=Command");
jburhenn 0:4d7a64104b46 36 break;
jburhenn 0:4d7a64104b46 37 case CONFIG_TOPIC:
jburhenn 0:4d7a64104b46 38 printf("topic=Config");
jburhenn 0:4d7a64104b46 39 break;
jburhenn 0:4d7a64104b46 40 default:
jburhenn 0:4d7a64104b46 41 printf("topic=%d", message.topic);
jburhenn 0:4d7a64104b46 42 break;
jburhenn 0:4d7a64104b46 43 }
jburhenn 0:4d7a64104b46 44 printf(" channel=%d", message.channel);
jburhenn 0:4d7a64104b46 45 if (message.clientID) {
jburhenn 0:4d7a64104b46 46 printf(" clientID=%s", message.clientID);
jburhenn 0:4d7a64104b46 47 }
jburhenn 0:4d7a64104b46 48 if (message.type) {
jburhenn 0:4d7a64104b46 49 printf(" type=%s", message.type);
jburhenn 0:4d7a64104b46 50 }
jburhenn 0:4d7a64104b46 51 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 0:4d7a64104b46 52 if (message.getValue(i)) {
jburhenn 0:4d7a64104b46 53 printf(" value=%s", message.getValue(i));
jburhenn 0:4d7a64104b46 54 }
jburhenn 0:4d7a64104b46 55 if (message.getUnit(i)) {
jburhenn 0:4d7a64104b46 56 printf(" unit=%s", message.getUnit(i));
jburhenn 0:4d7a64104b46 57 }
jburhenn 0:4d7a64104b46 58 }
jburhenn 0:4d7a64104b46 59 if (message.id) {
jburhenn 0:4d7a64104b46 60 printf(" id=%s", message.id);
jburhenn 0:4d7a64104b46 61 }
jburhenn 0:4d7a64104b46 62 printf("\n");
jburhenn 0:4d7a64104b46 63 }
jburhenn 0:4d7a64104b46 64
jburhenn 0:4d7a64104b46 65 /**
jburhenn 0:4d7a64104b46 66 * Handle messages received from the Cayenne server.
jburhenn 0:4d7a64104b46 67 * @param[in] message The message received from the Cayenne server.
jburhenn 0:4d7a64104b46 68 */
jburhenn 0:4d7a64104b46 69 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:4d7a64104b46 70 {
jburhenn 0:4d7a64104b46 71 int error = 0;
jburhenn 0:4d7a64104b46 72 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:4d7a64104b46 73 outputMessage(message);
jburhenn 0:4d7a64104b46 74
jburhenn 0:4d7a64104b46 75 if (message.topic == COMMAND_TOPIC) {
jburhenn 0:4d7a64104b46 76 switch(message.channel) {
jburhenn 0:4d7a64104b46 77 case 0:
jburhenn 0:4d7a64104b46 78 // Set the onboard LED state
jburhenn 0:4d7a64104b46 79 led1 = atoi(message.getValue());
jburhenn 0:4d7a64104b46 80 // Publish the updated LED state
jburhenn 0:4d7a64104b46 81 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 82 printf("Publish LED state failure, error: %d\n", error);
jburhenn 0:4d7a64104b46 83 }
jburhenn 0:4d7a64104b46 84 break;
jburhenn 0:4d7a64104b46 85 }
jburhenn 0:4d7a64104b46 86
jburhenn 0:4d7a64104b46 87 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 0:4d7a64104b46 88 // An error response should be sent if there are issues processing the message.
jburhenn 0:4d7a64104b46 89 if ((error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 90 printf("Response failure, error: %d\n", error);
jburhenn 0:4d7a64104b46 91 }
jburhenn 0:4d7a64104b46 92 }
jburhenn 0:4d7a64104b46 93 }
jburhenn 0:4d7a64104b46 94
jburhenn 0:4d7a64104b46 95 /**
jburhenn 0:4d7a64104b46 96 * Connect to the Cayenne server.
jburhenn 0:4d7a64104b46 97 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:4d7a64104b46 98 */
jburhenn 0:4d7a64104b46 99 int connectClient(void)
jburhenn 0:4d7a64104b46 100 {
jburhenn 0:4d7a64104b46 101 int error = 0;
jburhenn 0:4d7a64104b46 102 // Connect to the server.
jburhenn 0:4d7a64104b46 103 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:4d7a64104b46 104 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:4d7a64104b46 105 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 106 wait(2);
jburhenn 0:4d7a64104b46 107 }
jburhenn 0:4d7a64104b46 108
jburhenn 0:4d7a64104b46 109 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:4d7a64104b46 110 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 111 return error;
jburhenn 0:4d7a64104b46 112 }
jburhenn 0:4d7a64104b46 113 printf("Connected\n");
jburhenn 0:4d7a64104b46 114
jburhenn 0:4d7a64104b46 115 // Subscribe to required topics.
jburhenn 0:4d7a64104b46 116 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 117 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 118 }
jburhenn 0:4d7a64104b46 119 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 120 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:4d7a64104b46 121 }
jburhenn 0:4d7a64104b46 122
jburhenn 0:4d7a64104b46 123 // 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.
jburhenn 0:4d7a64104b46 124 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:4d7a64104b46 125 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 0:4d7a64104b46 126 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 0:4d7a64104b46 127 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:4d7a64104b46 128
jburhenn 0:4d7a64104b46 129 return CAYENNE_SUCCESS;
jburhenn 0:4d7a64104b46 130 }
jburhenn 0:4d7a64104b46 131
jburhenn 0:4d7a64104b46 132 /**
jburhenn 0:4d7a64104b46 133 * Main loop where MQTT code is run.
jburhenn 0:4d7a64104b46 134 */
jburhenn 0:4d7a64104b46 135 void loop(void)
jburhenn 0:4d7a64104b46 136 {
jburhenn 0:4d7a64104b46 137 MQTTTimer timer(5000);
jburhenn 0:4d7a64104b46 138 TMP36 tmpSensor(A5);
jburhenn 0:4d7a64104b46 139
jburhenn 0:4d7a64104b46 140 while (true) {
jburhenn 0:4d7a64104b46 141 // Yield to allow MQTT message processing.
jburhenn 0:4d7a64104b46 142 mqttClient.yield(1000);
jburhenn 0:4d7a64104b46 143
jburhenn 0:4d7a64104b46 144 // Check that we are still connected, if not, reconnect.
jburhenn 0:4d7a64104b46 145 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:4d7a64104b46 146 network.disconnect();
jburhenn 0:4d7a64104b46 147 mqttClient.disconnect();
jburhenn 0:4d7a64104b46 148 printf("Reconnecting\n");
jburhenn 0:4d7a64104b46 149 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 150 wait(2);
jburhenn 0:4d7a64104b46 151 printf("Reconnect failed, retrying\n");
jburhenn 0:4d7a64104b46 152 }
jburhenn 0:4d7a64104b46 153 }
jburhenn 0:4d7a64104b46 154
jburhenn 0:4d7a64104b46 155 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:4d7a64104b46 156 if (timer.expired()) {
jburhenn 0:4d7a64104b46 157 int error = 0;
jburhenn 0:4d7a64104b46 158 if ((error = mqttClient.publishData(DATA_TOPIC, 5, TEMPERATURE, CELSIUS, tmpSensor.read())) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 159 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 160 }
jburhenn 0:4d7a64104b46 161 timer.countdown_ms(5000);
jburhenn 0:4d7a64104b46 162 }
jburhenn 0:4d7a64104b46 163 }
jburhenn 0:4d7a64104b46 164 }
jburhenn 0:4d7a64104b46 165
jburhenn 0:4d7a64104b46 166 /**
jburhenn 0:4d7a64104b46 167 * Main function.
jburhenn 0:4d7a64104b46 168 */
jburhenn 0:4d7a64104b46 169 int main()
jburhenn 0:4d7a64104b46 170 {
jburhenn 0:4d7a64104b46 171 // Initialize the network interface.
jburhenn 0:4d7a64104b46 172 printf("Initializing interface\n");
jburhenn 0:4d7a64104b46 173 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
jburhenn 0:4d7a64104b46 174
jburhenn 0:4d7a64104b46 175 // Set the default function that receives Cayenne messages.
jburhenn 0:4d7a64104b46 176 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:4d7a64104b46 177
jburhenn 0:4d7a64104b46 178 // Connect to Cayenne.
jburhenn 0:4d7a64104b46 179 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 180 // Run main loop.
jburhenn 0:4d7a64104b46 181 loop();
jburhenn 0:4d7a64104b46 182 }
jburhenn 0:4d7a64104b46 183 else {
jburhenn 0:4d7a64104b46 184 printf("Connection failed, exiting\n");
jburhenn 0:4d7a64104b46 185 }
jburhenn 0:4d7a64104b46 186
jburhenn 0:4d7a64104b46 187 if (mqttClient.connected())
jburhenn 0:4d7a64104b46 188 mqttClient.disconnect();
jburhenn 0:4d7a64104b46 189 if (network.connected())
jburhenn 0:4d7a64104b46 190 network.disconnect();
jburhenn 0:4d7a64104b46 191
jburhenn 0:4d7a64104b46 192 return 0;
jburhenn 0:4d7a64104b46 193 }
jburhenn 0:4d7a64104b46 194