0

Dependencies:   Cayenne-MQTT-mbed Servo nfc X_NUCLEO_IDW01M1v2 NetworkSocketAPI 13

Committer:
kapitaninternet
Date:
Thu Sep 05 20:00:08 2019 +0000
Revision:
12:32dbf8ff8d80
Parent:
11:60c50eae8b81
Child:
13:7b3b429e9731
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:d20655cd1f3c 1 #include "MQTTTimer.h"
jburhenn 0:d20655cd1f3c 2 #include "CayenneMQTTClient.h"
jburhenn 0:d20655cd1f3c 3 #include "MQTTNetworkIDW01M1.h"
jburhenn 0:d20655cd1f3c 4 #include "SpwfInterface.h"
kapitaninternet 10:8892a1436342 5 #include "mbed.h"
kapitaninternet 11:60c50eae8b81 6 #include "XNucleoIKS01A2.h" // czujniki ruchu i otoczenia
kapitaninternet 11:60c50eae8b81 7 #include "XNucleoNFC01A1.h" // modul nfc
kapitaninternet 10:8892a1436342 8 #include "NDefLib/NDefNfcTag.h"
kapitaninternet 10:8892a1436342 9 #include "NDefLib/RecordType/RecordURI.h"
kapitaninternet 12:32dbf8ff8d80 10 #include "Servo.h"
kapitaninternet 12:32dbf8ff8d80 11
kapitaninternet 9:c206c33736fe 12
kapitaninternet 9:c206c33736fe 13 /* Instantiate the expansion board */
kapitaninternet 9:c206c33736fe 14 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
kapitaninternet 9:c206c33736fe 15
kapitaninternet 9:c206c33736fe 16 /* Retrieve the composing elements of the expansion board */
kapitaninternet 9:c206c33736fe 17 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
kapitaninternet 9:c206c33736fe 18 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
kapitaninternet 9:c206c33736fe 19
kapitaninternet 10:8892a1436342 20 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
kapitaninternet 10:8892a1436342 21 char* username = "68880f30-7425-11e9-beb3-736c9e4bf7d0";
kapitaninternet 10:8892a1436342 22 char* password = "19f07b4d8806fe42bdda724980634f39d8e639ba";
kapitaninternet 10:8892a1436342 23 char* clientID = "bb8e7cc0-74b9-11e9-94e9-493d67fd755e";
kapitaninternet 10:8892a1436342 24
kapitaninternet 12:32dbf8ff8d80 25 AnalogIn ain(A0);
kapitaninternet 12:32dbf8ff8d80 26 DigitalOut myLed(LED2);
kapitaninternet 12:32dbf8ff8d80 27 Servo myservo(PA_6);
kapitaninternet 12:32dbf8ff8d80 28 DigitalOut actuatorPin2(PA_7);
kapitaninternet 12:32dbf8ff8d80 29 float voltageMultiplier = 5.0;
kapitaninternet 12:32dbf8ff8d80 30 int publishInterval = 1000;
kapitaninternet 12:32dbf8ff8d80 31
kapitaninternet 10:8892a1436342 32 // WiFi network info.
kapitaninternet 10:8892a1436342 33 char* ssid = "Interneto";
kapitaninternet 10:8892a1436342 34 char* wifiPassword = "matu1234";
kapitaninternet 10:8892a1436342 35
kapitaninternet 12:32dbf8ff8d80 36 // /* Helper function for printing floats & doubles */
kapitaninternet 12:32dbf8ff8d80 37 // static char *print_double(char* str, double v, int decimalDigits=2)
kapitaninternet 12:32dbf8ff8d80 38 // {
kapitaninternet 12:32dbf8ff8d80 39 // int i = 1;
kapitaninternet 12:32dbf8ff8d80 40 // int intPart, fractPart;
kapitaninternet 12:32dbf8ff8d80 41 // int len;
kapitaninternet 12:32dbf8ff8d80 42 // char *ptr;
kapitaninternet 9:c206c33736fe 43
kapitaninternet 12:32dbf8ff8d80 44 // /* prepare decimal digits multiplicator */
kapitaninternet 12:32dbf8ff8d80 45 // for (;decimalDigits!=0; i*=10, decimalDigits--);
kapitaninternet 9:c206c33736fe 46
kapitaninternet 12:32dbf8ff8d80 47 // /* calculate integer & fractinal parts */
kapitaninternet 12:32dbf8ff8d80 48 // intPart = (int)v;
kapitaninternet 12:32dbf8ff8d80 49 // fractPart = (int)((v-(double)(int)v)*i);
kapitaninternet 9:c206c33736fe 50
kapitaninternet 12:32dbf8ff8d80 51 // /* fill in integer part */
kapitaninternet 12:32dbf8ff8d80 52 // sprintf(str, "%i.", intPart);
kapitaninternet 9:c206c33736fe 53
kapitaninternet 12:32dbf8ff8d80 54 // /* prepare fill in of fractional part */
kapitaninternet 12:32dbf8ff8d80 55 // len = strlen(str);
kapitaninternet 12:32dbf8ff8d80 56 // ptr = &str[len];
kapitaninternet 9:c206c33736fe 57
kapitaninternet 12:32dbf8ff8d80 58 // /* fill in leading fractional zeros */
kapitaninternet 12:32dbf8ff8d80 59 // for (i/=10;i>1; i/=10, ptr++) {
kapitaninternet 12:32dbf8ff8d80 60 // if (fractPart >= i) {
kapitaninternet 12:32dbf8ff8d80 61 // break;
kapitaninternet 12:32dbf8ff8d80 62 // }
kapitaninternet 12:32dbf8ff8d80 63 // *ptr = '0';
kapitaninternet 12:32dbf8ff8d80 64 // }
kapitaninternet 9:c206c33736fe 65
kapitaninternet 12:32dbf8ff8d80 66 // /* fill in (rest of) fractional part */
kapitaninternet 12:32dbf8ff8d80 67 // sprintf(ptr, "%i", fractPart);
kapitaninternet 9:c206c33736fe 68
kapitaninternet 12:32dbf8ff8d80 69 // return str;
kapitaninternet 12:32dbf8ff8d80 70 // }
kapitaninternet 9:c206c33736fe 71
kapitaninternet 9:c206c33736fe 72 /**
kapitaninternet 9:c206c33736fe 73 * Write a Ndef URI message linking to st.com site.
kapitaninternet 9:c206c33736fe 74 * Write an NDef message with a Uri record linking the st.com site
kapitaninternet 9:c206c33736fe 75 * @param nfcNucleo expansion board where write the NDef message
kapitaninternet 9:c206c33736fe 76 */
kapitaninternet 9:c206c33736fe 77
jburhenn 0:d20655cd1f3c 78 SpwfSAInterface interface(D8, D2); // TX, RX
jburhenn 0:d20655cd1f3c 79 MQTTNetwork<SpwfSAInterface> network(interface);
jburhenn 2:445f09de09bd 80 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:d20655cd1f3c 81
jburhenn 0:d20655cd1f3c 82 /**
jburhenn 0:d20655cd1f3c 83 * Print the message info.
jburhenn 0:d20655cd1f3c 84 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 85 */
jburhenn 2:445f09de09bd 86 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 87 {
jburhenn 0:d20655cd1f3c 88 switch (message.topic) {
jburhenn 0:d20655cd1f3c 89 case COMMAND_TOPIC:
jburhenn 0:d20655cd1f3c 90 printf("topic=Command");
jburhenn 0:d20655cd1f3c 91 break;
jburhenn 0:d20655cd1f3c 92 case CONFIG_TOPIC:
jburhenn 0:d20655cd1f3c 93 printf("topic=Config");
jburhenn 0:d20655cd1f3c 94 break;
jburhenn 0:d20655cd1f3c 95 default:
jburhenn 0:d20655cd1f3c 96 printf("topic=%d", message.topic);
jburhenn 0:d20655cd1f3c 97 break;
jburhenn 0:d20655cd1f3c 98 }
jburhenn 0:d20655cd1f3c 99 printf(" channel=%d", message.channel);
jburhenn 0:d20655cd1f3c 100 if (message.clientID) {
jburhenn 0:d20655cd1f3c 101 printf(" clientID=%s", message.clientID);
jburhenn 0:d20655cd1f3c 102 }
jburhenn 0:d20655cd1f3c 103 if (message.type) {
jburhenn 0:d20655cd1f3c 104 printf(" type=%s", message.type);
jburhenn 0:d20655cd1f3c 105 }
jburhenn 0:d20655cd1f3c 106 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 2:445f09de09bd 107 if (message.getValue(i)) {
jburhenn 2:445f09de09bd 108 printf(" value=%s", message.getValue(i));
jburhenn 0:d20655cd1f3c 109 }
jburhenn 2:445f09de09bd 110 if (message.getUnit(i)) {
jburhenn 2:445f09de09bd 111 printf(" unit=%s", message.getUnit(i));
jburhenn 0:d20655cd1f3c 112 }
jburhenn 0:d20655cd1f3c 113 }
jburhenn 0:d20655cd1f3c 114 if (message.id) {
jburhenn 0:d20655cd1f3c 115 printf(" id=%s", message.id);
jburhenn 0:d20655cd1f3c 116 }
jburhenn 0:d20655cd1f3c 117 printf("\n");
jburhenn 0:d20655cd1f3c 118 }
jburhenn 0:d20655cd1f3c 119
jburhenn 0:d20655cd1f3c 120 /**
jburhenn 0:d20655cd1f3c 121 * Handle messages received from the Cayenne server.
jburhenn 0:d20655cd1f3c 122 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 123 */
jburhenn 2:445f09de09bd 124 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 125 {
jburhenn 0:d20655cd1f3c 126 int error = 0;
jburhenn 0:d20655cd1f3c 127 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:d20655cd1f3c 128 outputMessage(message);
jburhenn 0:d20655cd1f3c 129
jburhenn 1:a2d8aec2bb92 130 if (message.topic == COMMAND_TOPIC) {
jburhenn 1:a2d8aec2bb92 131 switch(message.channel) {
kapitaninternet 12:32dbf8ff8d80 132 case 4:
kapitaninternet 12:32dbf8ff8d80 133 // Set the onboard LED state & actuator PIN
kapitaninternet 12:32dbf8ff8d80 134 myLed = atoi(message.getValue());
kapitaninternet 12:32dbf8ff8d80 135
kapitaninternet 12:32dbf8ff8d80 136 actuatorPin2 = atoi(message.getValue());
jburhenn 1:a2d8aec2bb92 137 // Publish the updated LED state
jburhenn 2:445f09de09bd 138 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 139 printf("Publish LED state failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 140 }
jburhenn 1:a2d8aec2bb92 141 break;
jburhenn 1:a2d8aec2bb92 142 }
jburhenn 1:a2d8aec2bb92 143
jburhenn 1:a2d8aec2bb92 144 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 1:a2d8aec2bb92 145 // An error response should be sent if there are issues processing the message.
jburhenn 4:939f308d004a 146 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 147 printf("Response failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 148 }
jburhenn 0:d20655cd1f3c 149 }
jburhenn 0:d20655cd1f3c 150 }
jburhenn 0:d20655cd1f3c 151
jburhenn 0:d20655cd1f3c 152 /**
jburhenn 0:d20655cd1f3c 153 * Connect to the Cayenne server.
jburhenn 0:d20655cd1f3c 154 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:d20655cd1f3c 155 */
jburhenn 0:d20655cd1f3c 156 int connectClient(void)
jburhenn 0:d20655cd1f3c 157 {
jburhenn 0:d20655cd1f3c 158 int error = 0;
jburhenn 0:d20655cd1f3c 159 // Connect to the server.
jburhenn 0:d20655cd1f3c 160 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:d20655cd1f3c 161 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:d20655cd1f3c 162 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 163 wait(2);
jburhenn 0:d20655cd1f3c 164 }
jburhenn 0:d20655cd1f3c 165
jburhenn 2:445f09de09bd 166 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:d20655cd1f3c 167 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 168 return error;
jburhenn 0:d20655cd1f3c 169 }
jburhenn 0:d20655cd1f3c 170 printf("Connected\n");
jburhenn 0:d20655cd1f3c 171
jburhenn 0:d20655cd1f3c 172 // Subscribe to required topics.
jburhenn 0:d20655cd1f3c 173 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 174 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 175 }
jburhenn 0:d20655cd1f3c 176 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 177 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:d20655cd1f3c 178 }
jburhenn 0:d20655cd1f3c 179
jburhenn 0:d20655cd1f3c 180 // 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:d20655cd1f3c 181 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:d20655cd1f3c 182 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 0:d20655cd1f3c 183
jburhenn 0:d20655cd1f3c 184 return CAYENNE_SUCCESS;
jburhenn 0:d20655cd1f3c 185 }
jburhenn 0:d20655cd1f3c 186
jburhenn 0:d20655cd1f3c 187 /**
jburhenn 0:d20655cd1f3c 188 * Main loop where MQTT code is run.
jburhenn 0:d20655cd1f3c 189 */
jburhenn 0:d20655cd1f3c 190 void loop(void)
jburhenn 0:d20655cd1f3c 191 {
jburhenn 7:78cefe0937ab 192 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
kapitaninternet 12:32dbf8ff8d80 193 MQTTTimer timer(publishInterval);
jburhenn 0:d20655cd1f3c 194
jburhenn 0:d20655cd1f3c 195 while (true) {
jburhenn 0:d20655cd1f3c 196 // Yield to allow MQTT message processing.
jburhenn 0:d20655cd1f3c 197 mqttClient.yield(1000);
jburhenn 0:d20655cd1f3c 198
jburhenn 0:d20655cd1f3c 199 // Check that we are still connected, if not, reconnect.
jburhenn 0:d20655cd1f3c 200 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:d20655cd1f3c 201 network.disconnect();
jburhenn 0:d20655cd1f3c 202 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 203 while (connectClient() != CAYENNE_SUCCESS) {
kapitaninternet 12:32dbf8ff8d80 204 wait(3);
jburhenn 0:d20655cd1f3c 205 }
jburhenn 0:d20655cd1f3c 206 }
jburhenn 0:d20655cd1f3c 207
kapitaninternet 12:32dbf8ff8d80 208 // Publish data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:d20655cd1f3c 209 if (timer.expired()) {
jburhenn 0:d20655cd1f3c 210 int error = 0;
kapitaninternet 9:c206c33736fe 211
kapitaninternet 10:8892a1436342 212 uint8_t id;
kapitaninternet 10:8892a1436342 213 float value1, value2;
kapitaninternet 12:32dbf8ff8d80 214 // char buffer1[32], buffer2[32];
kapitaninternet 9:c206c33736fe 215
kapitaninternet 10:8892a1436342 216 /* Enable all sensors */
kapitaninternet 10:8892a1436342 217 hum_temp->enable();
kapitaninternet 10:8892a1436342 218 press_temp->enable();
kapitaninternet 12:32dbf8ff8d80 219 hum_temp->read_id(&id);
kapitaninternet 12:32dbf8ff8d80 220 press_temp->read_id(&id);
kapitaninternet 10:8892a1436342 221
kapitaninternet 10:8892a1436342 222 hum_temp->get_temperature(&value1);
kapitaninternet 12:32dbf8ff8d80 223 // press_temp->get_temperature(&value1);
kapitaninternet 10:8892a1436342 224 press_temp->get_pressure(&value2);
kapitaninternet 12:32dbf8ff8d80 225 // printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
kapitaninternet 12:32dbf8ff8d80 226 float voltage_read = ain.read() * voltageMultiplier;
kapitaninternet 9:c206c33736fe 227
kapitaninternet 9:c206c33736fe 228 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, value1)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 229 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 230 }
kapitaninternet 10:8892a1436342 231
kapitaninternet 10:8892a1436342 232 if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_VOLTAGE, UNIT_VOLTS, voltage_read)) != CAYENNE_SUCCESS) {
kapitaninternet 10:8892a1436342 233 printf("Publish voltage failed, error: %d\n", error);
kapitaninternet 10:8892a1436342 234 }
kapitaninternet 9:c206c33736fe 235
kapitaninternet 9:c206c33736fe 236 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, value2)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 237 printf("Publish barometric pressure failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 238 }
kapitaninternet 9:c206c33736fe 239 // Restart the countdown timer for publishing data every 2 seconds. Change the timeout parameter to publish at a different interval.
kapitaninternet 12:32dbf8ff8d80 240 timer.countdown_ms(publishInterval);
jburhenn 0:d20655cd1f3c 241 }
jburhenn 0:d20655cd1f3c 242 }
jburhenn 0:d20655cd1f3c 243 }
jburhenn 0:d20655cd1f3c 244
jburhenn 0:d20655cd1f3c 245 /**
jburhenn 0:d20655cd1f3c 246 * Main function.
jburhenn 0:d20655cd1f3c 247 */
jburhenn 0:d20655cd1f3c 248 int main()
jburhenn 0:d20655cd1f3c 249 {
kapitaninternet 12:32dbf8ff8d80 250 myLed = 0;
kapitaninternet 12:32dbf8ff8d80 251
kapitaninternet 12:32dbf8ff8d80 252 actuatorPin2 = 0;
jburhenn 0:d20655cd1f3c 253 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
jburhenn 0:d20655cd1f3c 254
jburhenn 0:d20655cd1f3c 255 // Set the default function that receives Cayenne messages.
jburhenn 0:d20655cd1f3c 256 mqttClient.setDefaultMessageHandler(messageArrived);
kapitaninternet 12:32dbf8ff8d80 257
kapitaninternet 12:32dbf8ff8d80 258 for(float p=0; p<1.0; p += 0.2) {
kapitaninternet 12:32dbf8ff8d80 259 myservo = p;
kapitaninternet 12:32dbf8ff8d80 260 wait(1.5);
kapitaninternet 12:32dbf8ff8d80 261 }
jburhenn 0:d20655cd1f3c 262
jburhenn 0:d20655cd1f3c 263 // Connect to Cayenne.
jburhenn 0:d20655cd1f3c 264 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 265 // Run main loop.
jburhenn 0:d20655cd1f3c 266 loop();
jburhenn 0:d20655cd1f3c 267 }
jburhenn 0:d20655cd1f3c 268
kapitaninternet 12:32dbf8ff8d80 269
jburhenn 0:d20655cd1f3c 270 if (mqttClient.connected())
jburhenn 0:d20655cd1f3c 271 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 272 if (network.connected())
jburhenn 0:d20655cd1f3c 273 network.disconnect();
kapitaninternet 12:32dbf8ff8d80 274 return 0;
kapitaninternet 10:8892a1436342 275 }