asdasd

Dependencies:   Cayenne-MQTT-mbed mbed X_NUCLEO_IKS01A2 X_NUCLEO_IDW01M1v2 NetworkSocketAPI TMP36

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 * Example app for using the Cayenne MQTT mbed library to send data from a TMP36 sensor. 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 "TMP36.h"
00011 #include "mbed.h"
00012 #include "XNucleoIKS01A2.h" 
00013 
00014 // WiFi network info.
00015 char* ssid = "ssid";
00016 char* wifiPassword = "wifiPassword";
00017 
00018 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
00019 char* username = "MQTT_USERNAME";
00020 char* password = "MQTT_PASSWORD";
00021 char* clientID = "CLIENT_ID";
00022 
00023 SpwfSAInterface interface(D8, D2); // TX, RX
00024 MQTTNetwork<SpwfSAInterface> network(interface);
00025 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
00026 // płytka dodatkowa
00027 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00028 //czujniki
00029 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00030 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
00031 
00032 DigitalOut led1(LED1);
00033 
00034 /**
00035 * Print the message info.
00036 * @param[in] message The message received from the Cayenne server.
00037 */
00038 void outputMessage(CayenneMQTT::MessageData& message)
00039 {
00040     switch (message.topic)  {
00041     case COMMAND_TOPIC:
00042         printf("topic=Command");
00043         break;
00044     case CONFIG_TOPIC:
00045         printf("topic=Config");
00046         break;
00047     default:
00048         printf("topic=%d", message.topic);
00049         break;
00050     }
00051     printf(" channel=%d", message.channel);
00052     if (message.clientID) {
00053         printf(" clientID=%s", message.clientID);
00054     }
00055     if (message.type) {
00056         printf(" type=%s", message.type);
00057     }
00058     for (size_t i = 0; i < message.valueCount; ++i) {
00059         if (message.getValue(i)) {
00060             printf(" value=%s", message.getValue(i));
00061         }
00062         if (message.getUnit(i)) {
00063             printf(" unit=%s", message.getUnit(i));
00064         }
00065     }
00066     if (message.id) {
00067         printf(" id=%s", message.id);
00068     }
00069     printf("\n");
00070 }
00071 
00072 /**
00073 * Handle messages received from the Cayenne server.
00074 * @param[in] message The message received from the Cayenne server.
00075 */
00076 void messageArrived(CayenneMQTT::MessageData& message)
00077 {
00078     int error = 0;
00079     // Add code to process the message. Here we just ouput the message data.
00080     outputMessage(message);
00081 
00082     if (message.topic == COMMAND_TOPIC) {
00083         switch(message.channel) {
00084         case 0:
00085             // Set the onboard LED state
00086             led1 = atoi(message.getValue());
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(5000);
00146     TMP36 tmpSensor(A5);
00147     float stopnie,paskal;
00148     char buffer6[32],buffer7[32];
00149     press_temp->enable();
00150     press_temp->get_pressure(&stopnie);
00151     while (true) {
00152         // Yield to allow MQTT message processing.
00153         mqttClient.yield(1000);
00154 
00155         // Check that we are still connected, if not, reconnect.
00156         if (!network.connected() || !mqttClient.connected()) {
00157             network.disconnect();
00158             mqttClient.disconnect();
00159             printf("Reconnecting\n");
00160             while (connectClient() != CAYENNE_SUCCESS) {
00161                 wait(2);
00162                 printf("Reconnect failed, retrying\n");
00163             }
00164         }
00165 
00166         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
00167         if (timer.expired()) {
00168             int error = 0;
00169             if ((error = mqttClient.publishData(stopnie)) != CAYENNE_SUCCESS) {
00170                 printf("Publish temperature failed, error: %d\n", error);
00171             }
00172             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
00173             timer.countdown_ms(5000);
00174         }
00175     }
00176 }
00177 
00178 
00179 
00180 
00181 
00182 
00183 //coś do dziesiątek 
00184 
00185 
00186 
00187 
00188 
00189 static char *print_double(char* str, double v, int decimalDigits=2)
00190 {
00191 int i = 1;
00192 int intPart, fractPart
00193 ;int len;
00194 char *ptr;/* prepare decimal digits multiplicator */
00195 for (;decimalDigits!=0; i*=10, decimalDigits--);
00196 /* calculate integer & fractinal parts */
00197 intPart = (int)v;
00198 fractPart= (int)((v-(double)(int)v)*i);/* fill in integer part */
00199 sprintf(str, "%i.", intPart);/* prepare fill in of fractional part */
00200 len = strlen(str);
00201 ptr = &str[len];/* fill in leading fractional zeros */
00202 for (i/=10;i>1; i/=10, ptr++) 
00203 {
00204     if (fractPart >= i) 
00205     {
00206         break;
00207         }
00208         *ptr = '0';
00209         }/* fill in (rest of) fractional part */
00210         sprintf(ptr, "%i", fractPart);
00211         return str;
00212         }
00213 int main ()
00214 {
00215 uint8_t id;                 
00216 float value1, value2;             //  Deklaracja 
00217 char buffer1[32], buffer2[32],buffer3[32],buffer4[32],buffer5[32];
00218 hum_temp->enable();
00219 press_temp->enable();
00220  //  hum_temp->get_temperature(&value1);         hum_temp->get_humidity(&value3);         press_temp->get_pressure(&value2); 
00221 printf("Start Programu");  
00222 hum_temp->read_id(&id);
00223 printf("HTS221 temeperatura=0x%X\r\n",id);  
00224 press_temp->read_id(&id);   
00225 printf("LPS22HB temeperatura=0x%X\r\n",id);                                            
00226 //  z czujników i wyświetlanie/
00227 press_temp->get_pressure(&value1);
00228 printf("Cisnie:  %7s hPa\r\n ", print_double(buffer1, value1)); 
00229 press_temp->get_temperature(&value2);
00230 printf("Temp:  %7s C\r\n ", print_double(buffer2, value2));
00231 double x,bary,ile;// Cisnie w kole zadane  
00232 int wzorzecP,wzorzecT;
00233 wzorzecP=230000;
00234 wzorzecT=293;
00235 x=(((wzorzecP)*(value2+ 273))/(wzorzecT));
00236 bary=x/100000;
00237 ile=(bary-(value1/1000));
00238 printf("Ile ma byc w kole?  %7s B\r\n ", print_double(buffer3,bary));
00239 printf("Ilejest w kole?  %7s B\r\n ", print_double(buffer1,value1/1000));
00240 if ((value1/1000)>bary)
00241 {
00242 printf("Upusc %7s B\r\n ", print_double(buffer5,ile ));  
00243 }
00244 else if ((value1/1000)<bary)
00245 {
00246     printf("dobij %7s B\r\n ", print_double(buffer5,ile ));
00247     }
00248      printf("Initializing interface\n");
00249     interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
00250 
00251     // Set the default function that receives Cayenne messages.
00252     mqttClient.setDefaultMessageHandler(messageArrived);
00253 
00254     // Connect to Cayenne.
00255     if (connectClient() == CAYENNE_SUCCESS) {
00256         // Run main loop.
00257         loop();
00258     }
00259     else {
00260         printf("Connection failed, exiting\n");
00261     }
00262 
00263     if (mqttClient.connected())
00264         mqttClient.disconnect();
00265     if (network.connected())
00266         network.disconnect();
00267 
00268     return 0;
00269 }
00270 
00271