iot_water_monitor_v2

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

Files at this revision

API Documentation at this revision

Comitter:
DuyLionTran
Date:
Sun Dec 10 17:17:41 2017 +0000
Parent:
13:344a5e2b2f53
Child:
15:7ba48f016569
Commit message:
version 1.6 time added using RTC

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Dec 07 17:11:37 2017 +0000
+++ b/main.cpp	Sun Dec 10 17:17:41 2017 +0000
@@ -61,7 +61,7 @@
 /***************************************************************
  * Structs/Classess
  ***************************************************************/
-static Serial serial(USBTX, USBRX); 
+static Serial pc(SERIAL_TX, SERIAL_RX); 
 
 DigitalOut 	myled(LED1);
 AnalogIn 	phSensor(SENSOR_1_PIN);
@@ -183,11 +183,12 @@
     }    
 }
 
-int publish(MQTT::Client<MQTTNetwork, Countdown, MQTT_MAX_PACKET_SIZE>* client) {
+int 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()) { 
@@ -195,10 +196,10 @@
 	}
 	
 	adc0 = ADC_PHVal;
-	
+	strftime(timeBuf, 50, "%Y/%m/%d %H:%M:%S", localtime(&inputTime));
     sprintf(buf,
-     "{\"Project\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.4f}",
-              projectName, cmdID, adc0);
+     "{\"Project\":\"%s\",\"Time\":\"%s\",\"Type\":1,\"cmdID\":%d,\"ADC0\":%0.4f}",
+              projectName, timeBuf, cmdID, adc0);
     message.qos        = MQTT::QOS0;
     message.retained   = false;
     message.dup        = false;
@@ -221,11 +222,12 @@
  * Main
  ***************************************************************/ 
 int main() {   
-   serial.baud(115200);
+   pc.baud(115200);
    timer.start();
    lastRead = 0;
-   serial.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
-   serial.printf("\r\nconnecting to AP\r\n");            
+//   set_time(1512950250);
+   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) {
@@ -243,21 +245,19 @@
        wait(1.0); // Permanent failures - don't retry
    }
 
-   myled=1;         
-   int count = 0;    
+   myled=1;          
 	while (true) {
+		time_t seconds = time(NULL);
 		getADC();
 		if (connected == true) {
 	       	if ((uint32_t)(timer.read() - lastRead) >= uploadInterval) {               // Publish a message every 3 second
-	    		if (publish(&client) != MQTT::SUCCESS) {    			 
+	    		if (publish(&client, seconds) != MQTT::SUCCESS) {    			 
 	        		myled=0;
-					count=0;
 	            	client.disconnect();
 					mqttNetwork.disconnect();			   
 	            	attemptConnect(&client, &mqttNetwork, network);   // if we have lost the connection                
 	        	} else {					 
 					myled = 1;
-	            	count = 0;
 	            	cmdID++;
 				}
 			lastRead = timer.read();