Send email from Pic32 suggestions

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
Stuartk
Posts: 54
Joined: 12 Jan 2013 04:17

Send email from Pic32 suggestions

#1 Post by Stuartk » 27 Jan 2015 17:27

I'm trying to find a way to send an email directly from a Pic32MX795. I'm presently using mikroBasic Pro for Pic 32. I'm definitely limited by my lack of knowledge about network protocols and I was wondering if anyone has done this and is willing to share guidance or code. The main page of my current creation is pictured:
photo4.jpg
photo4.jpg (75.75 KiB) Viewed 3921 times
It senses the differential pressure across a filter and then triggers an alarm. It would be really nice if it sent an email also. Sending a text is not that practical. My understanding is that here in Canada, it would require a Sim card and an cell phone account which are not free. Also the notification for this issue is not time critical. It's not as if your freezer has lost power and your $100,000 of food is your grocery store is melting. This notification can wait a few days.

I was able to get the Ethernet example with the EasyPic Fusion working but I don't know how to take it further. I found a couple of other ways to send an email. This particular module would do the job, however the documentation is somewhat poor and it is less then straightforward to use:

http://www.netburner.com/products/seria ... et/sb70-lc

Another simpler and cheaper option would just be to connect to a GPIO on a Raspberry Pi. (Likely the whole project can be done on a Pi)
I was hoping that there was a MikroElektronika solution. Remote notification is an important issue to add to the ME toolbox.

Thanks for your help,
Stuart

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Send email from Pic32 suggestions

#2 Post by marina.petrovic » 28 Jan 2015 14:13

Hi,

Unfortunately, we do not have some similar project/example which can help you.

You can take a look at this forum topics (there you can find some useful suggestions):
http://www.mikroe.com/forum/viewtopic.php?t=8518
https://www.mikroe.com/forum/viewtopic.php?f=97&t=28508

Also, you can search our forum and LibStock Website, maybe some of our users posted their projects which can be helpful.

Best regards,
Marina

Stuartk
Posts: 54
Joined: 12 Jan 2013 04:17

Re: Send email from Pic32 suggestions

#3 Post by Stuartk » 28 Jan 2015 14:57

Hi Marina,

I had already seen this post from 4 years ago. I was hoping that there was some new information or suggestions since then. I also looked through the Libstock code to no avail.

It seems the only current solution to send an email, is to use an outside platform such as Netburner, Arduino or Raspberry Pi.

I hope that MikroElektronika or a bright developer investigates this issue further and adds it, as it is a critical missing piece of functionality.

Thanks,

Stuart

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Send email from Pic32 suggestions

#4 Post by marina.petrovic » 29 Jan 2015 13:42

Hi,

I will certainly pass your suggestion to my colleagues from Software Department
and they will consider to implement some solution.

Best regards,
Marina

Stuartk
Posts: 54
Joined: 12 Jan 2013 04:17

Three ways to send an email from Pic32

#5 Post by Stuartk » 08 Mar 2015 04:12

Hello,

I've been able to send an email from my embedded project using the Pic32MX795 in 3 different ways. Although none of them are particularly elegant, they will all work until a better method comes along from MikroElektronika.


1. Use IFTTT with the Spark Core/Photon


Sparkio is a new startup that sells WiFi and soon cellphone modules for embedded projects. These modules are really quite nice, and so far have worked quite well for me.

https://store.spark.io/
https://www.spark.io/

They have a built in STM32 ARM Cortex M3 so they can do a lot more then the typical Wifi module.

You connect the module to your router using a cellphone app.
http://docs.spark.io/assets/images/spark-app.jpg

You program the module online on their site and flash it. You even can store your programs online on your account so you can modify them from anywhere.
I recommend reading the startup page:
http://docs.spark.io/start/

Spark is working on sending emails directly from their modules. For now you have to use an external site.

IFTTT (If this then that)
https://ifttt.com/

This is a site that many of you have already seen. It allows you to set up simple web based triggers. There are numerous tutorials on the Spark forum.

Essential you are setting up: If Spark (such as pressing button connecting to your Spark, or having a pin go low on your spark) then Send Email (from your gmail account)
This is called a "recipe", you set it up on your IFTTT account

This code for the Spark sends an email defined in your IFTTT account when the button connected to pin D1 is brought low:

int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state
int led = D7; // integrated LED

void setup() {

pinMode(inputPin, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); //read the state of the push-button

if (val == LOW) { //if push-button pressed
state = !state; //reverse on/off state
delay(250); //primitive button debounce
Serial.println("button pushed!");
digitalWrite(led, HIGH); // Turn ON the LED pins
notification();
}
}

void notification()
{
delay(1000);
Spark.publish("button1",NULL, 60, PRIVATE);
//
Serial.println("Complete");
//
digitalWrite(led, LOW); // Blink LED
delay(250);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
}



The IFTTT site has tremendous possibilities. For example, there are tutorials on the Spark forum that teach you how to sense the temperature of a remote DS1820 and paste the readings into a spreadsheet on your computer from your embedded device. This would be difficult if not impossible for the non-professional without IFTTT.

MikroElektronika would be well advised to become a publisher on IFTTT to link directly from it's compilers.

Pro's: Inexpensive module at $19
Tremendously helpful forum that is highly responsive to your needs and will help you without an indifferent or surly attitude
You have to use a wifi module anyways, it may as well be theirs
Upcoming Spark P0, surface mount module for $10

Con's: Still a work in progress
Cheaper modules are available if you're building a million of something
May be difficult for an end consumer to set up an IFTTT account and Recipe if you are bulding a consumer product


2. Using PushingBox from Spark Photon


https://www.pushingbox.com/

Pushingbox is quite simple to use. You set up a scenario which can allow you to email,text, tweet all at the same time if you want. The scenario is triggered by a unique code called a 'devid' that you paste into the Spark modules code.

The following code sends an request to PushingBox when pin D1 on the Spark module is brought low.In this case it tells PushingBox to send an email. Just paste your devid in the correct spot and it will work as is:

int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state
int led = D7; // integrated LED

TCPClient client;
char reply[512];

void setup() {

pinMode(inputPin, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop() {
val = digitalRead(inputPin); //read the state of the push-button

if (val == LOW) { //if push-button pressed
state = !state; //reverse on/off state
delay(250); //primitive button debounce
Serial.println("button pushed!");
digitalWrite(led, HIGH); // Turn ON the LED pins
notification();
}
}

void notification()
{
if (client.connect("api.pushingbox.com", 80))
{
out("GET /pushingbox?devid=vC12345678ABCD HTTP/1.1\r\n"); //Put your unique devid here
out("Host: api.pushingbox.com\r\n");
out("User-Agent: Spark/1.0\r\n");
out("Content-Type: text/html\r\n");
out("Connection: close\r\n\r\n");
//DEBUG_PRINTLN("Closing Connection...");
in(reply, 3000);
Serial.println("Complete");
//
digitalWrite(led, LOW); // Blink LED
delay(250);
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
}
else{
Serial.println("connection failed");
digitalWrite(led, LOW); // Pulse LED Quickly
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
delay(100);
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
}
//
}

void out(const char *s)
{
client.write( (const uint8_t*)s, strlen(s) );
//Serial.println("Writing");
}

void in(char *ptr, uint8_t timeout)
{
int pos = 0;
unsigned long lastTime = millis();
while( client.available()==0 && millis()-lastTime<timeout)
{
} //do nothing
lastTime = millis();
unsigned long lastdata = millis();
while ( client.available() || (millis()-lastdata < 500))
{
if (client.available())
{
char c = client.read();
//DEBUG_PRINTLN(c);
lastdata = millis();
ptr[pos] = c;
pos++;
}
if (pos >= 512 - 1)
break;
}
ptr[pos] = '\0'; //end the char array
while (client.available()) client.read(); // makeshift client.flush()
client.flush(); //for safety
delay(400);
client.stop();
}


3. Using the Raspberry Pi


This simple python script will send an email from the Raspberry Pi when GPIO22 is set high:


#-------------Setup------------------------------

import RPi.GPIO as GPIO
import time
import smtplib
GPIO.setwarnings(False)

#-----------Define Pins--------------------------

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.OUT)
GPIO.setup(22,GPIO.IN)

state = True

#----------------Define Constants-----------------
smtpUser = 'you@gmail.com'
smtpPass = 'your gmail password'

toAdd1 = 'send@email.com' # The email that you want to send to
toAdd2 = 'anotheremailtosendto@email.com'

fromAdd = 'This Email is From' #smtpUser

subject = 'The subject of your email'
header = 'To: ' + toAdd1 + '\n' + 'From: ' + fromAdd + '\n' + 'Subject: ' + subject
body = 'Body of your email'

#----------------Functions------------------------
#*************************************************

def Flash_Activity_Led():
GPIO.output(4,True)
time.sleep(0.25)
GPIO.output(4,False)
time.sleep(0.25)

def Send_First_Email():
print(header + '\n' + body)
s= smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpUser, smtpPass)
s.sendmail(fromAdd, toAdd1, header + '\n\n' + body)
#.................................................
#
while True:
input22 = GPIO.input(22) # the current input at GPIO 22
Flash_Activity_Led() # Shows that the program is active
if input22 == 1: # If the input is high
print("The button has been pressed")
Send_First_Email()


Pros: Very robust
Huge base of information available, mature platform
You probably only need to use the Raspberry Pi and nothing else

Con's:
Kludgy
More expensive
Only really usable for a one off project, like a unique monitor in an industrial setting

Cheers to all,

Stuart

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Send email from Pic32 suggestions

#6 Post by marina.petrovic » 09 Mar 2015 16:13

Hi,

Thank you very much for sharing your solutions and detailed explanation with our users.
Many of them will certainly find it useful.

Best regards,
Marina

Post Reply

Return to “mikroBasic PRO for PIC32 General”