Pushover sketch to send pushover messages from the arduino

Created this sketch to send pushover.net messages directly from the arduino

Tought I share:

/*

   Pushover sketch by M.J. Meijer 2014

   Send pushover.net messages from the arduino

*/

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

byte mac[] = {0xDE,0xAC,0xBF,0xEF,0xFE,0xAA};

// Pushover settings
char pushoversite[] = "api.pushover.net";
char apitoken[] = "your30characterapitokenhere12";
char userkey [] = "your30characteruserkeyhere123";

int length;

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());

  delay(5000);
  Serial.println("Ready");
}

void loop()
{
  pushover("OMG, Yes it works!!!");  
  delay(60000); 
}

byte pushover(char *pushovermessage)
{
  String message = pushovermessage;

  length = 81 + message.length();

  if(client.connect(pushoversite,80))
  {
    client.println("POST /1/messages.json HTTP/1.1");
    client.println("Host: api.pushover.net");
    client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(length);
    client.println("\r\n");;
    client.print("token=");
    client.print(apitoken);
    client.print("&user=");
    client.print(userkey);
    client.print("&message=");
    client.print(message);
    while(client.connected())  
    {
      while(client.available())
      {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
  }  
}

so basicly in your own code you just need this function:

byte pushover(char *pushovermessage)
{
  String message = pushovermessage;

  length = 81 + message.length();

  if(client.connect(pushoversite,80))
  {
    client.println("POST /1/messages.json HTTP/1.1");
    client.println("Host: api.pushover.net");
    client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(length);
    client.println("\r\n");;
    client.print("token=");
    client.print(apitoken);
    client.print("&user=");
    client.print(userkey);
    client.print("&message=");
    client.print(message);
    while(client.connected())  
    {
      while(client.available())
      {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
  }  
}

and then call the function with:

pushover("whatever message you want!");

you could also add the message priority to the function, by adding:

byte pushover(char *pushovermessage, int priority)

and change lenght = 81 to lenght = 93

and add this to the function:

client.print("&priority=");
    client.print(priority);

then you call the function with:

pushover("whatever message",0); // change 0 to 1 or 2 for different message priorities

enjoy!

Redcell,

Thank you for posting such a thorough example!

It works like a charm.

Take care,

Chad

Your welcome

here is another usefull example to use variables in a pushover message for example to send temperature reading for example from a DHT22 sensor:

Text[40];

char temp_str[8];

dtostrf(TemperatureSensor, 4, 1, temp_str); // convert float value from temperature sensor reading to a string, where TemperatureSensor is a variable defined from a temperature sensor value

sprintf(Text,"The current temperature is now %s°",temp_str);

pushover(Text,0); // Only with the 0 or 1 or 2 for message priority if you changed the above code to include the message priority, otherwise it would just be: pushover(Text);

so the TemperatureSensor must be defined before in the code for example:

 TemperatureSensor = dht1.readTemperature();

I also put the sketch on github: GitHub - avenantsoftware/PushoverArduino: Send Pushover messages from the arduino

You can also use pushover to replace the Serial.print function

for example when you want to know when your arduino starts put this in the: void setup():
somewhere at the bottom after everything else started:

pushover("The arduino started");

or if you use the message priority:

pushover("The arduino started",0);

Hi. Thanks for the info. And it works great as it is posted. I just have one question for you. When I change the parts of the code to make it do the priority (specifically the part about the length). The arduino freezes up for about 45 seconds and does not display anything from after that in the function to the serial port and sends no push. It just starts back after the function in the main loop. Any ideas?

could you post your code?

Yea sure. I had it in a much bigger sketch first but when it froze i put it on an uno exactly like this and i get the same thing. But it to works great if you leave out the priority it even works if you leave everything for priority in except the length part and being that Ethernet is still new to me i don’t really know what is going on there.
Thanks for your help.

/*

   Pushover sketch by M.J. Meijer 2014

   Send pushover.net messages from the arduino

*/

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

byte mac[] = {0xDE,0xAC,0xBF,0xEF,0xFE,0xAA};

// Pushover settings
char pushoversite[] = "api.pushover.net";
char apitoken[] = "MYAPI";
char userkey [] = "MYKEY";

int length;

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());

  delay(5000);
  Serial.println("Ready");
}

void loop()
{
  pushover("OMG, Yes it works!!!",0);  
  delay(60000); 
}

byte pushover(char *pushovermessage, int priority)
{
  String message = pushovermessage;

  length = 93 + message.length();

  if(client.connect(pushoversite,80))
  {
    client.println("POST /1/messages.json HTTP/1.1");
    client.println("Host: api.pushover.net");
    client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(length);
    client.println("\r\n");;
    client.print("token=");
    client.print(apitoken);
    client.print("&user=");
    client.print(userkey);
    client.print("&message=");
    client.print(message);
    client.print("&priority=");
    client.print(priority);
    while(client.connected())  
    {
      while(client.available())
      {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
  }  
}

sorry for the late replay, but I am verry bussy

I created the priority part purely theoraticly, not tested it myself...

but now I have..

I looked at the pushover api and it requires more parameters..

so the code shoudl look like this:

byte pushover(char *pushovermessage, int priority)
{
  String message = pushovermessage;

  length = 113 + message.length();

  if(client.connect(pushoversite,80))
  {
    client.println("POST /1/messages.json HTTP/1.1");
    client.println("Host: api.pushover.net");
    client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.print(length);
    client.println("\r\n");;
    client.print("token=");
    client.print(apitoken);
    client.print("&user=");
    client.print(userkey);
    client.print("&message=");
    client.print(message);
    client.print("&priority=");
    client.print(priority);
    client.print("&retry=60");
    client.print("&expire=3600");
    while(client.connected())  
    {
      while(client.available())
      {
        char ch = client.read();
        Serial.write(ch);
      }
    }
    client.stop();
  }  
}

60 and 3600 means, the message is send every minute for an hour long, if you change these values you aslo have to change the lenght value accordingly

once it works, you can also comment out this code:

while(client.connected())  
    {
      while(client.available())
      {
        char ch = client.read();
        Serial.write(ch);
      }
    }

that code is to see output back from the pushover server in serial monitor, if you get a status 200 code, the message was sent succesfully

Thanks I will try that today and post results. Also I have been reading on the pushover page and I see that there are more variables that can be sent. I believe I understand how the send messages work. But back to the length that I don't really understand so my question is how did you calculate that. I am sorry if this has been covered already but I think thats my only problem

the lenght is determined by the lenght of the url you are sending

what you are sending is an url, the lenght in the code is the total lenght of the url in characters

the url your are sending is 113 characters + a variable of the message lenght, because the message lenght changes per message

if you add new parameters to the url

you add them up

so if you where to add for example the following parameters from the pushover api, to send a message to a specific device ( a Nexus7 in this example):

&device=Nexus7 (14 chars)

then the lenght becomes: 113 + 14 = 127

"token=123456789012345678901234567890&user=123456789012345678901234567890&message=" = 81 chars

by adding "&priority=0&retry=60&exprire=3600" the lenght becomes 113

by adding "&device=Nexus7" the lenght becomes 127

etc...

Thank you very much all works great.

Hello,

i am from Germany and i want to try to send a message to a pushover group. but it doesn´t work? can you help me?

Erromessage:

X-Runtime: 0.009438

c8
{"errors":["messages to users other than the creator of this application ("API-NAME") must be sent over SSL to https://api.pushover.net/"],"status":0,"request":"c448befc856a8efe707c581770c742f4"}
0

or when it doesn´t work, is there an onter programm like pushover?

I was trying this piece of code on my Arduino NANO with an enc28j60 ethernet shield but it doesn't seems to work... Am I doing something wrong??

This is what I have in the serial monitor:

Starting ethernet...

That's all :-s

Any thoughs to adapt this sketc to “inetGSM.h” library to be used with SIM900 gsm shields?