Send an email

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to send an email using Flyport GPRS.

Required services

In order to send emails, at this time, the user needs an account with an email provider that allows SMTP server access without encryption.
One such service, at the time of writing, is GMX.

Introduction to data mode

To perfom any type of data communication, APN parameters should be configured. It is suggested to set APN parameters everytime Flyport GPRS resets (or wakes up from hibernation/sleep)
Connection functions (like TCP/HTTP Client connection, FTP Client file exchange, email sending) are available only when APN is set correctly,

NOTE: APN (Access Point Name) parameters are specific for each mobile operator. Please, ask to your SIM Card provider.

How to use it

This example permits to send an email pressing a button.
Flyport detects automatically button pressure, and starts all the sequence to send the email to the configured recipient address.

This tutorial shows how to create a firmware with "state machine" execution. The state machine keeps track of your code execution. In this way, if problems occurs, Flyport can automatically start from a certain state. If the email sending process go wrong for any reason (could be an incoming call or network disconnection etc...) the firmware restarts from APN setup phase.

NOTE: The following example should be configured with your own parameters (APN and email).

Flyport PRO

Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
    vTaskDelay(20);
    int i;
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5) - useful for Flyport PRO evaluation board
    UARTWrite(1,"Flyport Task Started...\r\n");
 
    //Wait for GSM Connection successfull
    do
    {
	UpdateConnStatus();
	while(LastExecStat() == OP_EXECUTION);
    }while((LastConnStatus() != REG_SUCCESS)&&(LastConnStatus() != ROAMING));
 
    vTaskDelay(20);
    UARTWrite(1,"Flyport registered on network!\r\n");
 
    // Initialize p7 as digital input with internal pullup resistor
    IOInit(p7, inup);
 
    int actionState = 0; // 0: State Machine in idle state...
 
    while(1)
    {	
	vTaskDelay(20);
	IOPut(p31, toggle);
 
	// Poll Button input state...
	if(IOGet(p7) == off)
	{
            UARTWrite(1, "Button Pressed!\r\n");
	    if(actionState == 0)
		actionState++;	
	}
 
	switch(actionState)
	{
	    // Do not execute any operation, IDLE STATE
	    case 0:
		// Put here IDLE code execution (if needed)
		break;
 
	    // Step #1: Setup APN Params
	    case 1:
		UARTWrite(1, "setup APN params\r\n");
		APNConfig("apn.domain.fly","usernameAPN","passwordAPN",DYNAMIC_IP,DYNAMIC_IP,DYNAMIC_IP);
		vTaskDelay(20);
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
	            actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;	
 
	    // Step #2: Set SMTP parameters:
	    case 2:
		UARTWrite(1, "setup SMTP params\r\n");
		SMTPParamsSet("mail.gmx.com", 25, "sender@gmx.com", "username", "pass");
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		    actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;
 
	    // Step #3: Set email destinations
	    case 3:
		UARTWrite(1, "setup destination\r\n");
		SMTPEmailTo("destination@email.fly", SMTP_NO_DEST, SMTP_NO_DEST, SMTP_NO_DEST);
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
	        // Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		    actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;
 
	    // Step #4: Send email
	    case 4:
	        UARTWrite(1, "sending email...");
		SMTPEmailSend("Email from Flyport GPRS", "Flyport GPRS can send email using data connection!\r\nRegards");
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "not sent!\r\nFlyport will try again soon\r\n");
		    actionState = 1; // Repeat from case 1
		}
		else
		{
		    UARTWrite(1, "Email sent!\r\n");
		    actionState = 0; // Restart application loop
		}
		break;
 
	}
    }
}


Flyport

Bill of Materials

  • 1x Flyport GPRS Module
  • 1x miniUSB programmer
  • 1x Power Supply
  • 1x Proto Nest Board
  • 1x Button


Schematic

ButtonInput p7 inup.png


Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
void FlyportTask()
{
    vTaskDelay(20);
    UARTWrite(1,"Flyport Task Started...\r\n");
 
    //Wait for GSM Connection successfull
    do
    {
	UpdateConnStatus();
	while(LastExecStat() == OP_EXECUTION);
    }while((LastConnStatus() != REG_SUCCESS)&&(LastConnStatus() != ROAMING));
 
    vTaskDelay(20);
    UARTWrite(1,"Flyport registered on network!\r\n");
 
    // Initialize p7 as digital input with internal pullup resistor
    IOInit(p7, inup);
 
    int actionState = 0; // 0: State Machine in idle state...
 
    while(1)
    {	
	vTaskDelay(20);
	IOPut(p21, toggle);
 
	// Poll Button input state...
	if(IOGet(p7) == off)
	{
            UARTWrite(1, "Button Pressed!\r\n");
	    if(actionState == 0)
		actionState++;	
	}
 
	switch(actionState)
	{
	    // Do not execute any operation, IDLE STATE
	    case 0:
		// Put here IDLE code execution (if needed)
		break;
 
	    // Step #1: Setup APN Params
	    case 1:
		UARTWrite(1, "setup APN params\r\n");
		APNConfig("apn.domain.fly","usernameAPN","passwordAPN",DYNAMIC_IP,DYNAMIC_IP,DYNAMIC_IP);
		vTaskDelay(20);
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
	            actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;	
 
	    // Step #2: Set SMTP parameters:
	    case 2:
		UARTWrite(1, "setup SMTP params\r\n");
		SMTPParamsSet("mail.gmx.com", 25, "sender@gmx.com", "username", "pass");
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		    actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;
 
	    // Step #3: Set email destinations
	    case 3:
		UARTWrite(1, "setup destination\r\n");
		SMTPEmailTo("destination@email.fly", SMTP_NO_DEST, SMTP_NO_DEST, SMTP_NO_DEST);
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
	        // Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		    actionState = 1; // Repeat from case 1
		else
		    actionState++; // execute next step
		break;
 
	    // Step #4: Send email
	    case 4:
	        UARTWrite(1, "sending email...");
		SMTPEmailSend("Email from Flyport GPRS", "Flyport GPRS can send email using data connection!\r\nRegards");
		while(LastExecStat() == OP_EXECUTION)
		    vTaskDelay(20);
		// Choose next step:
		if(LastExecStat() != OP_SUCCESS)
		{
		    UARTWrite(1, "not sent!\r\nFlyport will try again soon\r\n");
		    actionState = 1; // Repeat from case 1
		}
		else
		{
		    UARTWrite(1, "Email sent!\r\n");
		    actionState = 0; // Restart application loop
		}
		break;
 
	}
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox