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
Diff: main.cpp
- Revision:
- 15:7ba48f016569
- Parent:
- 14:0129ac8844ee
- Child:
- 17:b89d884eb609
--- a/main.cpp Sun Dec 10 17:17:41 2017 +0000
+++ b/main.cpp Tue Dec 12 15:58:44 2017 +0000
@@ -8,6 +8,8 @@
#include "NDefLib/RecordType/RecordURI.h"
#include "MQTTNetwork.h"
#include "MQTTmbed.h"
+#include "ReadSensor.h"
+#include "SimpleMQTT.h"
/***************************************************************
* Definitions
@@ -17,17 +19,14 @@
#define ID MQTT_DEVICE_ID // For a registered connection is your device id
#define AUTH_TOKEN MQTT_DEVICE_PASSWORD // For a registered connection is a device auth-token
#define DEFAULT_TYPE_NAME MQTT_DEVICE_TYPE // For a registered connection is device type
+#define AUTH_METHOD MQTT_USERNAME
-#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
-#define IBM_IOT_PORT MQTT_PORT
+#define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
+#define IBM_IOT_PORT MQTT_PORT
#define MQTT_MAX_PACKET_SIZE 400
#define MQTT_MAX_PAYLOAD_SIZE 300
-#define SENSOR_1_PIN (A0)
-#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)
@@ -64,37 +63,34 @@
static Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
-AnalogIn phSensor(SENSOR_1_PIN);
-AnalogIn DOSensor(SENSOR_2_PIN);
Timer timer;
/***************************************************************
* Unity function definitions
***************************************************************/
-void subscribe_cb(MQTT::MessageData & msgMQTT);
-int subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
-int connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-int getConnTimeout(int attemptNumber);
-void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
-int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
-void getADC();
+void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT);
+int MQTT_Subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client);
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
+int MQTT_GetConnTimeout(int attemptNumber);
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network);
+int MQTT_Publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime);
/***************************************************************
* Unity function declarations
***************************************************************/
-void subscribe_cb(MQTT::MessageData & msgMQTT) {
+void MQTT_SubscribeCallback(MQTT::MessageData & msgMQTT) {
char msg[MQTT_MAX_PAYLOAD_SIZE];
msg[0]='\0';
strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
- printf ("--->>> subscribe_cb msg: %s\n\r", msg);
+ printf ("--->>> MQTT_SubscribeCallback msg: %s\n\r", msg);
}
-int subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
- return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb);
+int MQTT_Subscribe(char *pubTopic, MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
+ return client->subscribe(pubTopic, MQTT::QOS1, MQTT_SubscribeCallback);
}
-int connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
+int MQTT_Connect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
const char* iot_ibm = MQTT_BROKER_URL;
char hostname[strlen(org) + strlen(iot_ibm) + 1];
@@ -109,9 +105,6 @@
LOG("Nucleo IP ADDRESS: %s\n\r", network->get_ip_address());
LOG("Nucleo MAC ADDRESS: %s\n\r", network->get_mac_address());
LOG("Server Hostname: %s port: %d\n\r", hostname, IBM_IOT_PORT);
-// for(int i = 0; clientId[i]; i++){ // set lowercase mac
-// clientId[i] = tolower(clientId[i]);
-// }
LOG("Client ID: %s\n\r", clientId);
LOG("Topic: %s\n\r",MQTT_EVENT_TOPIC);
LOG("Subscription URL: %s\n\r", subscription_url);
@@ -134,7 +127,7 @@
data.struct_version = 0;
data.clientID.cstring = clientId;
data.keepAliveInterval = MQTT_KEEPALIVE; // in Sec
- data.username.cstring = "use-token-auth";
+ data.username.cstring = AUTH_METHOD;
data.password.cstring = auth_token;
printf ("AutToken: %s\n\r", auth_token);
@@ -145,7 +138,7 @@
}
connected = true;
printf ("--->MQTT Connected\n\r");
- if ((rc=subscribe(topicCMD, client)) == 0) {
+ if ((rc = MQTT_Subscribe(topicCMD, client)) == 0) {
LOG ("--->>>MQTT subscribed to: %s\n\r", topicCMD);
} else {
LOG ("--->>>ERROR MQTT subscribe : %s\n\r", topicCMD);
@@ -156,25 +149,25 @@
}
-int getConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
+int MQTT_GetConnTimeout(int attemptNumber) { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
// after 20 attempts, retry every 10 minutes
return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
}
-void attemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
+void MQTT_AttemptConnect(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTNetwork *mqttNetwork, NetworkInterface* network) {
connected = false;
- while (connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) {
+ while (MQTT_Connect(client, mqttNetwork, network) != MQTT_CONNECTION_ACCEPTED) {
if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
return; // don't reattempt to connect if credentials are wrong
}
- int timeout = getConnTimeout(++retryAttempt);
+ int timeout = MQTT_GetConnTimeout(++retryAttempt);
WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
// if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
- // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
+ // or maybe just add the proper members to do this disconnect and call MQTT_AttemptConnect(...)
// this works - reset the system when the retry count gets to a threshold
if (retryAttempt == 5)
NVIC_SystemReset();
@@ -183,23 +176,21 @@
}
}
-int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime) {
+int MQTT_Publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client, time_t inputTime) {
MQTT::Message message;
const char* pubTopic = MQTT_EVENT_TOPIC;
char buf[MQTT_MAX_PAYLOAD_SIZE];
char timeBuf[50];
- float adc0;
if (!client->isConnected()) {
printf ("---> MQTT DISCONNECTED\n\r"); return MQTT::FAILURE;
}
- adc0 = ADC_PHVal;
strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
sprintf(buf,
"{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.4f}",
- projectName, timeBuf, cmdID, adc0);
+ projectName, timeBuf, cmdID, ADC_PHVal);
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
@@ -213,11 +204,6 @@
return client->publish(pubTopic, message);
}
-void getADC() {
- ADC_PHVal = phSensor.read();
-// ADC_DOVal = DOSensor.read();
-// voltageValue = (ADC_PHVal * 5.0);
-}
/***************************************************************
* Main
***************************************************************/
@@ -238,7 +224,7 @@
MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
printf ("ATTEMPT CONNECT\n\r");
- attemptConnect(&client, &mqttNetwork, network);
+ MQTT_AttemptConnect(&client, &mqttNetwork, network);
if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
printf ("---ERROR line : %d\n\r", __LINE__);
while (true)
@@ -248,14 +234,14 @@
myled=1;
while (true) {
time_t seconds = time(NULL);
- getADC();
+ ADC_PHVal = SENSOR_ReadPHValue();
if (connected == true) {
if ((uint32_t)(timer.read() - lastRead) >= uploadInterval) { // Publish a message every 3 second
- if (publish(&client, seconds) != MQTT::SUCCESS) {
+ if (MQTT_Publish(&client, seconds) != MQTT::SUCCESS) {
myled=0;
client.disconnect();
mqttNetwork.disconnect();
- attemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
+ MQTT_AttemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
} else {
myled = 1;
cmdID++;