#include <SPI.h>
#include <Ethernet.h>
#include <SHT1x.h>
#include "Wire.h"
////////////////////////////////////////////////////////////////////////////////
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 8
#define clockPin 9
SHT1x sht1x(dataPin, clockPin);
////////////////////////////////////////////////////////////////////////////////
// Ethernet data
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDD };
byte ip[] = { 192,168,1, 42 };
byte gateway[] = { 192, 168, 1, 1};
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192, 168, 1, 2 };
// initialize the library instance:
Client client(server, 80);
const int postingInterval = 29000; // delay between updates
long lastConnectionTime = 0; // last time connected to the server
boolean lastConnected = false; // state of the connection
long counter = 1;
String txtMsg="";
void sendData() {
Serial.println("...");
if (client.connect()) {
Serial.println("connecting...");
client.print("POST /input/ HTTP/1.1\n");
client.print("Host: HOSTNAME\n");
client.print("X-ApiKey: RANDOM_API_KEY\n");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.print("Content-Length: ");
client.println(txtMsg.length(), DEC);
client.print("Content-Type: text/json\n");
client.println("Connection: close\n");
client.println(txtMsg);
lastConnectionTime = millis();
counter += 1;
Serial.println("sent...");
}
else {
Serial.println("connection failed");
delay(1000);
}
}
////////////////////////////////////////////////////////////////////////////////
// DS1307
#define DS1307_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
void setDT()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 0;
minute = 01;
hour = 15;
dayOfWeek = 0;
dayOfMonth = 6;
month = 11;
year = 11;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
////////////////////////////////////////////////////////////////////////////////
String Float2String(float value)
{
// Convert a float to String with two decimals.
String s;
s = int(value);
s += '.';
s += int((value - int(value)) * 100);
return s;
}
void setup()
{
// start the Ethernet connection:
Ethernet.begin(mac, ip);
Wire.begin();
Serial.begin(9600);
// if datetime should be set:
//setDT()
// delay for the ethernet to get up
delay(1000);
}
void loop()
{
float temp_c, humidity;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
if(millis() - lastConnectionTime > postingInterval) {
// disconnect old client
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(1000);
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
humidity = sht1x.readHumidity();
Serial.print("Temperature: ");
Serial.print(temp_c, DEC);
Serial.print("C / ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Read data from the clock
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// the JSON message
txtMsg = "{ \"temperature\":";
txtMsg += Float2String(temp_c);
txtMsg += ", \"humidity\":";
txtMsg += Float2String(humidity);
txtMsg += ", \"counter\":";
txtMsg += counter;
txtMsg += ", \"datetime\":\'";
txtMsg += int(year);
txtMsg += "-";
txtMsg += int(month);
txtMsg += "-";
txtMsg += int(dayOfMonth);
txtMsg += " ";
txtMsg += int(hour);
txtMsg += ":";
txtMsg += int(minute);
txtMsg += ":";
txtMsg += int(second);
txtMsg += "\'";
txtMsg += "}";
// print for debugging
Serial.println(txtMsg);
sendData();
}
lastConnected = client.connected();
}