Gateway is not sending e-mail...

Started by Lensdigital, September 28, 2015, 03:46:49 PM

Lensdigital

I setup event to play sound and send e-mail if my mailbox sensor (it's actually based on reed switch and two magnets so there's almost 0 power loss because magnets placed on opposite sides of reed switch so when door is closed reed switch is pulled by both magnets in opposite directions and thus off. When door opens magnet attached to it, moves away and switch closes circuit).
Anyway, sound alert works perfect, but there's no e-mail being sent out :(
I configured settings.js with my gmail credentials (app access code).
In metrics.js I have these entries:

in export.metrics
Mbox_open : { name:'MBox', regexp:/MBOX\:O/i, value:'MBox Opened', pin:1, graph:1, logValue:1, graphOptions:{ legendLbl:'Mailbox', lines: { show:false, fill:false }, points: { show: true, radius: 5, lineWidth:1 }, grid: { backgroundColor: {colors:['#000', '#03c', '#08c']}}, yaxis: { ticks: 0 }}},

and in export.events

mailboxEmail : { label:'Mailbox Open : Email', icon:'mail', descr:'Send email when mailbox is opened', serverExecute:function(node) { if (node.metrics['MBox'] && node.metrics['MBox'].value == 'Mbox Opened' && (Date.now() - new Date(node.metrics['MBox'].updated).getTime() < 2000)) { sendEmail('MAILBOX OPENED', 'Mailbox Opened NODE: [' + node._id + ':' + node.label + '] @ ' + (new Date().toLocaleTimeString() + (new Date().getHours() > 12 ? 'PM':'AM'))); }; } },


Checking my Google app password shows that it hasn't been triggered even once...
What am I doing wrong? Is there some log files I can check? :)

detroit_johnny

Yes you can see if the gateway actually attempted to send the email out or not. 

Under the /home/pi/moteino directory have a look at the gateway.sys.log file and scroll to the bottom for the most recent entries. 

If it's a success, you will see an entry like this:
[09-28-15_19:15:31.647] [LOG] SENDSMS SUCCESS: 250 2.0.0 OK 1332482131 94sm9675254iop.31 - gsmtp

In GMail try to tell Google to lower the security level, visit https://myaccount.google.com/u/1 and then click on "Connected Apps & Sites" on the left side and then find the setting "Allow less secure apps" and turn that slider to ON.  See if that does the trick. 

In the settings.js file I'm only using the fields Felix provided and able to send my gmail account mail to itself as well as SMS to my Verizon phone by using the format <your phone #>@vtext.com

Lensdigital

Thanks Detroit_johnny!

Unfortunately I don't have gateway.sys.log file anywhere :(
Also I tried going to Google settings you suggested, but it's unavialble to me :(
This setting is not available for accounts with 2-Step Verification enabled

I just tried SMS event, and it also didn't work...  I wish there was some way to test just e-mail portion without gateway...

detroit_johnny

@Lensdigital, glad to help and hope we can get this working for you.

For my gmail, I created a brand new account there exclusively for this email task.  Therefore I don't have 2-step verification enabled either.  It makes my account more "hacker-friendly" but I never plan to share the email address with anyone, and it's a very long email name.  No one will likely ever guess it esp since it's just for me  :-)  So try a new account.

I'm using Felix's v6 image file on my Pi, maybe that's why I have the log file since I didn't do anything to invoke it. 

I looked around a little bit and on my Pi, in the directory /home/pi there is a file called 'gateway.conf' and inside that file it mentions the gateway.sys.log file.  Here is the code in that file

#!upstart
description "Moteino gateway.js home automation socket server"
author      "[email protected]"

start on startup
stop on shutdown

# Automatically Respawn/restart:
respawn
respawn limit 10 50

script
    export HOME="/root"
    echo $$ > /var/run/gateway.pid
    #execute node on the gateway.js script, redirect console output to log file
    exec sudo -u pi /opt/node/bin/node /home/pi/moteino/gateway.js >> /home/pi/moteino/gateway.sys.log 2>&1
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /home/pi/moteino/gateway.sys.log
end script

pre-stop script
    rm /var/run/gateway.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /home/pi/moteino/gateway.sys.log
end script

Lensdigital

#4
Ah, I know why I don't have it :) I never enabled auto-start and didn't install upstart yet...
Gotta try it. BTW great idea about separate e-mail...

Edit: Ok got autostart working and have gateway.sys.log file.
But when I trigger mailbox even, nothing shows up in the log :(

Kilo95

i'm having the same problem with getting messages to send from my sonarmote. ive set up an app specific password with gmail. gateway.sys.log does not show it was triggered even though i have the metrics.js set to where it should.

detroit_johnny

@Lensdigital if you don't see an event posted in the gateway.sys.log file then it means your event format is not working for one reason or another.  Could be on your metric.js side or on your moteino side.  You might want to put the moteino back on your computer and monitor the serial port to see what kind of messages it is sending out, and ensure the gateway is seeing what is expected. 

Lensdigital

Quote from: detroit_johnny on October 01, 2015, 10:10:39 PM
@Lensdigital if you don't see an event posted in the gateway.sys.log file then it means your event format is not working for one reason or another.  Could be on your metrics.js side or on your moteino side.  You might want to put the moteino back on your computer and monitor the serial port to see what kind of messages it is sending out, and ensure the gateway is seeing what is expected.
It's gotta be metric.js (I posted my config in first post). I can seem mailbox events just fine on webpage, and even get Audio notification...
Will try to figure it out...

LaneF

I'll throw my Hat in the Ring !!

I've been working on simple email and SMS for over a week now with no glimmer of light.

I've read the post about changing my Settings.js to smtp.gmail.com and Port 465 (I think) and the metrics.js Transport section, tried that.  Looked at my Gateway.sys.log file in the Moteino directory, it hasn't been modified since Sept 24th. 

I went in Google and "turned on" my allow apps to email function.  That didn't do anything.

I saw Felix's reply to a link, honestly that link doesn't have enough detail to trouble shot.   It linked to me to an example page on Github.  I came across a GUI that may help but it appears to be for Windows and I'm using a MAC.

Is there a page I can refer to that walks you through the expected, correct, setup and the syntax for each event.  Be it Motion, Mailbox, GarageMote, Sonar or whatever.  I get you have to work out the bugs, and do some research to find your solution, but the lack of reference material is what's making it tough.

LaneF

Felix

Lane,
It's not much to it actually, there's this section of code in gateway.js:

var transporter = nodemailer.createTransport({
    service: settings.credentials.emailservice, //"gmail" is preconfigured by nodemailer, but you can setup any other email client supported by nodemailer
    auth: {
        user: settings.credentials.email,
        pass: settings.credentials.emailpass,
    }
});


The user and pass are imported from another file, settings.js:
exports.credentials = {
  emailservice : 'gmail',                  //default is gmail, see nodemailer reference for other clients - https://github.com/andris9/Nodemailer
  email : '[email protected]',           //put your gmail address here
  emailpass : '_____________',             //put your gmail password or app access code here
  emailAlertsTo : '[email protected]',  //put your alert/notification email here (can be the same as above)
  smsAlertsTo : '[email protected]',  //if you want SMS notifications, fill this in with your phone number (it's your cell#, domain differs for each carrier, ex: [email protected] for verizon)
};

You have to fill in email and emailpass for the email function to work. If you want SMS then find out the email address associated with your phone from your mobile carrier, it's usually just your number@yourcarrier type of address.
There are also email alerts, so if you want to send yourself or someone else an email for certain events instead of an SMS, then fill in emailAlertsTo.

For GMAIL you can create an app password - I created a password just for my gateway at this link, and that bypasses 2-step verification if you have it setup. Then fill that in the emailpass field above, that's it, should work. Remember to restart your gateway, you can pkill node and the process monitor will restart it or if you're in doubt do a full sudo reboot.

Lensdigital

Felix, is there a way to stimulate mail/sms event without actual live node? I.e. exact Serial string I can send to Pi along with matching setting string in metrics file? This will help everyone to troubleshoot issue and at least discount email settings as being culprit.:-)

Felix

You mean simulate?
Send email/sms without sending it?
But how would that work?
You can replace the default events I defined with whatever you'd like .. for instance you could send a LOG message to the log window on the UI I guess...

Lensdigital

Quote from: Felix on October 22, 2015, 08:49:57 AM
You mean simulate?
Send email/sms without sending it?
But how would that work?
You can replace the default events I defined with whatever you'd like .. for instance you could send a LOG message to the log window on the UI I guess...
Sorry I should've been more clear. Don't think complex, think simple test. Let's say I just setup my Raspberry Pi with all the software and apps, and I just want to see if gateway.js can test e-mail or SMS (but send it to real recipient). I just want to test that part, nothing else.  So I attach Arduino Uno or Moteino or whatever with simpler simple sketch that just does
void setup () {
Serial.begin (115200);
Serial.println ("PROPERLY FORMATTED STRING");
}


So once Gateway sees this message over serial it sends me e-mail and SMS.

Can you provide such example (both "PROPERLY FORMATTED STRING") and lines in the metric.js so it can happen? 

LaneF

Felix,

I went into Google and my two step verification is in fact turned off.

I think I'll try it with the email my cable provider Comcast set up for me and see it there server will work.   It would be nice since I don't use that account for anything except to collect SPAM.

Thanks for your tips -- I'll keep working away at it.

LaneF

Felix

Quote from: Lensdigital on October 22, 2015, 10:26:40 AM
Quote from: Felix on October 22, 2015, 08:49:57 AM
You mean simulate?
Send email/sms without sending it?
But how would that work?
You can replace the default events I defined with whatever you'd like .. for instance you could send a LOG message to the log window on the UI I guess...
Let's say I just setup my Raspberry Pi with all the software and apps, and I just want to see if gateway.js can test e-mail or SMS (but send it to real recipient). I just want to test that part, nothing else.  So I attach Arduino Uno or Moteino or whatever with simpler simple sketch that just does
void setup () {
Serial.begin (115200);
Serial.println ("PROPERLY FORMATTED STRING");
}


So once Gateway sees this message over serial it sends me e-mail and SMS.
Can you provide such example (both "PROPERLY FORMATTED STRING") and lines in the metric.js so it can happen?

So you want to send an email with gateway.js running. Look at the motionEmail event in metrics.js. That sends an email when a "MOTION" metric is received from a Moteino. You can of course fake that by sending "[NodeId] MOTION" to your Pi where a [NodeId] has the motionEmail event active.

If you want something custom like send email when you send a magic string over serial that's something you'll have to add on your own.