Praca inz. Mechatronika

Dependencies:   Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI

Revision:
8:d37ceed0cf58
Parent:
7:78cefe0937ab
Child:
9:ac39920339f5
--- a/main.cpp	Fri Nov 11 18:29:29 2016 +0000
+++ b/main.cpp	Mon Jan 25 13:31:41 2021 +0000
@@ -9,19 +9,41 @@
 #include "SpwfInterface.h"
 
 // WiFi network info.
-char* ssid = "ssid";
-char* wifiPassword = "wifiPassword";
+char* ssid = "HUAWEI Mate 10 lite";
+char* wifiPassword = "b080d7069728";
 
 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
-char* username = "MQTT_USERNAME";
-char* password = "MQTT_PASSWORD";
-char* clientID = "CLIENT_ID";
+char* username = "842b3540-1ecb-11ea-b301-fd142d6c1e6c";
+char* password = "53d11d4eb1e0e5402158cbb9eae9d32729ef3863";
+char* clientID = "4f12e010-3194-11eb-883c-638d8ce4c23d";
 
 SpwfSAInterface interface(D8, D2); // TX, RX
 MQTTNetwork<SpwfSAInterface> network(interface);
 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
 
-DigitalOut led1(LED1);
+DigitalOut alarm(PC_15); // P30 sterowanie alarmem
+DigitalIn drzwi1(PC_10); // P56 Drzwi wejściowe
+DigitalIn drzwi2(PC_11); // P57 Drzwi balkonowe
+
+DigitalOut str1(PC_7); // P48 SterowanieIn1
+DigitalOut str2(PA_7); // P49 SterowanieIn2
+DigitalOut str3(PA_6); // P50 SterowanieIn3
+DigitalOut str4(PA_5); // P51 SterowanieIn4
+
+DigitalOut swt1(PB_5); // P43 SwiatloIn1
+DigitalOut swt2(PB_4); // P44 SwiatloIn2
+DigitalOut swt3(PB_10); // P45 SwiatloIn3
+DigitalOut swt4(PA_8); // P46 SwiatloIn3
+
+DigitalIn okno1(PC_9); // P34 okno1
+DigitalIn okno2(PC_6); // P35 okno2
+DigitalIn okno3(PB_2); // P36 okno3
+DigitalIn okno4(PB_1); // P37 okno4
+
+DigitalIn czjn1(PC_1); // P39 czujnik1
+DigitalIn czjn2(PC_2); // P40 czujnik2
+DigitalIn czjn3(PC_3); // P41 czujnik3
+DigitalIn czjn4(PC_0); // P42 czujnik4
 
 /**
 * Print the message info.
@@ -63,19 +85,86 @@
 
 /**
 * Handle messages received from the Cayenne server.
-* @param[in] message The message received from the Cayenne server.
-*/
+* @param[in] message The message received from the Cayenne server. */
+
 void messageArrived(CayenneMQTT::MessageData& message)
 {
     int error = 0;
-    // Add code to process the message. Here we just ouput the message data.
     outputMessage(message);
 
     if (message.topic == COMMAND_TOPIC) {
         switch(message.channel) {
-        case 0:
+        case 0: // wirtualny kanał nr 0
+            alarm = atoi(message.getValue());
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 3: // wirtualny kanał nr 3
+            str1 = atoi(message.getValue());
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 6:
+            // Set the onboard LED state
+            str2 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 9:
+            // Set the onboard LED state
+            str3 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 12:
             // Set the onboard LED state
-            led1 = atoi(message.getValue());
+            str4 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 15:
+            // Set the onboard LED state
+            swt1 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 16:
+            // Set the onboard LED state
+            swt2 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 17:
+            // Set the onboard LED state
+            swt3 = atoi(message.getValue());
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+            
+            case 18:
+            // Set the onboard LED state
+            swt4 = atoi(message.getValue());
             // Publish the updated LED state
             if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
                 printf("Publish LED state failure, error: %d\n", error);
@@ -134,7 +223,7 @@
 void loop(void)
 {
     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
-    MQTTTimer timer(5000);
+    MQTTTimer timer(2000);
 
     while (true) {
         // Yield to allow MQTT message processing.
@@ -154,17 +243,48 @@
         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
         if (timer.expired()) {
             int error = 0;
-            if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 1, NULL, NULL, drzwi1)) != CAYENNE_SUCCESS) // wartośc 1 oznacza nr wirtualnego kanału
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, NULL, NULL, drzwi2)) != CAYENNE_SUCCESS) // wartośc 2 oznacza nr wirtualnego kanału
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 4, NULL, NULL, czjn1)) != CAYENNE_SUCCESS) // wartośc 4 oznacza nr wirtualnego kanału
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 7, NULL, NULL, czjn2)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 10, NULL, NULL, czjn3)) != CAYENNE_SUCCESS) 
+            {
                 printf("Publish temperature failed, error: %d\n", error);
             }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
-                printf("Publish luminosity failed, error: %d\n", error);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 13, NULL, NULL, czjn4)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);    
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 5, NULL, NULL, okno1)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);
             }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
-                printf("Publish barometric pressure failed, error: %d\n", error);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 8, NULL, NULL, okno2)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 11, NULL, NULL, okno3)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);
+            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 14, NULL, NULL, okno4)) != CAYENNE_SUCCESS) 
+            {
+                printf("Publish temperature failed, error: %d\n", error);
             }
             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
-            timer.countdown_ms(5000);
+            timer.countdown_ms(2000);
         }
     }
 }