Difference between revisions of "XPicoWiFi/ModemEmulation"

From Lantronix Wiki!
Jump to navigation Jump to search
Line 13: Line 13:
 
This example reads an accelerometer on a Freescale FRDM-26 board, then sends the three axis data to dweet.io with the name of the "thing" being the MAC address of the xPico Wi-Fi.
 
This example reads an accelerometer on a Freescale FRDM-26 board, then sends the three axis data to dweet.io with the name of the "thing" being the MAC address of the xPico Wi-Fi.
  
The code is tested on the FRDM-26 board running the MQX-LITE RTOS with CodeWarrior 10.6.
+
The code is tested on the FRDM-26 board running the MQX-LITE RTOS with CodeWarrior 10.6. See the [[XPicoWiFi/CommandLineStatusExample|Command Line Status example]] for the definition of the getXSR(char[] buffer) function to retrieve the MAC Address of the xPico Wi-Fi.
  
 
  <nowiki>
 
  <nowiki>

Revision as of 16:17, 6 April 2015

Overview

The Modem Emulation feature allows you to use familiar AT commands from an attached microcontroller to control the connections of the xPico Wi-Fi.

The documentation for Modem Emulation can be found in the User Guide on Chapter 10.

Example: Send HTTP data, decide server at run-time

The following example sends HTTP data to a server. While it could use the Tunnel if the server was known ahead of time, there are two reasons to use Modem Emulation in this example:

  • Use ATD0 to enter the Command Line Interface to get status data to send to the server
  • Decide on the server address at run-time, rather than having to pre-configure it

This example reads an accelerometer on a Freescale FRDM-26 board, then sends the three axis data to dweet.io with the name of the "thing" being the MAC address of the xPico Wi-Fi.

The code is tested on the FRDM-26 board running the MQX-LITE RTOS with CodeWarrior 10.6. See the Command Line Status example for the definition of the getXSR(char[] buffer) function to retrieve the MAC Address of the xPico Wi-Fi.


void runApp(void) {
	char buffer[1024];
	
	LEDG_On();
	serialInit();
	InitAccelMagnetometer();
	  
	WAIT1_Waitms(3000);

        // Use ATD0 to get into CLI and put the serial number of the xPico Wi-Fi
        // into buffer
	getXSR(buffer);
	
	while (TRUE) {
		FX1_GetRaw8XYZ(&xyz[0]);

                // Make a connection to dweet.io and port 80 for the HTTP packet

		snprintf(buffer, 200, "ATD dweet.io:80\r");
		SendString(buffer, &deviceData);
		waitPostResponse(10,500, buffer, 1024);
		
		if (strstr(buffer,"CONNECT") != NULL) {
			LEDG_Off();
			LEDR_On();
			createDweetMessage(buffer);
			SendString(buffer, &deviceData);
			waitPostResponse(10,500, buffer, 1024);
			WAIT1_Waitms(500);
					
                        // Use +++ to switch to command mode
			
			snprintf(buffer, 200, "+++");
			SendString(buffer, &deviceData);
			FlushBuf();
			waitPostResponse(3,100, buffer, 1024);
			if (strstr(buffer, "NO CARRIER") != NULL) {
				LEDR_Off();
				LEDG_On();
			}
			
                        // Hangup the connection
			
			snprintf(buffer, 200, "ATH\r");
			SendString(buffer, &deviceData);
			FlushBuf();
			waitPostResponse(3,50, buffer, 1024);
			if (strstr(buffer, "NO CARRIER") != NULL) {
				LEDR_Off();
				LEDG_On();
			}
		}
		WAIT1_Waitms(100);
	}
}

int waitPostResponse(int interchar, int totalTO, char buffer[], int size)
{
	int timeout = 0;
	int received = 0;
	unsigned char c;

	while(TRUE)
	{
		if (RxBuf_NofElements()!=0)
		{
			timeout = 0;
			(void)RxBuf_Get(&c);
			if (received < size) {
				snprintf(buffer,size,"%s%c",buffer,c);
			}
			received += 1;
		} else {
			if ( (received == 0 && timeout > totalTO) || (received != 0 && timeout > interchar)) {
				break;
			} else {
				timeout += 1;
				WAIT1_Waitms(20);
			}
		}
	}
	return received;
}

int createDweetMessage(char postBuffer[]) {
	char headerString[] =
		"GET /dweet/for/%s?x_axis=%d&y_axis=%d&z_axis=%d HTTP/1.1\r\n"
		"Host: dweet.io\r\n"
		"Connection: keep-alive\r\n"
		"Accept: */*\r\n"
		"\r\n";


	return snprintf(postBuffer, 1024, headerString, uid, xyz[0],xyz[1],xyz[2]);
}