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