Duy tran / Mbed OS iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

main.cpp

Committer:
DuyLionTran
Date:
2018-01-03
Revision:
26:f40cc4d011b0
Parent:
25:cf7a3e31622a
Child:
29:3636503b2539

File content as of revision 26:f40cc4d011b0:

/***************************************************************
 * Includes
 ***************************************************************/
#include "mbed.h"

#include "ReadSensor.h"
#include "SimpleMQTT.h"
#include "flash_programming.h"

/***************************************************************
 * Definitions
 ***************************************************************/
#define RELAY_1_PIN   (D11)
#define RELAY_2_PIN   (D12)

/***************************************************************
 * Variables
 ***************************************************************/
bool        isUploading         = false;
uint8_t 	uploadType          = ADC_VALUE;

uint32_t 	lastRead        	= 0;
uint16_t    readSecond      	= 5;  /* Read timer every 2 second(s) */
uint32_t    uploadPeriod        = 4;  /* Period between each time upload all data = uploadPeriod*readSecond = 20 s */
uint32_t    uploadPeriodCounter = 0;

struct UploadValue DataStruct = {17, 18, 0, 1, 0, 65, 80, 10};

/* Analog Handles */
float ADC_PHVal;
float ADC_DOVal;

/***************************************************************
 * Structs/Classess
 ***************************************************************/
static Serial pc(SERIAL_TX, SERIAL_RX); 

DigitalOut 	myled(LED1);

Timer UploadTimer;
/***************************************************************
 * Unity function definitions
 ***************************************************************/
 
/***************************************************************
 * Unity function declarations
 ***************************************************************/ 



/***************************************************************
 * Main
 ***************************************************************/ 
int main() {   
   	pc.baud(115200);
   	UploadTimer.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;   
   FP_WriteConfigValues(25, 26, 27);
	while (true) {
		time_t seconds = time(NULL);
		ADC_PHVal = SENSOR_ReadPHADC();
		if(connected == true) {
	       	if ((uint32_t)(UploadTimer.read() - lastRead) >= readSecond) {               // Publish a message every 3 second	
	       		printf("Upload time %ds\r\n", FP_ReadValue(UPLOAD_PERIOD_ARRESS));		
				if (!isUploading) {
					uploadPeriodCounter++;
					if (uploadPeriodCounter == uploadPeriod) {	
						uploadPeriodCounter = 0;
						isUploading         = true;
					}					
				}
				else {
					if (MQTT_PublishAll(&client, seconds, uploadType, DataStruct) ==  MQTT::SUCCESS) {
						myled = 1;
						uploadType++;
						if (uploadType > CONFIG_VALUE) {
							isUploading = false;
							uploadType  = ADC_VALUE;
							commandID++;
							UploadTimer.reset();
						}
					}
					else {
						myled = 0;
						client.disconnect();
						mqttNetwork.disconnect();
						MQTT_AttemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection 
					}
				}
				lastRead = UploadTimer.read();
	    	}        
	    	client.yield(500);                                                        // allow the MQTT client to receive subscribe messages and manage keep alive
		} 
		else if (connected == false) {
			connected = true;
		} 
	}
}