This program utilizes the mcr20 Thread Shield on the FRDM-K64F MCU which is a two-part workspace (HVAC Server (RX)/Probe(TX)) to handle low temperature events read at the probe(s) to prevent pipes from freezing.

Dependencies:   DHT fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_wireless_uart by NXP

Revision:
29:c49f83ab80bd
Parent:
27:1eb29717bfd9
--- a/main.cpp	Tue Aug 18 13:04:46 2015 +0000
+++ b/main.cpp	Thu Mar 30 20:31:09 2017 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "rtos.h"
-
+#include "DHT.h"
 #include "Phy.h"
 #include "SMAC_Interface.h"
 #include "SMAC_Config.h"
@@ -40,8 +40,11 @@
 
 #define gDefaultBaudRate_UART_c 115200UL
 
+#define BUF_SIZE 3
+
 Serial uart(USBTX, USBRX);
 CircularBuffer uartBuf;
+DHT tempSensor(D4,DHT22);
 
 #ifdef VERBOSE
 static bool_t bCCAFailed;
@@ -71,19 +74,77 @@
 extern smacErrors_t smacToAppMcpsSap(smacToAppDataMessage_t* pMsg, instanceId_t instance);
 
 DigitalOut led1(LED_GREEN);
+DigitalOut led2(LED_RED);
+DigitalOut led3(LED_BLUE);
 InterruptIn sw2(SW2);
 uint32_t button_pressed;
+uint8_t timer;
 Thread *thread2;
 Thread *eventsThread;
+Thread *timerThread;
 
 void uartSetBaudRate(uint32_t b)
 {
     uart.baud(b);
 }
 
+/*ISR for sw2*/
+/*On sw2 press, spoof HVAC server by sending 30F as current temp*/
 void sw2_press(void)
+{      
+	/*Add fake temp to buffer*/ 
+    (void)uartBuf.addToBuffer('0');
+    (void)uartBuf.addToBuffer('3');
+    (void)uartBuf.addToBuffer('0');
+    
+    /*Set flags for TX in eventsThread and signal that an event has occured*/
+    gTaskEventFlags |= gWUSelf_EVENT_c;
+    thread2->signal_set(0x1);
+    
+}
+
+void timer_thread(void const *argument)
 {
-    thread2->signal_set(0x1);
+	char Buffer[BUF_SIZE];
+	int temp = 0;
+	int error = 0;
+	
+	while (true) {
+	
+		Thread::wait(1000);
+		timer++;	
+		
+		printf("timer(%d)\n\r",timer);
+		
+		/*Update temp and load to Buffer every 10 seconds*/
+		if(timer >= 10)
+		{
+			printf("Timer up! Preparing to send temp..\n\r");
+			
+			/*Reset timer*/
+			timer = 0;
+			
+			/*Wait for tempSensor data to be ready*/
+        	error = tempSensor.readData();
+        	while(0 != error)
+        	{
+        		error = tempSensor.readData();
+        	}
+        	
+        	temp = (int)tempSensor.ReadTemperature(FARENHEIT);
+        	
+        	/*Convert temp to ASCII and add to Buffer*/
+        	Buffer[0] = (temp / 100) + 0x30;//hundreds digit
+        	Buffer[1] = ((temp % 100) / 10) + 0x30;//tens digit
+        	Buffer[2] = ((temp % 100) % 10) + 0x30;//ones digit
+        		
+        	/*Buffer gets ASCII conversion of temp*/
+        	for(int i = 0; i < sizeof(Buffer); i++)
+        	{
+        		(void)uartBuf.addToBuffer(Buffer[i]);
+        	}/*end for*/
+		}/*end if*/	
+	}/*end while*/
 }
 
 void led_thread(void const *argument)
@@ -108,23 +169,27 @@
 
     while (true)
     {
+        /*Wait for signal*/
         Thread::signal_wait(0x1);
+        
+        /*If packet has been sent, reenable recieving mode*/
         if(gMcps_Cnf_EVENT_c == (gTaskEventFlags & gMcps_Cnf_EVENT_c))
         {
-            //get back in RX
+            /*Reenable RX requests*/
             MLMERXEnableRequest(gAppRxPacket, 0); 
-            //uart.printf("McpsDataCnf: Packet sent\r\n");
 
         }
         
+        /*If a packet has been recieved, rcvd gets data*/
         if(gMcps_Ind_EVENT_c == (gTaskEventFlags & gMcps_Ind_EVENT_c))
         {
-            rcvd = gAppRxPacket->smacPdu.smacPdu[0];
-
-            //get back in RX
-            //gAppRxPacket = (rxPacket_t*)MEM_BufferAlloc(gMaxSmacSDULength_c + sizeof(rxPacket_t));
-            //gAppRxPacket->u8MaxDataLength = gMaxSmacSDULength_c;
-            uart.printf("%c", rcvd);
+            for(int i = 0; i <= gAppRxPacket->u8DataLength; i++)
+            {
+            	rcvd = gAppRxPacket->smacPdu.smacPdu[i];
+				uart.printf("%c", rcvd);
+			}
+			
+            /*Reenable RX requests*/
             MLMERXEnableRequest(gAppRxPacket, 0);
 
             
@@ -145,15 +210,29 @@
             uart.printf("CcaCnf: \r\n");
         }
         
+        /*If there is something on the buffer, load packet and send*/
         if(gWUSelf_EVENT_c == (gTaskEventFlags & gWUSelf_EVENT_c))
         {
-            if (buffer_Ok_c == uartBuf.getFromBuffer(&c))
+            
+            /*Set Data Length to number of items on uartBuf*/
+            gAppTxPacket->u8DataLength = uartBuf.getCount();
+                
+            /*Load TX packets until uartBuf is empty*/
+            for(int i = 0; uartBuf.getCount() > 0; i++)
             {
-                gAppTxPacket->smacPdu.smacPdu[0] = c;
-                gAppTxPacket->u8DataLength = 1;
+            	uartBuf.getFromBuffer(&c);
+        		gAppTxPacket->smacPdu.smacPdu[i] = c;
+ 			}
+ 				/*Disable RX requests (block incoming packets)*/
                 (void)MLMERXDisableRequest();
+                /*Generate request to send data packet*/
                 (void)MCPSDataRequest(gAppTxPacket);
-            }
+                
+                printf("Data Sent\n\r");
+                
+                /*Toggle Red LED after transmission*/
+                led2 = !led2;
+            
         }
        
         gTaskEventFlags = 0;
@@ -161,11 +240,16 @@
 }
 
 int main()
-{
+{    
+    led1 = 1;/*Turn off Green LED*/
+    led2 = 1;/*Turn off Red LED*/
+    led3 = 1;/*Turn off Blue LED*/
+    
     MEM_Init();
     Thread thread(led_thread);
     thread2 = new Thread(button_thread);
     eventsThread = new Thread(events_thread);
+    timerThread = new Thread(timer_thread);
     Phy_Init();
     InitSmac();
     
@@ -182,14 +266,12 @@
     sw2.fall(&sw2_press);
     while (true) 
     {
-        if(uart.readable())
-        { 
-            (void)uartBuf.addToBuffer(uart.getc());
-        }
+        /*Set signal for eventsThread if anything on uartBuf to be sent*/
         if ( uartBuf.getCount() )
         {
             gTaskEventFlags |= gWUSelf_EVENT_c;
             eventsThread->signal_set(0x1);
+            
         }   
         Thread::yield();
     }