Exemple #1
0
void main(void)
{
	// Stop the watchdog timer, and configure the clock module.
	WDTCTL = WDTPW + WDTHOLD;
	ConfigureClockModule();

    // Initialize port pins associated with the LEDs and turn off LEDs.
    SetLEDState(LED1,OFF);
    SetLEDState(LED2,OFF);
    InitializeLEDPortPins();

    // Initialize the port pin associated with the pushbutton
    InitializePushButtonPortPin();

    // Configure timer A to generate the required interrupt.
	ConfigureTimerA();

	//Set the GIE Bit
	_BIS_SR(GIE);

	// Infinite loop
//	while(1) {
//		TOGGLE_LED1;
//		_delay_cycles(500000);
//	}

}
int ToggleLED(UserLed user_led)
{
	int ret;
	LED_State state;

	ret = GetLEDState(user_led, &state);
	if (ret!=0)
		return -1;
	return SetLEDState(user_led, !state);
}
/*******************************************************************************
*	MQTT Callback Handler
*
*	Is called when the LEd status changes ()
********************************************************************************/
int MQTTcallbackHandler(MQTTCallbackParams params)
{
	IoT_Error_t rc = NONE_ERROR;

	int ret;
	bool des_state, rep_state;
	int payload_len = (int)params.MessageParams.PayloadLen;
	char* payload = (char*)params.MessageParams.pPayload;

	//Receive desired LED state in payload
	INFO("Subscribe callback");
	INFO("%.*s\t%.*s",
				(int)params.TopicNameLen, params.pTopicName,
				(int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload);

	//Read the desired state from message
	ret = MSG_GetDesiredState( payload_len, payload, &des_state);
	if(ret !=0)
		goto JSON_ERROR;

	//Update current LED- and GPIO- state to the desired state
	INFO("Updating state: %d\n",des_state);
	SetLEDState(UserLED_4, des_state);
	Write_GPIO(GPIO12, des_state);

	//Write response message
	char msg_payload[MAX_PAYLOAD];
	payload = msg_payload;
	payload_len = MAX_PAYLOAD;

	LED_State led_state;
	GetLEDState(UserLED_4, &led_state);
	ret = MSG_SetReportedState(  payload_len, payload, (int)led_state);
	if(ret !=0)
		goto JSON_ERROR;

	char topic[512];
        sprintf(topic, led_state_pub_topic, GetMachineID());
	printf("Sending payload: %s", payload);
	rc = MQTT_Send_Message(topic, payload, strlen(payload) );
	if (NONE_ERROR != rc)
		ERROR("Could not publish new LED state to topic: %s", topic );

	return 0;

JSON_ERROR:
	ERROR("JSON format Error! %s", (char*)params.MessageParams.pPayload);
	return -1;
}
int TurnOFF_LED(UserLed user_led)
{
	return SetLEDState(user_led, LED_OFF);
}
int TurnOn_LED(UserLed user_led)
{
	return SetLEDState(user_led, LED_ON);
}