Created
January 9, 2016 10:56
-
-
Save Oliv4945/90a24612998153e7ae0d to your computer and use it in GitHub Desktop.
Example to send data from an Arduino to http://ioPush.net
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
/* | |
Web client sketch for IDE v1.0.1 and w5100/w5200 | |
Uses POST method. | |
Posted November 2012 by SurferTim | |
*/ | |
/* | |
* Modified the 09/01/2016 to send data to http://ioPush.net | |
*/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
byte mac[] = { | |
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
// Server configuration | |
char serverName[] = "iopush.net"; | |
int serverPort = 80; | |
char pageName[] = "/app/api/post"; | |
// Auth token | |
char authToken[] = "YourAuthToken"; | |
EthernetClient client; | |
int totalCount = 0; | |
// insure params is big enough to hold your variables | |
char params[28]; | |
// set this to the number of milliseconds delay | |
// this is 30 seconds | |
#define delayMillis 30000UL | |
unsigned long thisMillis = 0; | |
unsigned long lastMillis = 0; | |
void setup() { | |
Serial.begin(9600); | |
// disable SD SPI | |
pinMode(4,OUTPUT); | |
digitalWrite(4,HIGH); | |
Serial.print(F("Starting ethernet...")); | |
if(!Ethernet.begin(mac)) Serial.println(F("failed")); | |
else Serial.println(Ethernet.localIP()); | |
delay(2000); | |
Serial.println(F("Ready")); | |
} | |
void loop() | |
{ | |
// If using a static IP, comment out the next line | |
Ethernet.maintain(); | |
thisMillis = millis(); | |
if(thisMillis - lastMillis > delayMillis) | |
{ | |
lastMillis = thisMillis; | |
// ******* Set data to post ********** | |
sprintf(params,"{\"body\": \"Total count : %i\", \"badge\": \"I\", \"push\"; \"True\"}",totalCount); | |
if(!postPage(serverName,serverPort,pageName,params, authToken)) Serial.print(F("Fail ")); | |
else Serial.print(F("Pass ")); | |
totalCount++; | |
Serial.println(totalCount,DEC); | |
} | |
} | |
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData, char* authToken) | |
{ | |
int inChar; | |
char outBuf[64]; | |
Serial.print(F("connecting...")); | |
if(client.connect(domainBuffer,thisPort) == 1) | |
{ | |
Serial.println(F("connected")); | |
// send the header | |
sprintf(outBuf,"POST %s HTTP/1.1",page); | |
client.println(outBuf); | |
sprintf(outBuf,"Host: %s",domainBuffer); | |
client.println(outBuf); | |
client.println(F("Connection: close\r\nContent-Type: application/json")); | |
sprintf(outBuf,"authentication_token: %s",authToken); | |
client.println(outBuf); | |
sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData)); | |
client.println(outBuf); | |
// send the body (variables) | |
client.print(thisData); | |
} | |
else | |
{ | |
Serial.println(F("failed")); | |
return 0; | |
} | |
int connectLoop = 0; | |
while(client.connected()) | |
{ | |
while(client.available()) | |
{ | |
inChar = client.read(); | |
Serial.write(inChar); | |
connectLoop = 0; | |
} | |
delay(1); | |
connectLoop++; | |
if(connectLoop > 10000) | |
{ | |
Serial.println(); | |
Serial.println(F("Timeout")); | |
client.stop(); | |
} | |
} | |
Serial.println(); | |
Serial.println(F("disconnecting.")); | |
client.stop(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment