Mark's Blog on HC-SR04 Alarm via Email

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Mark's Blog on HC-SR04 Alarm via Email

#1 Post by MarkIngle » 19 Feb 2016, 03:52

I thought it would be a good idea to post the progress on my latest project here. My project is to create an interface for entering an email address on the ESP Config page and using that email address to send alerts when a sensor is tripped. I will be using a HC-SR04 sensor that I already have working. I will document all of my struggles as I only know the basics of programming, etc. Hopefully this will help someone like others have helped me!

Here is a high level plan:

1. Get HC-SR04 working
2. Learn to compile the ESP Easy Code and flash the ESP
3. Add the ability to enter an email address in the config page
4. Include the needed header files to support email function
5. Send a hard coded email address to personal email account
6. Send email to address entered in the config page
7. Send email based on sensor mode change from 1 to 0/above a certain threshold.
8. Brag to peers at work!
Last edited by MarkIngle on 19 Feb 2016, 05:03, edited 3 times in total.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: HC-SR04 Alarm via Email Blog

#2 Post by MarkIngle » 19 Feb 2016, 04:06

Step 2 ---

Compiling the ESP code is not too hard but you need to make sure that you pull the code and the libraries that are specific to the bundle. This bundle is currently located on sourceforge

https://sourceforge.net/projects/espeas ... p/download

Download this zip file and make sure that it will compile in Arduino. Once the compile completes look for the location that the bin file was created....here is mine output after a successful compile..

C:\Users\MULTIR~1\AppData\Local\Temp\build5193875245038748295.tmp/ESPEasy.cpp.bin -bm dio -bf 40 -bz 4M -bs .text -bp 4096 -ec -eo C:\Users\MULTIR~1\AppData\Local\Temp\build5193875245038748295.tmp/ESPEasy.cpp.elf -bs .irom0.text -bs .text -bs .data -bs .rodata -bc -ec

The ESPEasy.cpp.bin is your new bin file that can be flashed on to the ESP8266 via the ESP12E dev board or a separate UART

Next you need to get the bin file flashed on to the ESP8266. To do this you need to use the flash tool created by the devs....thank you Mr. Dev!

Below are the input values I used and the resulting flash........take a close look at my copy command. Particularly the filename copied to...

C:\ESPEasyRoot>copy c:\Users\MultirotorGCS\AppData\Local\Temp\build5193875245038748295.tmp\ESPEasy.cpp.bin ESPEasy_RIng_4096.bin /Y
1 file(s) copied.

C:\ESPEasyRoot>flash
Comport (example 3, 4, ..) :8
Flash Size (example 512, 1024, 4096) :4096
Build (example 71, 72, ..) :Ing
Using com port: 8
Using bin file: ESPEasy_RIng_4096.bin
esptool v0.4.6 - (c) 2014 Ch. Klippel <ck@atelier-klippel.de>
setting board to nodemcu
setting baudrate from 115200 to 115200
setting port from COM1 to COM8
setting address from 0x00000000 to 0x00000000
espcomm_upload_file
stat ESPEasy_RIng_4096.bin success
setting serial port timeouts to 1000 ms
opening bootloader
resetting board
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
read 0, requested 1
trying to connect
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_send_command: receiving 2 bytes of data
espcomm_open
Uploading 356848 bytes from ESPEasy_RIng_4096.bin to flash at 0x00000000
erasing flash
size: 0571f0 address: 000000
first_sector_index: 0
total_sector_count: 88
head_sector_count: 16
adjusted_sector_count: 72
adjusted_size: 048000
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
setting serial port timeouts to 10000 ms
setting serial port timeouts to 1000 ms
espcomm_send_command: receiving 2 bytes of data
writing flash
....................................................................................................................................
....................................................................................................................................
starting app without reboot
espcomm_send_command: sending command header
espcomm_send_command: sending command payload
espcomm_send_command: receiving 2 bytes of data
closing bootloader
flush start
setting serial port timeouts to 1 ms
setting serial port timeouts to 1000 ms
flush complete
Press any key to continue . . .
Last edited by MarkIngle on 19 Feb 2016, 04:35, edited 1 time in total.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: HC-SR04 Alarm via Email Blog

#3 Post by MarkIngle » 19 Feb 2016, 04:17

To explain the 'flash' command..... You will be prompted for several values(input).

Comport - The comport you set for Arduino probably but definiely the comport you have your ESP connected to.
Flash size - This is the size you set in the Arduino IDE....Tool>Flash Size....my is 4M or 4096
Build (example...77,78) - The file name I created is called ESPEasy_RIng_4096.bin. The flash tool was written to read the bin files in the current directory and it looks for a unique file name based on the input value you give. Note the "Ing" in the file name I created. The flash tool will look for ESPEasy_Rxxx_4096.bin. xxx = whatever you included in the "to" filename in the copy command.
Last edited by MarkIngle on 19 Feb 2016, 04:37, edited 1 time in total.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: HC-SR04 Alarm via Email Blog

#4 Post by MarkIngle » 19 Feb 2016, 04:34

Step 3 -----

First I need to add a variable to hold the email address entered. I choose to add to the SettingsStruct (some lines have been removed in this post) that is located in the ESPEasy.ino file

struct SettingsStruct
{
unsigned long PID;
int Version;
byte Unit;
byte Controller_IP[4];
unsigned int ControllerPort;
...
...
...
float TaskDevicePluginConfigFloat[TASKS_MAX][PLUGIN_CONFIGFLOATVAR_MAX];
long TaskDevicePluginConfigLong[TASKS_MAX][PLUGIN_CONFIGLONGVAR_MAX];
char emailaddress[26];

Next I added the emailaddress variable to the code for the webpage and input. Also in the ESPEasy.ino file

//********************************************************************************
// Web Interface config page
//********************************************************************************
void handle_config() {
if (!isLoggedIn()) return;

char tmpString[64];

String name = WebServer.arg("name");
String password = WebServer.arg("password");
..
...
....
String apkey = WebServer.arg("apkey");
String emailaddress = WebServer.arg("emailaddress");
..
...
....
espsubnet.toCharArray(tmpString, 26);
str2ip(tmpString, Settings.Subnet);
Settings.Unit = unit.toInt();

emailaddress.toCharArray(tmpString, 26);
urlDecode(tmpString);
strcpy(Settings.emailaddress, tmpString);


SaveSettings();
..
...
....
reply += F("'><TR><TD>ESP Subnet:<TD><input type='text' name='espsubnet' value='");
sprintf_P(str, PSTR("%u.%u.%u.%u"), Settings.Subnet[0], Settings.Subnet[1], Settings.Subnet[2], Settings.Subnet[3]);
reply += str;

reply += F("'><TR><TD>Email Address:<TD><input type='text' name='emailaddress' value='");
reply += Settings.emailaddress;


reply += F("'><TR><TD><TD><input class=\"button-link\" type='submit' value='Submit'>");
reply += F("</table></form>");
addFooter(reply);
WebServer.send(200, "text/html", reply);
}

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: HC-SR04 Alarm via Email Blog

#5 Post by MarkIngle » 19 Feb 2016, 05:00

Below is the result.......
ESPEmailfield.PNG
ESPEmailfield.PNG (30.15 KiB) Viewed 17303 times

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#6 Post by MarkIngle » 19 Feb 2016, 05:20

Step 4 -----

The following header files need to be added to the ESPEasy.ino file:

#include <SPI.h>
#include <Ethernet.h>

The first time I complied with these headers I received the following compile error:

In file included from C:\Users\MultirotorGCS\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\Ethernet\src/Dhcp.h:7:0,
from C:\Users\MultirotorGCS\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\Ethernet\src/Ethernet.h:9,
from ESPEasy.ino:164:
C:\Users\MultirotorGCS\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\Ethernet\src/EthernetUdp.h:42:0: warning: "UDP_TX_PACKET_MAX_SIZE" redefined [enabled by default]
#define UDP_TX_PACKET_MAX_SIZE 24
^
In file included from ESPEasy.ino:156:0:
C:\Users\MultirotorGCS\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries\ESP8266WiFi\src/WiFiUdp.h:28:0: note: this is the location of the previous definition
#define UDP_TX_PACKET_MAX_SIZE 8192



Apparently the variable UDP_TX_PACKET_MAX_SIZE is defined twice in two different header files. This will not wok so I decided to rename the variable in the EthernetUdp.h file since its "new" to the ESP code base. I made the following changes in the EthernetUdp.h file:


//#define UDP_TX_PACKET_MAX_SIZE 24

#define UDP_TX_PACKET_MAX_SIZE_EMAIL 24 //Modified to support email SMTP integration with ESP8266


The "//" turns the following text into a comment. When modifying code I never remove statements. Its safer to just comment them out so that they dont compile. Also if the "wheels fall off" you can remove the // to get things working again.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Mark's Blog on HC-SR04 Alarm via Email

#7 Post by tozett » 19 Feb 2016, 08:20

hi,
at first there is to say that i am no dev and therefore no help in programming details here,
but may you want to consider some design- and architectural references for your project?

why dont you do an "EMAIL"-plugin?
the devs made room for extensions via plugins,
http://www.esp8266.nu/forum/viewtopic.php?f=6&t=718

so you dont have to fiddle in the source, and you can participate from furter updates.
everythins of your plugin functionalty is supported in future ...

you are extending a sensor node with more functionality.
there is a discussion of limitation of the ESP to do this here.
for more features directly on the ESP, a plugin can be a good way, at least

otherwise emailing via backends (node-red, domoticz, raspberry-pi...) is the ususual way

next: may some dev could help you coding..., sorry... :oops:

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#8 Post by MarkIngle » 19 Feb 2016, 13:33

How do I learn the plugin architecture?

Your suggestion sounds reasonable but I'm not sure where to start

Martinus

Re: Mark's Blog on HC-SR04 Alarm via Email

#9 Post by Martinus » 19 Feb 2016, 14:17

I made a email test plugin some time ago, when I stumbled on a simple SMTP sample on the internet. Actually just for fun to see if the code worked and could be squeezed into a plugin.

The plugin stores the config and sends a test email when I hit the submit button.
email.png
email.png (8.75 KiB) Viewed 17268 times
But that's all it does... I never finished it because I had no use for it on a sensor device. (And Domoticz can send me a notification if I would ever need it).
Second limitation could be that this plugin only supports plain SMTP without authentication.

Having an e-mail send feature is one thing, next challenge would be "how to use it".

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#10 Post by MarkIngle » 19 Feb 2016, 15:12

It would be useful for me to ....Email me when my garage door opens between working hours?!??

Send an email when my tool box is opened

I can think of several more. These are situations where it's only for a short period of time. When I'm fishing it would be great to know if someone opens the rod box on the boat...when my dogs crate gets to a certain temp
Last edited by MarkIngle on 20 Feb 2016, 01:21, edited 1 time in total.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#11 Post by MarkIngle » 19 Feb 2016, 21:49

This is tough. I cannot find a library that will work with the ESP!

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Mark's Blog on HC-SR04 Alarm via Email

#12 Post by tozett » 19 Feb 2016, 22:04

you are in deep problems with emailing directly on the esp....
:?

namirda
Normal user
Posts: 53
Joined: 22 Jan 2016, 17:09

Re: Mark's Blog on HC-SR04 Alarm via Email

#13 Post by namirda » 19 Feb 2016, 22:28

Hi Mark,

I think a better approach would be to use the mqtt facilities of ESP Easy to output data from the SR04 and then use MQTTWarn to send the email/push notifications or whatever else you need. MQTTWarn works really well - it is very flexible and can be configured easily with simple python scripts. I discovered it only recently and can recommend it. It adds simplicity, flexibility and scalability if the notifications part of an alarm system is kept well separate from the sensors!

N

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#14 Post by MarkIngle » 20 Feb 2016, 00:18

Does your recommendation require a broker?

namirda
Normal user
Posts: 53
Joined: 22 Jan 2016, 17:09

Re: Mark's Blog on HC-SR04 Alarm via Email

#15 Post by namirda » 20 Feb 2016, 00:40

Hi Mark,

Yes it does. Mosquitto is a common choice and is pretty painless to set up.

N

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#16 Post by MarkIngle » 20 Feb 2016, 00:56

I agree. But I try to architect solutions with minimum service dependencies. In short, the more parts that make up a software solution the more opportunity for breakage and failure. Just a personal thing. I'm gonna grab some dinner and try a few more things. I'll let you know but I sense the ESP wifi libraries may not support what I'm after. This is just a statement not a request. The platform is just fine as is.

CORRECTION: the wifi libraries may support what I'm after but I do not understand how to add the code. :)

Martinus

Re: Mark's Blog on HC-SR04 Alarm via Email

#17 Post by Martinus » 20 Feb 2016, 11:50

Do you need a library for this? When using a plain smtp server it's not much more than this communication sample:

Code: Select all

      if (Plugin_104_MTA(client, "",                                                   "220 ", 10 * SECs ) == false) break;
      if (Plugin_104_MTA(client, "EHLO " + aDomain,                                    "250 ", 10 * SECs ) == false) break;
      if (Plugin_104_MTA(client, "MAIL FROM:" + aFrom + "",                            "250 ", 10 * SECs ) == false) break;
      if (Plugin_104_MTA(client, "RCPT TO:" + aTo + "",                                "250 ", 10 * SECs ) == false) break;
      if (Plugin_104_MTA(client, "DATA",                                               "354 ", 10 * SECs ) == false) break;
      if (Plugin_104_MTA(client, "Subject:" + aSub + "\r\n\r\n" + aMesg + "\r\n.\r\n", "250 ", 10 * SECs ) == false) break;
It's plain text send using a TCP client socket.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#18 Post by MarkIngle » 22 Feb 2016, 02:45

Yeah that makes sense...guess I am complicating things! Where can I find the 104 plug-in reference in the code sample?

I am not making any progress at all and I have other ideas I would like to try out.

andy
Normal user
Posts: 65
Joined: 26 Jan 2016, 23:07

Re: Mark's Blog on HC-SR04 Alarm via Email

#19 Post by andy » 15 Apr 2016, 01:45

Any movement on this yet? I'd like to see it implemented also. I can Tweet a message via ThingSpeak, but I'd prefer something not reliant on a third party if possible.

MarkIngle
Normal user
Posts: 47
Joined: 10 Feb 2016, 05:06

Re: Mark's Blog on HC-SR04 Alarm via Email

#20 Post by MarkIngle » 15 Apr 2016, 04:29

No movement on this at all. There is no library that supports emailing from the ESP8266 using the Arduino IDE. At least not that I could find. I used OpenHAB to get it working. Sorry!!

Martinus

Re: Mark's Blog on HC-SR04 Alarm via Email

#21 Post by Martinus » 15 Apr 2016, 17:21

andy wrote:Any movement on this yet? I'd like to see it implemented also. I can Tweet a message via ThingSpeak, but I'd prefer something not reliant on a third party if possible.
There's a demo email plugin in the plugin playground, but this is just to show how email could be done using plain SMTP on port 25.
You may be able to adopt it to your specific needs.

tozett
Normal user
Posts: 734
Joined: 22 Dec 2015, 15:46
Location: Germany

Re: Mark's Blog on HC-SR04 Alarm via Email

#22 Post by tozett » 05 Sep 2016, 20:52

as i surfed the web i found an smtp-lib and an demo for smtp for the esp8266 here:
https://gist.github.com/igrr/8f1b4a5771 ... e94c4c108d

i checked againt the playground-plugin,
https://github.com/ESP8266nu/ESPEasyPlu ... l_Demo.ino
which seems to do this by hand, so maybe, :roll:
if someone stumble upon this here: above the link for a lib...

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests