|
| 1 | +/* |
| 2 | + Dweet.io POST client for RestClient library |
| 3 | + Connects to dweet.io once every ten seconds, |
| 4 | + sends a POST request and a request body. Uses SSL |
| 5 | +
|
| 6 | + Shows how to use Strings to assemble path and body |
| 7 | +
|
| 8 | + note: WiFi SSID and password are stored in config.h file. |
| 9 | + If it is not present, add a new tab, call it "config.h" |
| 10 | + and add the following variables: |
| 11 | + char ssid[] = "ssid"; // your network SSID (name) |
| 12 | + char pass[] = "password"; // your network password |
| 13 | +
|
| 14 | + created 15 Feb 2016 |
| 15 | + by Tom Igoe |
| 16 | +
|
| 17 | + this example is in the public domain |
| 18 | +*/ |
| 19 | +#include <RestClient.h> |
| 20 | +#include <WiFi101.h> |
| 21 | +#include "config.h" |
| 22 | + |
| 23 | +char serverAddress[] = "dweet.io"; // server address |
| 24 | +int port = 80; |
| 25 | + |
| 26 | +WiFiClient wifi; |
| 27 | +RestClient client = RestClient(wifi, serverAddress, port); |
| 28 | +int status = WL_IDLE_STATUS; |
| 29 | +String response; |
| 30 | +int statusCode = 0; |
| 31 | + |
| 32 | +void setup() { |
| 33 | + Serial.begin(9600); |
| 34 | + while(!Serial); |
| 35 | + while ( status != WL_CONNECTED) { |
| 36 | + Serial.print("Attempting to connect to Network named: "); |
| 37 | + Serial.println(ssid); // print the network name (SSID); |
| 38 | + |
| 39 | + // Connect to WPA/WPA2 network: |
| 40 | + status = WiFi.begin(ssid, pass); |
| 41 | + } |
| 42 | + |
| 43 | + // print the SSID of the network you're attached to: |
| 44 | + Serial.print("SSID: "); |
| 45 | + Serial.println(WiFi.SSID()); |
| 46 | + |
| 47 | + // print your WiFi shield's IP address: |
| 48 | + IPAddress ip = WiFi.localIP(); |
| 49 | + Serial.print("IP Address: "); |
| 50 | + Serial.println(ip); |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + // assemble the path for the POST message: |
| 55 | + String dweetName = "scandalous-cheese-hoarder"; |
| 56 | + String path = "/dweet/for/" + dweetName; |
| 57 | + |
| 58 | + // assemble the body of the POST message: |
| 59 | + int sensorValue = analogRead(A0); |
| 60 | + String postData = "{\"sensorValue\":\""; |
| 61 | + postData += sensorValue; |
| 62 | + postData += "\"}"; |
| 63 | + |
| 64 | + Serial.println("making POST request"); |
| 65 | + client.setContentType("application/json"); |
| 66 | + statusCode = client.post(path, postData); |
| 67 | + response = client.readResponse(); |
| 68 | + Serial.print("Status code: "); |
| 69 | + Serial.println(statusCode); |
| 70 | + Serial.print("Response: "); |
| 71 | + Serial.println(response); |
| 72 | + |
| 73 | + Serial.println("Wait ten seconds\n"); |
| 74 | + delay(10000); |
| 75 | +} |
0 commit comments