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:
2017-12-29
Revision:
21:1a63f4eaae14
Parent:
19:fbc21a964938
Child:
23:65cd2b5c76d5

File content as of revision 21:1a63f4eaae14:

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

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

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

/***************************************************************
 * Variables
 ***************************************************************/
uint32_t 	lastRead        = 0;
uint32_t    uploadInterval  = 5+1;
struct UploadValue dataStruct;

/* 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, ADC_PHVal) == MQTT::SUCCESS) { 
	    			wait(0.1); 
	    			if (MQTT_PublishRelayState(&client, seconds, 1, 1) == MQTT::SUCCESS) {
	    				myled = 1;
	    				commandID++;	
	    			}
	    			else {
	    				myled=0;
	    				client.disconnect();
	    				mqttNetwork.disconnect();
	    				MQTT_AttemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection   
	    			}	             
	        	} 
	        	else {	
	        		myled=0;
	            	client.disconnect();
					mqttNetwork.disconnect();			   
	            	MQTT_AttemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection 	        					 

				} 
				if ((commandID % 50) == 0) {
					timer.reset();
				}
				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);
		}
	}
}