Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sketch isn't very robust. The HTTP Post to pushingbox seems to get hungup sometimes because
- client.stop() isn't regularly executed
- */
- #include <Ethernet.h>
- #include <SPI.h>
- #include "Tokens.h" // holds DEVID_FTTEST
- uint32_t time = 0;
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- byte ip[] = { 192, 168, 216, 50 }; // IP address of Ethernet Shield
- char serverName[] = "api.pushingbox.com";
- char url[] = "/pushingbox";
- EthernetClient client; // create a client that connects to Google
- boolean lastConnected = false; // State of the connection last time through the main loop
- byte temp[4]; // hold dummy data
- void setup()
- {
- Serial.begin(9600);
- delay(1000);
- Serial.println(F("Begin Setup"));
- Ethernet.begin(mac, ip);
- delay(1000); // give the Ethernet shield a second to initialize
- Serial.print(F("My IP address: "));
- Serial.println(Ethernet.localIP());
- }
- void loop()
- {
- static byte cnt;
- int inChar;
- int connectLoop = 0;
- if ( millis() > time + 30000)
- {
- client.stop();
- if( client.connect(serverName, 80) )
- {
- Serial.println(F("Client Connected :)"));
- // create some dummy test data
- temp[0] = cnt++;
- temp[1] = 61;
- temp[2] = 92;
- temp[3] = (byte) millis();
- char sensorTemp[10];
- char feedData[150];
- strcpy(feedData, DEVID_FTTEST);
- for(int i=0; i<4; i++)
- {
- sprintf(sensorTemp, "&T%d=%d", i+1, temp[i]);
- strcat(feedData, sensorTemp);
- }
- postRequest(serverName, url, feedData);
- time = millis();
- }
- else
- {
- Serial.println(F("Client connection failed :("));
- client.stop();
- }
- }
- // print the response from server
- // Should get HTTP/1.1 200 OK
- if (client.available())
- {
- char c = client.read();
- Serial.print(c);
- }
- // if there's no net connection, but there was one last time
- // through the loop, then stop the client:
- if (!client.connected() && lastConnected)
- {
- Serial.println(F("disconnecting..."));
- client.stop();
- }
- lastConnected = client.connected();
- }
- void postRequest(char *hostName, char *url, String feedData)
- {
- String buf = "POST " + String(url) + " HTTP/1.1";
- Serial.println(F("Data sent to server: "));
- Serial.println(buf);
- Serial.println("Host: " + String(hostName));
- Serial.println("Content-Type: application/x-www-form-urlencoded");
- Serial.println("Content-Length: " + String(feedData.length()));
- Serial.println("");
- Serial.println(feedData);
- Serial.println("");
- Serial.println("");
- client.println(buf);
- client.println("Host: " + String(hostName));
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.println("Content-Length: " + String(feedData.length()));
- client.println("");
- client.println(feedData);
- client.println("");
- client.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement