iot_water_monitor_v2
Dependencies: easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code
Revision 57:898fcb6692cd, committed 2018-04-03
- Comitter:
- DuyLionTran
- Date:
- Tue Apr 03 17:03:01 2018 +0000
- Parent:
- 56:3729bedac5ab
- Commit message:
- ; * version 2.9.8 03-04-2018 Minor changes. Time frame updated to IBM Watson every 60s
Changed in this revision
--- a/Application/main.cpp Fri Mar 30 10:55:27 2018 +0000 +++ b/Application/main.cpp Tue Apr 03 17:03:01 2018 +0000 @@ -16,6 +16,7 @@ * version 2.9 03-22-2018: Watchdog worked. Some hardware bugs found * version 2.9.7 03-29-2018: Try to upload 1 more time if upload fail * version 2.9.7 03-30-2018 + * version 2.9.8 03-04-2018 Minor changes. Time frame updated to IBM Watson every 60s */ /*************************************************************** @@ -45,6 +46,7 @@ #define CALIB_STATE_CHANGE_PERIOD_S 5 #define ALARM_TIME_ODD 20 +#define SEND_TIME_INTERVAL 60 /* Send Time frame every 60s */ /*************************************************************** * Variables ***************************************************************/ @@ -231,17 +233,23 @@ lastRead = 0; pc.printf("\r\nViKa IoT Water Monitor mbed Application\r\n"); - pc.printf("\r\nconnecting to AP\r\n"); + pc.printf("\r\nconnecting to AP\r\n"); + + wd.Configure(24.8); + wd.Service(); NetworkInterface* network = easy_connect(true); if (!network) { printf ("Error easy_connect\n\r"); wifiConnected = false; } + wd.Service(); printf ("ATTEMPT CONNECT\n\r"); MQTTNetwork mqttNetwork(network); MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork); + wd.Service(); MQTT_AttemptConnect(&client, &mqttNetwork, network, DataStruct); + wd.Service(); if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { printf ("---ERROR line : %d, error type %d\n\r", __LINE__, connack_rc); wifiConnected = false; @@ -251,7 +259,7 @@ if (wd.WatchdogCausedReset()) { printf("Watchdog caused reset.\r\n"); } - wd.Configure(24.8); + wd.Service(); DisplayDO.attach(&SensorRun, PROCESS_SENSOR_VALUE_S); ReadAllFlashValues(); SENSOR_ReadDoCharacteristicValues(); @@ -277,11 +285,28 @@ if (MQTT_PublishAll(&client, seconds, SENSOR_VALUE, DataStruct) != MQTT::SUCCESS) { wait(2); MQTT_PublishAll(&client, seconds, SENSOR_VALUE, DataStruct); + wait(2); + uint32_t uploadTimeFramePeriod = SEND_TIME_INTERVAL; + if (MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod) == MQTT::SUCCESS) { + } + else { + MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod); + } } + else { + wait(2); + uint32_t uploadTimeFramePeriod = SEND_TIME_INTERVAL; + if (MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod) == MQTT::SUCCESS) { + } + else { + MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod); + } + } + wait(2); isFirstUpload = false; } /* Periodic upload */ - if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) { // Read timer every readSecond(s) + if ((uint32_t)(UploadTimer.read() - lastRead) >= READ_SECOND) { // Read timer every readSecond(s) /* Start uploading data */ if (!isUploading) { wd.Service(); @@ -290,6 +315,20 @@ uploadPeriodCounter = 0; isUploading = true; intervalSecondCounter = INTERVAL_BETWEEN_EACH_UPLOAD_TYPE; + } + else if ((uploadPeriodCounter % SEND_TIME_INTERVAL) == 0) { + uint32_t uploadTimeFramePeriod = SEND_TIME_INTERVAL; + if (MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod) == MQTT::SUCCESS) { + myled = 1; + } + else { + if (MQTT_PublishDeviceManage(&client, seconds, DataStruct.CONFIG_UploadInterval, uploadTimeFramePeriod) == MQTT::SUCCESS) { + myled = 1; + } + else { + myled = 0; + } + } } } else {
--- a/Simple-MQTT/SimpleMQTT.h Fri Mar 30 10:55:27 2018 +0000 +++ b/Simple-MQTT/SimpleMQTT.h Tue Apr 03 17:03:01 2018 +0000 @@ -166,6 +166,7 @@ */ int MQTT_PublishAll(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint8_t uploadType, struct UploadValue uploadStruct); +int MQTT_PublishDeviceManage(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint32_t uploadPeriod, uint32_t sendTimePeriod); /********************************************************************************************************************************************************************************************/ /*************************************************************** * Unity function declarations @@ -524,4 +525,32 @@ return retVal; } +int MQTT_PublishDeviceManage(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime, uint32_t uploadPeriod, uint32_t sendTimePeriod) { + MQTT::Message message; + const char* pubTopic = MQTT_MANAGE_TOPIC; + char buf[MQTT_MAX_PAYLOAD_SIZE]; + char timeBuf[50]; + + if (!client->isConnected()) { + printf ("---> MQTT DISCONNECTED\n\r"); + return MQTT::FAILURE; + } + strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime)); +// sprintf(buf, "{\"d\":{\"metadata\":{\"Device ID\":\"PROEVN\",\"Time\":%s,\"Upload Interval\":%d,\"Send Time Frame Interval\":%d},\"supports\":{\"deviceActions\":true}}}", +// timeBuf, uploadPeriod, sendTimePeriod); + sprintf(buf, "{\"d\":{\"metadata\":{\"Upload Interval\":%d},\"supports\":{\"deviceActions\":true}}}", + uploadPeriod); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf); + + if((message.payloadlen + strlen(pubTopic)+1) >= MQTT_MAX_PACKET_SIZE) + printf("message too long!\r\n"); + + LOG("Publishing %s\n\r", buf); + return client->publish(pubTopic, message); +} + #endif /* __SIMPLEMQTT_H__ */
--- a/mbed_app.json Fri Mar 30 10:55:27 2018 +0000 +++ b/mbed_app.json Tue Apr 03 17:03:01 2018 +0000 @@ -17,10 +17,10 @@ "value": "D0" }, "esp8266-ssid": { - "value": "\"GreenLantern\"" + "value": "\"Thanh Thao\"" }, "esp8266-password": { - "value": "\"deAlembert\"" + "value": "\"NgocQuyen\"" }, "esp8266-debug": { "value": true @@ -33,6 +33,7 @@ "MQTT_DEVICE_PASSWORD=\"PROEVN2017\"", "MQTT_EVENT_TOPIC=\"iot-2/evt/status/fmt/json\"", "MQTT_COMMAND_TOPIC=\"iot-2/cmd/command/fmt/json\"", + "MQTT_MANAGE_TOPIC=\"iotdevice-1/mgmt/manage\"", "MQTT_DEVICE_ID=\"PROEVN\"", "MQTT_DEVICE_TYPE=\"Nucleo_8266\"", "MQTT_KEEPALIVE 10",