Last active
February 14, 2016 22:08
-
-
Save santocyber/e59dd8e48af21f430313 to your computer and use it in GitHub Desktop.
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
#include <SPI.h> | |
#include <Ethernet.h> | |
//#include <SendCarriots.h> // You must include the library here in order to use it in the sketch | |
#include <SHT1x.h> | |
#define dataPin 10 | |
#define clockPin 11 | |
SHT1x sht1x(dataPin, clockPin); | |
// Xively parameters | |
const String APIKEY = "44921ed682813e2bd3e2224de44ee5cd8c92a95d7530459e3aeac8ea6f60e12a"; // Insira aqui sua APIKEY da conta no Carriots, com aspas mesmo | |
const String DEVICE = "defaultDevice@santocyber.santocyber"; // ID do dispositivo no Carriots, com aspas também | |
float h,t; | |
int flag; | |
char buffer; | |
// Enter a MAC address for your controller below. | |
// Newer Ethernet shields have a MAC address printed on a sticker on the shield | |
//byte mac[] = { 0x31, 0xAD, 0xF2, 0x6E, 0xCA, 0x82 }; | |
//byte ip[] = { 192,168,2,222 }; | |
//byte gateway[] = { 192,168,2,1 }; | |
//byte subnet[] = { 255,255,255,0 }; | |
byte mac[] = {0x00, 0x23, 0xdf, 0x82, 0xd4, 0x01}; | |
byte ip[] = { 192,168,2,222 }; | |
byte server[] = {82,223,244,60}; | |
int port = 80; | |
//IPAddress ip(10,10,10,4); // Your IP Address | |
//IPAddress gateway(10,10,10,1); // Define o gateway da rede | |
//IPAddress subnet(255,255,255,0); // Define a máscara da rede | |
//IPAddress server(82,223,244,60); // Endereço IP do api.carriots.com | |
//SendCarriots sender; // Instantiate an instance of the SendCarriots library | |
EthernetClient client; // Inicializa o cliente da rede Ethernet | |
//1200000 | |
const long interval = 50000; // Intervalo de 20 minutos (20*60*1000 ms) | |
unsigned long previousMillis = interval-1000; | |
void setup() { | |
Serial.begin(9600); // Start serial port | |
Serial.println(F("Starting")); | |
Ethernet.begin(mac,ip); // Start the Ethernet connection | |
} | |
void loop() { | |
// Read values from the sensor | |
t = sht1x.readTemperatureC(); | |
h = sht1x.readHumidity(); | |
// Print the values to the serial port | |
Serial.print("Temperature: "); | |
Serial.print(t, DEC); | |
Serial.print("C / "); | |
Serial.print(" Humidity: "); | |
Serial.print(h); | |
Serial.println("%"); | |
unsigned long currentMillis = millis(); // Rotina de contagem do intervalo de 20 minutos | |
if(currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
sendStream(); // Chama a função que envia os dados para o servidor do Carriots | |
} | |
while(client.available()) { // Rotina de debug: mostra no terminal serial as mensagens recebidas | |
char c=client.read(); // do servidor do Carriots | |
Serial.print(c); | |
} | |
if(!client.connected()) client.stop(); | |
delay(500); | |
} | |
void sendStream() { | |
char TempString[32]; // Array temporário para armazenar dados | |
dtostrf(t,2,1,TempString); // dtostrf( [Float variable] , [Minimum SizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] ) | |
String temp = String(TempString); | |
dtostrf(h,2,2,TempString); | |
String humid = String(TempString); | |
if (client.connect(server, 80)) { // Caso haja conexão | |
Serial.println(F("connected")); | |
// Constrói a mensagem de dados a ser enviada para o Carriots: | |
String json = "{\"protocol\":\"v2\",\"device\":\""+String(DEVICE)+"\",\"at\":\"now\",\"data\":{\"T\":"+String(temp)+",\"H\":"+String(humid)+"}}"; | |
int length=json.length(); | |
// For debugging: | |
Serial.print(F("carriots.apikey: ")); | |
Serial.println(String(APIKEY)); | |
Serial.print(F("Content-Length: ")); | |
Serial.println(String(length)); | |
Serial.println(F("Connection: close")); | |
Serial.println(json); | |
// Constrói o comando HTTP para estabelecer conexão com o api.carriots.com | |
client.println(F("POST /streams HTTP/1.1")); | |
client.println(F("Host: api.carriots.com")); | |
client.println(F("Accept: application/json")); | |
client.println(F("User-Agent: Arduino-Carriots")); | |
client.println(F("Content-Type: application/json")); | |
client.print(F("carriots.apikey: ")); | |
client.println(String(APIKEY)); | |
client.print(F("Content-Length: ")); | |
int thisLength = json.length(); | |
client.println(String(thisLength)); | |
client.println(F("Connection: close")); | |
client.println(); | |
client.println(json); // Envia a mensagem de dados. | |
} | |
else { | |
// Caso haja falha na conexão: | |
Serial.println(F("connection failed")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment