Duy tran / Mbed OS 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:
Duy Tran@DESKTOP-0RPIPEQ
Date:
Mon Jan 08 22:09:56 2018 +0700
Parent:
30:e49b8e99db65
Child:
32:8226837c56ae
Commit message:
version 2.0: add command execution

Changed in this revision

Application/CommandExecution.cpp Show annotated file Show diff for this revision Revisions of this file
Application/CommandExecution.h Show annotated file Show diff for this revision Revisions of this file
Application/main.cpp Show annotated file Show diff for this revision Revisions of this file
Simple-MQTT/SimpleMQTT.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Application/CommandExecution.cpp	Mon Jan 08 22:09:56 2018 +0700
@@ -0,0 +1,2 @@
+#include "CommandExecution.h"
+#include "mbed.h"
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Application/CommandExecution.h	Mon Jan 08 22:09:56 2018 +0700
@@ -0,0 +1,7 @@
+#ifndef __COMMANDEXECUTION_H__
+#define __COMMANDEXECUTION_H__
+
+void CE_HandleRelays();
+void CE_HandleCondig();
+
+#endif __COMMANDEXECUTION_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Application/main.cpp	Mon Jan 08 22:09:56 2018 +0700
@@ -0,0 +1,130 @@
+// For receiving message handles, search "Message Handles" in SimpleMQTT.h
+
+/***************************************************************
+ * 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 = {19, 25, 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);
+DigitalOut  led1(RELAY_1_PIN);
+DigitalOut  led2(RELAY_2_PIN);
+
+Timer UploadTimer;
+/***************************************************************
+ * Unity function definitions
+ ***************************************************************/
+ 
+/***************************************************************
+ * Unity function declarations
+ ***************************************************************/ 
+void MQTT_MessageHandles(uint8_t ControlSignal) {
+	switch(ControlSignal) {
+		case (1): led1 = !led1;
+		break;
+		case (2): led2 = !led2;
+		break;
+		default: break;	
+	}	
+}
+
+
+/***************************************************************
+ * Main
+ ***************************************************************/ 
+int main() {   
+   	pc.baud(115200);
+   	UploadTimer.start();
+   	lastRead = 0;
+   set_time(1515445620);
+   	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;
+		} 
+	}
+}
+    
--- a/Simple-MQTT/SimpleMQTT.h	Mon Jan 08 15:00:41 2018 +0000
+++ b/Simple-MQTT/SimpleMQTT.h	Mon Jan 08 22:09:56 2018 +0700
@@ -12,7 +12,7 @@
 #include "MQTTmbed.h"
 
 #include "Json.h"
-#include "Json"
+#include "CommandExecution.h"
 /***************************************************************
  * Definitions
  ***************************************************************/