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
main.cpp
- Committer:
- DuyLionTran
- Date:
- 2017-12-12
- Revision:
- 17:b89d884eb609
- Parent:
- 15:7ba48f016569
- Child:
- 19:fbc21a964938
File content as of revision 17:b89d884eb609:
/***************************************************************
* Includes
***************************************************************/
#include "mbed.h"
#include "ReadSensor.h"
#include "SimpleMQTT.h"
/***************************************************************
* Definitions
***************************************************************/
#define RELAY_1_PIN (D11)
#define RELAY_2_PIN (D12)
/***************************************************************
* Variables
***************************************************************/
uint16_t cmdID = 0;
uint32_t lastRead = 0;
uint32_t uploadInterval = 5;
/* Analog Handles */
float ADC_PHVal;
float ADC_DOVal;
/***************************************************************
* Structs/Classess
***************************************************************/
static Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
Timer timer;
/***************************************************************
* Unity function definitions
***************************************************************/
/***************************************************************
* Unity function declarations
***************************************************************/
/***************************************************************
* Main
***************************************************************/
int main() {
pc.baud(115200);
timer.start();
lastRead = 0;
// set_time(1513125870);
pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
pc.printf("\r\nconnecting to AP\r\n");
NetworkInterface* network = easy_connect(true);
if (!network) {
printf ("Error easy_connect\n\r");
}
MQTTNetwork mqttNetwork(network);
MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE> client(mqttNetwork);
printf ("ATTEMPT CONNECT\n\r");
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)
wait(1.0); // Permanent failures - don't retry
}
myled=1;
while (true) {
time_t seconds = time(NULL);
ADC_PHVal = SENSOR_ReadPHADC();
if (connected == true) {
if ((uint32_t)(timer.read() - lastRead) >= uploadInterval) { // Publish a message every 3 second
if (MQTT_PublishADC(&client, seconds, cmdID, ADC_PHVal) != MQTT::SUCCESS) {
myled=0;
client.disconnect();
mqttNetwork.disconnect();
MQTT_AttemptConnect(&client, &mqttNetwork, network); // if we have lost the connection
} else {
myled = 1;
cmdID++;
}
lastRead = timer.read();
}
client.yield(500); // allow the MQTT client to receive subscribe messages and manage keep alive
} else if (connected == false) {
connected = true;
} else {
wait(0.5);
}
}
}