Last active
August 29, 2015 14:14
-
-
Save guglielmino/7f4044217e3d212d046f to your computer and use it in GitHub Desktop.
A sample to use Pushetta API from Arduino with Ethernet shield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
///////////////// | |
// MODIFY HERE // | |
///////////////// | |
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 }; // Be sure this address is unique in your network | |
char APIKEY[] = "ffaabb444f1177772222f7243534badaanaa1100"; // Put here your API key | |
char CHANNEL[] = "my channel"; // and here your channel name | |
boolean DEBUG = true; | |
////////////// | |
// End // | |
////////////// | |
char serverName[] = "api.pushetta.com"; | |
boolean lastConnected = false; // State of the connection last time through the main loop | |
EthernetClient client; | |
void setup() { | |
Serial.begin(9600); | |
// start the Ethernet connection: | |
Serial.println("Init Ethernet Shield"); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
while(true); // no point in carrying on, so do nothing forever | |
} | |
else{ | |
Serial.println("Ethernet ready"); | |
Serial.print("My IP address: "); | |
Serial.println(Ethernet.localIP()); | |
} | |
delay(1000); // give the Ethernet shield a second to initialize: | |
} | |
void loop() | |
{ | |
sendToPushetta(CHANNEL, "Hello world!"); | |
delay(60000); | |
} | |
//Function for sending the request to Pushetta | |
void sendToPushetta(char channel[], String text){ | |
client.stop(); | |
if(DEBUG){Serial.println("connecting...");} | |
if (client.connect(serverName, 80)) | |
{ | |
if(DEBUG){Serial.println("connected");} | |
if(DEBUG){Serial.println("sending request");} | |
client.print("POST /api/pushes/"); | |
client.print(channel); | |
client.println("/ HTTP/1.1"); | |
client.print("Host: "); | |
client.println(serverName); | |
client.print("Authorization: Token "); | |
client.println(APIKEY); | |
client.println("Content-Type: application/json"); | |
client.print("Content-Length: "); | |
client.println(text.length()+46); | |
client.println(); | |
client.print("{ \"body\" : \""); | |
client.print(text); | |
client.println("\", \"message_type\" : \"text/plain\" }"); | |
client.println(); | |
} | |
else { | |
if(DEBUG){Serial.println("connection failed");} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment