Skip to content

Instantly share code, notes, and snippets.

@liangsteve
Created January 17, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangsteve/4b0248cb45ea1b7588e2 to your computer and use it in GitHub Desktop.
Save liangsteve/4b0248cb45ea1b7588e2 to your computer and use it in GitHub Desktop.
/*
* 2015 SensorUp (http://www.sensorup.com)
* This content is released under the (https://opensource.org/licenses/MIT) MIT License.
*
* Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)
* from the internal temperature sensor in an Arduino (http://playground.arduino.cc/Main/InternalTemperatureSensor).
*
* It works with Arduino Ethernet board. (https://www.arduino.cc/en/Main/ArduinoBoardEthernet)
*/
#include <SPI.h>
#include <HttpClient.h>
#include <Ethernet.h>
/******************************************************************************************
* change the <id> from the line below (e.g., #define DATASTREAM_ID_TEMP <id>) to the
* <id> of your SensorThings Datastream. You can get the Datastream <id> from the SensorUp
* playground's Observation API Request: /st-playground/proxy/v1.0/Datastreams(<id>)/Observations
*****************************************************************************************/
#define DATASTREAM_ID_TEMP 1186
/******************************************************************************************
* change the <token> from the line below (e.g., #define ACCESS_TOKEN <token>) to the
* <token> of your SensorThings Datastream. You can get the St-P-Access-Token <token> from the SensorUp
* playground's Observation API Request: St-P-Access-Token: 8a4bec01-fda3-462d-a9c0-a46e6d921807
*****************************************************************************************/
#define ACCESS_TOKEN "8a4bec01-fda3-462d-a9c0-a46e6d921807"
#define SERVER_IP "pg-api.sensorup.com"
#define PORT 80
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(127, 0, 0, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
unsigned long datastream[] = {DATASTREAM_ID_TEMP};
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 Celcius.
return (t);
}
void postToServer(double value,unsigned long datastreamId)
{
Serial.println("connecting...");
String str = "{ ";
str += " \"result\":";
str += value;
str += "}";
Serial.println(str);
// if you get a connection, report back via serial:
if (client.connect(SERVER_IP, PORT)) {
Serial.println("connected");
// Make a HTTP request:
client.println("POST /st-playground/proxy/v1.0/Datastreams("+ String(datastreamId) +")/Observations HTTP/1.1");
String host = "Host: ";
host.concat(SERVER_IP);
client.println(host);
client.println("Connection: close");
client.println("Content-Type: application/json");
String token = "St-P-Access-Token:";
token += ACCESS_TOKEN;
client.println(token);
client.println("Cache-Control: no-cache");
client.print("Content-Length: ");
client.print(str.length());
client.print("\n\n");
client.print(str);
while(true)
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
break;
}
}
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
//The internal temperature is the temperature inside the chip
postToServer(GetTemp(),datastream[0]);
delay(2500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment