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
main.cpp@16:65193d8d29e6, 2014-12-09 (annotated)
- Committer:
- emuboy
- Date:
- Tue Dec 09 16:34:21 2014 +0000
- Revision:
- 16:65193d8d29e6
- Parent:
- 11:59dcefdda506
Just a small example how to use the pushover.net service and generally POST in mbed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 2 | #include "EthernetInterface.h" |
donatien | 0:bb128f0e952f | 3 | |
emuboy | 16:65193d8d29e6 | 4 | |
emuboy | 16:65193d8d29e6 | 5 | /* |
emuboy | 16:65193d8d29e6 | 6 | By Andrea Campanella |
emuboy | 16:65193d8d29e6 | 7 | Just a small example how to use pushover.net |
emuboy | 16:65193d8d29e6 | 8 | with mbed and generally ho to use POST in mbed. |
emuboy | 16:65193d8d29e6 | 9 | */ |
emuboy | 16:65193d8d29e6 | 10 | |
emuboy | 16:65193d8d29e6 | 11 | |
emuboy | 16:65193d8d29e6 | 12 | int main() |
emuboy | 16:65193d8d29e6 | 13 | { |
donatien | 0:bb128f0e952f | 14 | EthernetInterface eth; |
donatien | 0:bb128f0e952f | 15 | eth.init(); //Use DHCP |
donatien | 0:bb128f0e952f | 16 | eth.connect(); |
emilmont | 2:e087e9b789e9 | 17 | printf("IP Address is %s\n", eth.getIPAddress()); |
emuboy | 16:65193d8d29e6 | 18 | // Pushover settings |
emuboy | 16:65193d8d29e6 | 19 | char pushoversite[] = "api.pushover.net"; |
emuboy | 16:65193d8d29e6 | 20 | char apitoken[] = "APIKEY"; |
emuboy | 16:65193d8d29e6 | 21 | char userkey [] = "USERKEY"; |
emuboy | 16:65193d8d29e6 | 22 | char message [] = "Hello mate"; |
emilmont | 7:65188f4a8c25 | 23 | TCPSocketConnection sock; |
emuboy | 16:65193d8d29e6 | 24 | sock.connect(pushoversite, 80); |
emuboy | 16:65193d8d29e6 | 25 | |
emuboy | 16:65193d8d29e6 | 26 | char http_cmd[300] = "POST /1/messages.json HTTP/1.1\r\n" ; |
emuboy | 16:65193d8d29e6 | 27 | char length[10]; |
emuboy | 16:65193d8d29e6 | 28 | sprintf(length, "%d", 113 + sizeof(message)-1); |
emuboy | 16:65193d8d29e6 | 29 | |
emuboy | 16:65193d8d29e6 | 30 | //strcat( http_cmd , "POST /1/messages.json HTTP/1.1" ); |
emuboy | 16:65193d8d29e6 | 31 | strcat( http_cmd , "Host: api.pushover.net\r\n"); |
emuboy | 16:65193d8d29e6 | 32 | strcat( http_cmd , "Connection: close\r\n "); |
emuboy | 16:65193d8d29e6 | 33 | strcat( http_cmd , "Content-Type: application/x-www-form-urlencoded\r\n"); |
emuboy | 16:65193d8d29e6 | 34 | strcat( http_cmd , "Content-Length: "); |
emuboy | 16:65193d8d29e6 | 35 | strcat( http_cmd , length); |
emuboy | 16:65193d8d29e6 | 36 | strcat( http_cmd , "\r\n\r\n");; |
emuboy | 16:65193d8d29e6 | 37 | strcat( http_cmd , "token="); |
emuboy | 16:65193d8d29e6 | 38 | strcat( http_cmd , apitoken); |
emuboy | 16:65193d8d29e6 | 39 | strcat( http_cmd , "&user="); |
emuboy | 16:65193d8d29e6 | 40 | strcat( http_cmd , userkey); |
emuboy | 16:65193d8d29e6 | 41 | strcat( http_cmd , "&message="); |
emuboy | 16:65193d8d29e6 | 42 | strcat( http_cmd , message); |
emuboy | 16:65193d8d29e6 | 43 | strcat( http_cmd , "&priority="); |
emuboy | 16:65193d8d29e6 | 44 | strcat( http_cmd , "0"); |
emuboy | 16:65193d8d29e6 | 45 | strcat( http_cmd , "&retry=60"); |
emuboy | 16:65193d8d29e6 | 46 | strcat( http_cmd , "&expire=3600"); |
emuboy | 16:65193d8d29e6 | 47 | printf("Sending :%s\n", http_cmd); |
emuboy | 16:65193d8d29e6 | 48 | |
emilmont | 11:59dcefdda506 | 49 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
emuboy | 16:65193d8d29e6 | 50 | |
emilmont | 7:65188f4a8c25 | 51 | |
emilmont | 9:4757a976148d | 52 | char buffer[300]; |
donatien | 0:bb128f0e952f | 53 | int ret; |
emilmont | 7:65188f4a8c25 | 54 | while (true) { |
emilmont | 9:4757a976148d | 55 | ret = sock.receive(buffer, sizeof(buffer)-1); |
emilmont | 7:65188f4a8c25 | 56 | if (ret <= 0) |
emilmont | 7:65188f4a8c25 | 57 | break; |
emilmont | 9:4757a976148d | 58 | buffer[ret] = '\0'; |
emilmont | 9:4757a976148d | 59 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
emilmont | 7:65188f4a8c25 | 60 | } |
emuboy | 16:65193d8d29e6 | 61 | |
emilmont | 7:65188f4a8c25 | 62 | sock.close(); |
emuboy | 16:65193d8d29e6 | 63 | |
emilmont | 7:65188f4a8c25 | 64 | eth.disconnect(); |
emuboy | 16:65193d8d29e6 | 65 | |
emilmont | 9:4757a976148d | 66 | while(1) {} |
donatien | 0:bb128f0e952f | 67 | } |