Can anyone help me!

I have been trying make my imp send a monthly email report of what a button does for nearly a year now and i cant get it to work. All i want it to do is send what is in the log. I have had around 340 builds. I have tried mailgun and thing speak. Is there anyone out there that can help me out. it is driving me crazy and I am about to give up on it all

Hi Josh,

patience is the key here, i know its frustrating and the best way imho is to get the logging to a remote working before playing with the data u want.
Did u use the examples the wizards here provided?
Check out the bearded scripts of your choice (link) and it helped me to understand things better how and whats needed to send data to a 3th party.
I used xively and it works excellent.
Once all is logging to a 3th party (mail,pubnub, xively) then play with the variables u want.

(tip: start fresh with the on board impdata to post like hardware.voltage(),imp.scanwifinetworks() enz. so u wont have to hook up anything to get data posted on the 3th party)

repo bearded here : https://github.com/electricimp/reference/tree/master/webservices

Once u get the data flowing, u will forget about the other 340 builds :slight_smile:

Patch

https://discourse.electricimp.com/discussion/3318/electric-imp-email-monthly-report#Item_5

Also making multiple topics about the same thing isn’t that nice.

But did you try the code I gave you the link to?

are you having trouble getting mailgun message to go through?

or are you having trouble building a report to send?

Hi Mikey Dk yes i tried the code before it works but it just sends text not the pin values.

local url = format(“https://api:%s@%s%s/messages&quot”, API_KEY, MAILGUN_URL, MAILGUN_DOMAIN);

I think I have to change the &quot but I am not sure what it is

Hi mjkuw94 if i do a easier code i can just send an email like just saying hi but if I try to get the imp values i have this problem
local url = format(“https://api:%s@%s%s/messages&quot”, API_KEY, MAILGUN_URL, MAILGUN_DOMAIN);

I think I have to change the &quot but I am not sure what it is

Hi Patch Yes I am using the examples thank god. I am using mailgun have you used it before? I can make simple contections like send an email when the imp is turned on but to send the imp values i have a problem with the code see in the above post. Any Ideas

sorry, I don’t understand that single line of code either. The example I found does not have that.

here is the code I found from the above links

Agent code

`
function mailgun(subject, message)
{
local from = "imp@no-reply.com";//from - I don’t know if this is important
local to = "youremail@hotmail.com";//put your email ‘to’ here

local apikey = “key-4se8s46e84f8as6e84se86s4e8f64se8f6”;//put your values here for apikey
local domain = “sandboxs648fase8q48a4se83fa4sd8f34s.mailgun.org”;//put your values here for domain

local request = http.post(“https://api:” + apikey + "@api.mailgun.net/v2/" + domain + “/messages”, {“Content-Type”: “application/x-www-form-urlencoded”}, “from=” + from + “&to=” + to + “&subject=” + subject + “&text=” + message);

local response = request.sendsync();
server.log("Mailgun response: " + response.body);
}

server.log(messagetosend);//some message to send, defined elsewhere

mailgun(“my imp”, messagetosend);//where messagetosend is a string that you supply
`

and here are some code stubs that can show how to save to permanent storage on the imp agent and also how to retrieve values from the same storage. You still need to write code to get this permanent storage appended without writing it from scratch each time the agent boots.

`
permanentstorage <- {
settings = {},
mymessage = “”
};

server.save(permanentstorage);

permanentstorage.mymessage = permanentstorage.mymessage + “new log message to add
”;
permanentstorage.mymessage = permanentstorage.mymessage + “a second new log message to add” + “
”;

server.save(permanentstorage);

local messagetosend = server.load().mymessage;
server.log(messagetosend);

mailgun(“my imp”, messagetosend);//

`

Maybe you already have the code that builds up your message and so you can ignore this last bit.

Josh,

Would you like a button press sent to a website database (using PHP) each time it is pressed? That way, your data report would be generated on a web page (from your database) regardless of the imp.

A button is pressed (or any pin is made active), the agent sends the pin number to a PHP API (script that takes the value and writes to a database, or text file on your website).

If the imp dies or something goes bad, you’ll still have all of your data in the database.

If you or any friends have a website, that site would be used as your database. A PHP script will reside on the website to receive the data and store it. A different script would be written to create reports, charts, graphs, from the database of button presses (including timestamps, etc).

Other than the imp cloud, you would be in control of all your data.

If you don’t have a website of your own, or access to one, let me know. I’ll host it on mine for a while so you can at least test it out. All I would need to know is what your agent will be sending (POST JSON). Perhaps a value of a button, whether you send button 1, button 2 or button 3. With that, I’ll give you the URL in which to POST the data.

I agree with @mlseim about logging to a database so it’s never lost. You might consider GroveStreams. It’s pretty easy and there are some examples on their site to get an imp up and running–logging data. They also have some patent pending data aggregation capabilities that may help with the monthly reporting via text or email.

Hi mlseim Sorry for the late reply Yeah if its still possible Id like to try your idea this is the way I tried to do it at the start but could get it to work. I don’t have a website for it as yet. here is my agent

Hi Mlseim I have just sent you an email. Thanks and sorry for the late reply

If you’re comfortable with it, you could post your agent (and device) code here so everyone can see what you’re trying to do and offer feedback. The link in your message goes to your (I’m assuming) agent, but there does not appear to be a http.onrequest() handler identified to take action on receipt the those messages.

If you’re having trouble understanding the example(s) at Grovestreams for logging data, you should reach out to them (forum or email). I know they are eager to help clarify how to interact with their system.

josh … I’ll work on a sample for you (using my website) this weekend.
remove your agent URL from your previous post so people don’t see your URL.
Email me so I can have your email address: info@catpin.com

You can also pursue the suggestion hvacspei has. Just know that you can get your own shared webhost account and domain name for $50-100 per year. Cleverdot.com offers the first year for $40 including domain name. You might find having a website opens-up so many other imp/website applications.

Hi hvacspei
Yes you are right in that I dont have a http.onrequest() but i can still make it email just not the pin value.

Device code:
`button <- hardware.pin1;

function buttonPress()
{
local state = button.read();
if (state == 1)
{
// The button is released
server.log(“Release”);
} else
{
// The button is pressed
server.log(“Press”);
}
}`

Agent code:
`// Configure the button to call buttonPress() when the pin’s state changes
button.configure(DIGITAL_IN_PULLUP, buttonPress);

function mailgun(subject, message)
{
local from = "imp@no-reply.com";
local to = "joshreid76@hotmail.com"

local apikey = "i have taken my key out ";
local domain = “I have taken my local domain out”;

local request = http.post(“https://api:” + apikey + "@api.mailgun.net/v2/" + domain + “/messages”, {“Content-Type”: “application/x-www-form-urlencoded”}, “from=” + from + “&to=” + to + “&subject=” + subject + “&text=” + message);

local response = request.sendsync();
server.log("Mailgun response: " + response.body);
}
mailgun(“Electric Imp”, “Just saying hi! :)”);

function buttonPress() {
local state = button.read();
if (state == 1) {
mailgun(“my imp”, “The door has released.”);//where messagetosend is a string that you supply
server.log(“release”);
} else {
mailgun(“my imp”, “The door is pressed.”);//where messagetosend is a string that you supply
server.log(“press”);
}

}
permanentstorage <- {
settings = {},
mymessage = “”
};

server.save(permanentstorage);

permanentstorage.mymessage = permanentstorage.mymessage + “open
”;
permanentstorage.mymessage = permanentstorage.mymessage + “close” + “
”;

server.save(permanentstorage);

local messagetosend = server.load().mymessage;
server.log(messagetosend);

mailgun(“my imp”, messagetosend);//`

Device Log
2014-12-19 20:47:55 UTC+10 [Device] Press 2014-12-19 20:47:55 UTC+10 [Device] Release

I can make the imp send an email but not the pin value like what is in the device log. Ideally I would like the pin values with time stamp sent to email on a monthly report or like what mlseim is going to help me with having it live on a site and then i can email it from there i believe. I will have a look into grove stream more as well. Thanks so much everyone for your help and any help would be much appreciated.

Can you submit your code again, but with the device and agent code separated and each wrapped with the “C” tags shown above? Your code is a bit of a challenge to understand as it’s listed above. The code tags will make it easier to spot issues.

With that said, the agent can not directly read the status of a button. You need to send the button status to the agent via an “agent.send()” call. In the agent code, you’ll define what function is to be called when that message is received. You do this by way of a “device.on()” call. You would want to pass the status of the button in the “agent.send()” call. I think if you look that direction, it will become clear as to how the agent can receive the button’s status and send it via mailgun.

Again, please separate the agent and device code and I’m sure we can get you straightened out. What you’re trying to do is relatively easy. That being said, I would reiterate my suggestion to use a service such as GroveStreams (or whatever you prefer). That way you could have a log of exactly when the button was pressed each time and aggregate to your heart’s content.

@joshreid - I wrapped your code in `</ code> tags to make it a bit more readable.

Something I found out recently is that the agent.send function can only send one object. I needed to send 2 pieces of data with the function and had to modify my code to send an array as the second object. Not a big deal, but it was a hiccup I encountered because I had not read the documentation before modifying my code.

Once the agent code has the data (button status), it will be easy to have it add the timestamp and email the status and time to any address (using mailgun).

You can also send a table, which lets you structure your data a bit more:

device code:
// send a table with various bits of information agent.send("tempData", { timestamp = time(), temperature = 90, humidity = 0.5 });

agent code:
device.on("tempData", function(tempData) { server.log("Temperature: " + tempData.temperature); server.log("Humidity: " + tempData.humidity); });

Referring to this example:
https://electricimp.com/docs/api/http/jsonencode/

If you have your own website (webhost account), you can send any data you wish to a PHP script and have your data saved on a server, or whatever you want to do with it.

From that example, line 4 would contain the URL plus a key:
local url = “https://www.mywebsite.com/?mykey=PT4u8EwGt3mNoqW”;

The PHP script will read-in the JSON array (decode it) and also GET that key. It will make sure it gets the correct key before doing anything.

Your PHP script on your website … simple demo to write data into a .csv file that can be opened with Excel. Send it as many JSON values as you wish. It’s an array.

`

<?php // Set your default timezone // This will be used to write a timestamp to your CSV file. date_default_timezone_set('America/Los_Angeles'); // Retrieve POST JSON from Imp $data = json_decode(file_get_contents('php://input'), true); // Retrieve API KEY if(isset($_GET['mykey'])){ $mykey=$_GET['mykey']; } if($mykey=="PT4u8EwGt3mNoqW"){ // Write button press to .csv file $myFile = "ImpButtons.csv"; $fh = fopen($myFile, 'a+') or die("can't open file"); $now=time(); foreach($data as $line){ $line=$line.",".$now.",".date('Y-m-d h:i:s',$now); fwrite($fh, $line."\ "); } fclose($fh); } ?>

`

And also know that PHP could save the data on the server, and also email it anywhere as plain text, or as a file attachment. You could capture data, save it, and pass it along to another API service.