Created
March 19, 2017 05:59
-
-
Save bhagman/ba12819dffa31074faae90a2b6cb6847 to your computer and use it in GitHub Desktop.
Using the ThingSpeak Data Service with an UNO WiFi (via REST)
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
/* | |
|| @author Brett Hagman <[email protected]> | |
|| @url https://roguerobotics.com/ | |
|| | |
|| @description | |
|| | A simple example of an UNO WiFi sending data to the ThingSpeak Data Service (REST). | |
|| | https://thingspeak.com/ | |
|| | | |
|| | In the Arduino IDE, you will need to add the "Arduino Uno WiFi Dev Ed Library" in the Library Manager. | |
|| | Sketch > Include Library > Manage Libraries... | |
|| # | |
*/ | |
// ThingSpeak Data Service REST URL format: | |
// https://api.thingspeak.com/update?api_key=[yourAPIkey]&field1=[field1data]&field2=[field2data] | |
#include <UnoWiFiDevEd.h> | |
#define CONNECTOR "rest" | |
#define SERVER "api.thingspeak.com" | |
#define PRIVATEKEY "YOURAPIKEYHERE" | |
#define TIMEBETWEEN 15000 | |
void setup() | |
{ | |
Ciao.begin(); | |
Ciao.println("ThingSpeak REST Example"); | |
} | |
void loop() | |
{ | |
char pathquery[200]; | |
// Creates the REST path and query | |
sprintf(pathquery, "/update?api_key=%s&field1=%d&field2=%s", PRIVATEKEY, getValue1(), String(getValue2()).c_str()); | |
// Display message in the WiFi Console: | |
Ciao.println("Sending data..."); | |
Ciao.println(pathquery); | |
// Upload the data, and wait for a response. | |
CiaoData data = Ciao.write(CONNECTOR, SERVER, pathquery); | |
if (!data.isEmpty()) | |
{ | |
Ciao.println( "State: " + String (data.get(1)) ); | |
Ciao.println( "Response: " + String (data.get(2)) ); | |
} | |
else | |
{ | |
Ciao.println("Write Error"); | |
} | |
// Wait the required time between requests to store data. | |
delay(TIMEBETWEEN); | |
} | |
int getValue1() | |
{ | |
static int currentValue = 0; | |
currentValue += 1; | |
if (currentValue >= 10) | |
currentValue = 0; | |
return currentValue; | |
} | |
float getValue2() | |
{ | |
static int currentValue = 0; | |
currentValue += 1; | |
if (currentValue >= 16) | |
currentValue = 0; | |
return sin(currentValue*PI/8.0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment