Last active
March 19, 2017 05:57
-
-
Save bhagman/68044c473427dbaee573fb159bb30555 to your computer and use it in GitHub Desktop.
Using the Sparkfun 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 Sparkfun Data Service. | |
|| | https://data.sparkfun.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... | |
|| # | |
*/ | |
// Sparkfun Data Service REST URL format: | |
// http://data.sparkfun.com/input/[publicKey]?private_key=[privateKey]&leveltrigger=[value]&temperature=[value] | |
#include <UnoWiFiDevEd.h> | |
#define CONNECTOR "rest" | |
#define SERVER "data.sparkfun.com" | |
#define PUBLICKEY "yourpublickey" | |
#define PRIVATEKEY "yourprivatekey" | |
#define TIMEBETWEEN 9000 | |
#define TRIGGERPIN 11 | |
void setup() | |
{ | |
pinMode(TRIGGERPIN, INPUT); | |
digitalWrite(TRIGGERPIN, HIGH); | |
Ciao.begin(); | |
} | |
void loop() | |
{ | |
String uri = "/input/"; | |
uri += PUBLICKEY; | |
uri += "?private_key="; | |
uri += PRIVATEKEY; | |
uri += "&temperature="; | |
uri += String(getTemp()); | |
uri += "&leveltrigger="; | |
uri += String(readTrigger()); | |
// Display message in the WiFi Console: | |
Ciao.println("Sending data..."); | |
Ciao.println(uri); | |
// Upload the data, and wait for a response. | |
CiaoData data = Ciao.write(CONNECTOR, SERVER, uri); | |
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 readTrigger() | |
{ | |
// When triggered, the TRIGGERPIN registers low. | |
if (digitalRead(TRIGGERPIN)) | |
{ | |
return 0; | |
} | |
else | |
{ | |
return 1; | |
} | |
} | |
// http://playground.arduino.cc/Main/InternalTemperatureSensor | |
double getTemp(void) | |
{ | |
unsigned int wADC; | |
double t; | |
// The internal temperature has to be used | |
// with the internal reference of 1.1V. | |
// Channel 8 can not be selected with | |
// the analogRead function yet. | |
// Set the internal reference and mux. | |
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); | |
ADCSRA |= _BV(ADEN); // enable the ADC | |
delay(20); // wait for voltages to become stable. | |
ADCSRA |= _BV(ADSC); // Start the ADC | |
// Detect end-of-conversion | |
while (bit_is_set(ADCSRA,ADSC)); | |
// Reading register "ADCW" takes care of how to read ADCL and ADCH. | |
wADC = ADCW; | |
// The offset of 324.31 could be wrong. It is just an indication. | |
t = (wADC - 324.31 ) / 1.22; | |
// The returned temperature is in degrees Celsius. | |
return (t); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment