Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code
Revision 9:268373df20d7, committed 2017-12-01
- Comitter:
- DuyLionTran
- Date:
- Fri Dec 01 14:51:46 2017 +0000
- Parent:
- 8:844796296dea
- Child:
- 10:5d45f805080a
- Commit message:
- version 1.1.5
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Dec 01 03:13:52 2017 +0000
+++ b/main.cpp Fri Dec 01 14:51:46 2017 +0000
@@ -18,7 +18,6 @@
#define SENSOR_2_PIN (A1)
#define SENSOR_3_PIN (A3)
#define SENSOR_4_PIN (A4)
-
#define RELAY_1_PIN (D11)
#define RELAY_2_PIN (D12)
@@ -27,17 +26,19 @@
#define DEVICE_ID "PROEVN"
#define TOKEN "PROEVN2017"
+#define MAX_FAIL_ATTEMPT (5)
+
typedef enum {
MQTT_SUCCESS = 0,
MQTT_NETWORK_FAIL = -1,
MQTT_FAIL = -2
-};
+} mqtt_ret_val;
/***************************************************************
* Variables
***************************************************************/
/* MQTT Varialbles */
-float firmwareVersion = 0.9;
+float firmwareVersion = 0.91;
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topicCMD[] = "iot-2/cmd/command/fmt/json";
char topicEvent[] = "iot-2/evt/status_update/fmt/json";
@@ -48,6 +49,7 @@
/* Internet Varialbles */
bool internetState = false;
+uint8_t failAttempt = 0;
uint16_t cmdID = 0;
/* Time Handles */
@@ -55,7 +57,9 @@
uint32_t lastRead;
/* Analog Handles */
-float analogVal0;
+float ADC_PHVal;
+float ADC_DOVal;
+float voltageValue;
/***************************************************************
* Structs/Classess
@@ -65,8 +69,10 @@
MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
Timer readTime;
+Ticker readADC;
AnalogIn phSensor(SENSOR_1_PIN);
+AnalogIn DOSensor(SENSOR_2_PIN);
Serial serial(USBTX, USBRX);
@@ -74,13 +80,13 @@
* Unity function definitions
***************************************************************/
/**
- * @brief Establish a connection to the internet
+ * @brief Establish a connection to the internet.
* @retval MQTT_SUCCESS if connected to the internet, MQTT_NETWORK_FAIL if failed to establish the connection
*/
int MQTT_internetConnect();
/**
- * @brief Connect to the MQTT data and subscribe to the topic
+ * @brief Connect to the MQTT data and subscribe to the topic.
* @retval MQTT_SUCCESS if connected to the data and subscribed the topic successfully.
*/
int MQTT_networkConnect();
@@ -92,14 +98,30 @@
int MQTT_init();
/**
- * @brief Publish a message to the MQTT topic
+ * @brief Publish a message to the MQTT topic.
* @param sendMessage[in]: the message to be sent.
* @retval MQTT_SUCCESS if the message is sent.
*/
int MQTT_publish(char *sendMessage);
+/**
+ * @brief Publish the information about the device when start up.
+ * @retval None.
+ */
+void MQTT_publishDeviceInfo();
+
+/**
+ * @brief Read ADC values
+ * @retval None.
+ */
void getADC();
+/**
+ * @brief Start the timer
+ * @retval None.
+ */
+void setTimer();
+
/***************************************************************
* Callbacks
***************************************************************/
@@ -109,7 +131,6 @@
logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
}
-
/***************************************************************
* Unity function declarations
***************************************************************/
@@ -130,18 +151,20 @@
int MQTT_networkConnect() {
logMessage("Connecting to %s:%d\r\n", server, ibmPort);
- int rc = MQTT_SUCCESS;
- rc = mqttNetwork.connect(server, ibmPort);
+
+ int rc = MQTT_SUCCESS;
+ rc = mqttNetwork.connect(server, ibmPort);
+
if (rc != 0) {
logMessage("rc from TCP connect is %d\r\n", rc);
return rc;
}
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
- data.MQTTVersion = 3;
- data.clientID.cstring = clientId;
- data.username.cstring = authMethod;
- data.password.cstring = token;
+ data.MQTTVersion = 3;
+ data.clientID.cstring = clientId;
+ data.username.cstring = authMethod;
+ data.password.cstring = token;
if ((rc = client.connect(data)) != 0) {
logMessage("rc from MQTT connect is %d\r\n", rc);
@@ -165,10 +188,12 @@
return ret;
}
}
+ internetState = true;
ret = MQTT_networkConnect();
if (ret != MQTT_SUCCESS) {
printf("Fail to connect to MQTT or fail to subscribe\r\n");
+ internetState = false;
}
return ret;
@@ -176,14 +201,47 @@
int MQTT_publish(char *sendMessage) {
MQTT::Message msg;
- msg.qos = MQTT::QOS0;
- msg.retained = false;
- msg.dup = false;
- msg.payload = sendMessage;
+ msg.qos = MQTT::QOS0;
+ msg.retained = false;
+ msg.dup = false;
+ msg.payload = sendMessage;
msg.payloadlen = strlen(sendMessage);
return client.publish((char *)topicEvent, msg);
}
+void MQTT_publishDeviceInfo() {
+ char greetingMessage[100];
+ sprintf(greetingMessage, "{\"Device Name\":\"PROEVN\",\"Firmware Version\":%.2f}", firmwareVersion);
+ printf("Sending payload: %s\r\n", greetingMessage);
+ if (!MQTT_publish((char *)greetingMessage)) {
+ printf("Publish ok\r\n");
+ failAttempt = 0;
+ }
+ else {
+ printf("Publish failed\r\n");
+ failAttempt++;
+ }
+}
+
+void upload() {
+ char payload[100];
+ sprintf(payload, "{\"Type\":1,\"Command ID\":%d,\"ADC0\":%.2f,\"Voltage 1\":%.2f}", cmdID, ADC_PHVal, voltageValue);
+ if (!MQTT_publish(payload)) {
+ cmdID++;
+ }
+}
+
+void getADC() {
+ ADC_PHVal = phSensor.read();
+// ADC_DOVal = DOSensor.read();
+ voltageValue = (ADC_PHVal * 5.0);
+}
+
+void setTimer() {
+ readTime.start();
+ readADC.attach(&getADC, 1);
+}
+
/***************************************************************
* Main
***************************************************************/
@@ -193,45 +251,37 @@
logMessage("IoT Water Monitor project, firmware version is %.2f\r\n", firmwareVersion);
MQTT_init();
-
- char greetingMessage[100];
- sprintf(greetingMessage, "{\"Name\":\"PROEVN\",\"Firmware Version\":%.1f}", firmwareVersion);
+ wait(0.5);
+ MQTT_publishDeviceInfo();
+
+
+ setTimer();
- printf("Sending payload: %s\r\n", greetingMessage);
- if (!MQTT_publish((char *)greetingMessage)) {
- printf("Publish ok\r\n");
- }
- else {
- printf("Publish failed\r\n");
- }
-
- readTime.start();
uploadPeriod = 6;
-
printf("Get into loop\r\n");
- float voltageValue;
- while(true) {
- analogVal0 = phSensor.read();
- voltageValue = (analogVal0 * 5.0);
-
+
+ while(true) {
if ((uint32_t)(readTime.read() - lastRead) > uploadPeriod) {
char payload[100];
- sprintf(payload, "{\"Type\":1,\"Command ID\":%d,\"ADC0\":%.2f,\"Voltage 1\":%.2f}", cmdID, analogVal0, voltageValue);
+ sprintf(payload, "{\"Type\":1,\"Command ID\":%d,\"ADC0\":%.2f,\"Voltage 1\":%.2f}", cmdID, ADC_PHVal, voltageValue);
printf("MQTT publish %s\r\n", payload);
if (!MQTT_publish(payload)) {
printf("Publish ok\r\n");
cmdID++;
+ failAttempt = 0;
}
else {
printf("Publish failed\r\n");
+ failAttempt++;
}
-
+ if (failAttempt == MAX_FAIL_ATTEMPT) {
+ failAttempt = 0;
+ internetState = false;
+ }
lastRead = readTime.read();
- }
-
+ }
client.yield(100);
-
}
}