Just a little example how to use the pushover.net service in mbed and generally POST
Dependencies: EthernetInterface mbed-rtos mbed
Fork of TCPSocket_HelloWorld by
Diff: main.cpp
- Revision:
- 16:65193d8d29e6
- Parent:
- 11:59dcefdda506
diff -r 69e41cbbc8ac -r 65193d8d29e6 main.cpp --- a/main.cpp Sun Sep 21 05:55:13 2014 +0000 +++ b/main.cpp Tue Dec 09 16:34:21 2014 +0000 @@ -1,17 +1,53 @@ #include "mbed.h" #include "EthernetInterface.h" -int main() { + +/* + By Andrea Campanella + Just a small example how to use pushover.net + with mbed and generally ho to use POST in mbed. +*/ + + +int main() +{ EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); - + // Pushover settings + char pushoversite[] = "api.pushover.net"; + char apitoken[] = "APIKEY"; + char userkey [] = "USERKEY"; + char message [] = "Hello mate"; TCPSocketConnection sock; - sock.connect("mbed.org", 80); - - char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; + sock.connect(pushoversite, 80); + + char http_cmd[300] = "POST /1/messages.json HTTP/1.1\r\n" ; + char length[10]; + sprintf(length, "%d", 113 + sizeof(message)-1); + + //strcat( http_cmd , "POST /1/messages.json HTTP/1.1" ); + strcat( http_cmd , "Host: api.pushover.net\r\n"); + strcat( http_cmd , "Connection: close\r\n "); + strcat( http_cmd , "Content-Type: application/x-www-form-urlencoded\r\n"); + strcat( http_cmd , "Content-Length: "); + strcat( http_cmd , length); + strcat( http_cmd , "\r\n\r\n");; + strcat( http_cmd , "token="); + strcat( http_cmd , apitoken); + strcat( http_cmd , "&user="); + strcat( http_cmd , userkey); + strcat( http_cmd , "&message="); + strcat( http_cmd , message); + strcat( http_cmd , "&priority="); + strcat( http_cmd , "0"); + strcat( http_cmd , "&retry=60"); + strcat( http_cmd , "&expire=3600"); + printf("Sending :%s\n", http_cmd); + sock.send_all(http_cmd, sizeof(http_cmd)-1); + char buffer[300]; int ret; @@ -22,10 +58,10 @@ buffer[ret] = '\0'; printf("Received %d chars from server:\n%s\n", ret, buffer); } - + sock.close(); - + eth.disconnect(); - + while(1) {} }