Esempio n. 1
0
void FlyportTask()
{	
	vTaskDelay(100);
	UARTWrite(1,"Welcome to GROVE NEST example!\r\n");

	// GROVE board
	void *board = new(GroveNest);
	
	void *tempAn = new(An_Temp);
 
	// Attach devices
	attachToBoard(board, tempAn, AN1);
 
	float anVal = 0.0;
	char mess[50];
	char msg[50];
	
	// Connection to Network
	#if defined (FLYPORT_WF)
	WFConnect(WF_DEFAULT);
	while (WFStatus != CONNECTED);
	#endif
	#if defined (FLYPORTETH)
	while(!MACLinked);
	#endif
	UARTWrite(1,"Flyport connected... hello world!\r\n");
	vTaskDelay(200);
	
	
	while(1)
	{
		postSensorValue("temperature", (double)(get(tempAn)), "1377515972748279ff7a4b8ee4b27ba6f6db0412227ba");
		
		vTaskDelay(6000);
	}
}
void FlyportTask()
{	
	vTaskDelay(100);
	UARTWrite(1,"LHINGS on GROVE NEST example!\r\n");

	// Action & Status vars.
	char mEventMsg[] = "PIR_Picus: Someone is moving here!";
	BOOL LedStatus = FALSE;
	int mCont = 0;
	
	// GROVE board
	void *board = new(GroveNest);

	// GROVE devices	
	// Digital Input/outputs
	void *button = new(Dig_io, IN);			// Button/PIR sensor
	attachToBoard(board, button, DIG1);
	void *led = new(Dig_io, OUT);			// Led
	attachToBoard(board, led, DIG2);
	
	// Connection to Network
	#if defined (FLYPORT)
	WFConnect(WF_DEFAULT);
	while (WFStatus != CONNECTED);
	#endif
	#if defined (FLYPORTETH)
	while(!MACLinked);
	#endif
	UARTWrite(1,"Flyport connected!\r\n");
	vTaskDelay(200);
	
	//Turn on/off onboard leds
    IOPut(D4Out, on);
    IOPut(D5Out, off);
	
	// ** INITIALIZE LHINGS **
	// Init. Lhings engine
	if(!LhingsBegin(LHINGS_DEVNAME, LHINGS_DEVUUID, LHINGS_USERNAME, LHINGS_APIKEY))
		UARTWrite(1,"ERROR: Lhings access vars. out of range\r\n");
		
	// Initialize Led status
	set(led, OFF);
	LedStatus = FALSE;
	
	// Set ** LHINGS STATUS vars. **
	// Set the Value of the Status vars. defined above in Lhings_StatusList[]
	LhingsStatusWrite("Led status", "off");
	
	
	while(1)
	{
		// ** Lhings LOOP ** 
		LhingsLoop();
		
		// Check pressure of button / motion detected by PIR sensor 
		if((get(button) != 0) && (mCont==0))
		{
			// Event detected: Publish it in Lhings with a Payload message
			LhingsEventWrite_withPayload("motion", mEventMsg, strlen(mEventMsg), EVT_PAYLOAD);				
			UARTWrite(1, "DIG1 motion detected!\r\n");
			vTaskDelay(20);
			
			// Initialize detection delay counter for PIR sensor
			mCont = 0x0025;
			
			if(LedStatus){
				set(led, OFF);
				LedStatus = FALSE;
				// Set ** LHINGS STATUS vars. **
				// Set the Value of the Status vars. defined above in Lhings_StatusList[]
				LhingsStatusWrite("Led status", "off");
			}
			else{
				set(led, ON);
				LedStatus = TRUE;
				// Set ** LHINGS STATUS vars. **
				// Set the Value of the Status vars. defined above in Lhings_StatusList[]
				LhingsStatusWrite("Led status", "on");
			}
		}
		
		// Process PIR sensor delay
		if(mCont != 0){
			vTaskDelay(5);
			mCont--;
		}
		
		// Check ** LHINGS ACTION ** reception of "switch" action
		if(LhingsActionAvailable("switch")){
			UARTWrite(1,"Action received: switch\r\n");
			
			if(LedStatus){
				set(led, OFF);
				LedStatus = FALSE;
				// Set ** LHINGS STATUS vars. **
				// Set the Value of the Status vars. defined above in Lhings_StatusList[]
				LhingsStatusWrite("Led status", "off");
			}
			else{
				set(led, ON);
				LedStatus = TRUE;
				// Set ** LHINGS STATUS vars. **
				// Set the Value of the Status vars. defined above in Lhings_StatusList[]
				LhingsStatusWrite("Led status", "on");
			}
		}
	}
}