Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2012 19:25
Show Gist options
  • Save anonymous/4144877 to your computer and use it in GitHub Desktop.
Save anonymous/4144877 to your computer and use it in GitHub Desktop.
peace of file from aqua_control project
#include "Arduino.h"
#include "DallasTemperature.h"
#include "EtherCard.h"
#include "OneWire.h"
#include "Wire.h"
#include "DS1307new.h"
extern HardwareSerial Serial;
#define thermometerPin 4 //DS18B20 wired to Digital pin 2
OneWire oneWire(thermometerPin);
DallasTemperature sensors(&oneWire);
float tempext = 0;
unsigned int year = 0000;
unsigned short int month = 00;
unsigned short int day = 00;
unsigned short int hour = 00;
unsigned short int minute = 00;
unsigned short int second = 00;
float temp;
char* tempChar;
char* tst;
uint16_t startAddr = 0x0000; // Start address to store in the NV-RAM
uint16_t lastAddr; // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55;
static byte mymac[] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static byte myip[] = {192,168,1,15};
byte Ethernet::buffer[1200];
BufferFiller bfill;
byte bait_address[4];
//Define Pins
const int fans = 6;
const int buttonPin = 7;
const int lights = 8;
//Define Status variables
int fanStatusCode = 0;
bool fansStatus = false;
bool buttonStatus = false;
bool fanWebStatus = false;
bool lightsStatus = false;
bool lightsWebStatus = false;
const String statusLigadasWeb = "Ligadas pela web";
const String statusLigadasManual = "Ligadas manualmente";
const String statusLigadasAutomatico = "Ligadas automaticamente";
const String statusDesligadas = "Desligadas";
void setup(){
pinMode(9, INPUT); // Test of the SQW pin, D2 = INPUT
digitalWrite(9, HIGH);
RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.ctrl = 0x00;
RTC.setCTRL();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip);
Serial.begin(9600);
sensors.begin(); // Start the OneWire sensors , in this case only one DS18B20
Serial.println("DS1307 + DS18B20 test");
Serial.println();
Serial.println("Date (dd/mm/yyyy), time (hh:mm:ss) and temperature (degC)");
}
//Method to get the actual temperature in celcius
void getTemp()
{
sensors.requestTemperatures(); //request for read values of all the sensors in the wire. in this case only one Ds18B20
temp=sensors.getTempCByIndex(0);
char buf[50];
tempChar = dtostrf(temp,3,2,buf);
}
//Method to control fans
void controlFans(){
int highLimit = 30;
if ((buttonStatus == true && fansStatus == false) || (temp >= highLimit && fansStatus == false) || (fanWebStatus == true && fansStatus == false)){
fansStatus = true;
digitalWrite(fans, HIGH);
Serial.println("Ligou");
}
else if(temp < highLimit && fansStatus == true && buttonStatus == false && fanWebStatus == false){
fansStatus = false;
digitalWrite(fans, LOW);
Serial.println("Desligou");
}
}
//Method to control lights
void controlLights(){
if (lightsWebStatus == true && lightsStatus == false){
lightsStatus = true;
digitalWrite(lights, HIGH);
}
else if(lightsStatus == true && lightsWebStatus == false){
lightsStatus = false;
digitalWrite(lights, LOW);
}
}
//Method to get lights status
const String getLightsStatus(){
if(lightsWebStatus){
return statusLigadasWeb;
}
else if (lightsStatus){
return statusLigadasAutomatico;
}
else{
return statusDesligadas;
}
}
//Method to get fans status
const String getFansStatus(){
if (buttonStatus){
return statusLigadasManual;
}
else if(fanWebStatus){
return statusLigadasWeb;
}
else if (fansStatus){
return statusLigadasAutomatico;
}
else{
return statusDesligadas;
}
}
//Method to control button
void controlButton(){
if(buttonStatus){
buttonStatus = false;
}
else{
buttonStatus = true;
}
}
void controlFansWeb(bool a){
if(a){
fanWebStatus = true;
}
else{
fanWebStatus = false;
}
}
void controlLightsWeb(bool a){
if(a){
lightsWebStatus = true;
}
else{
lightsWebStatus = false;
}
}
static word PageGetTemp() {
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"$S"),tempChar);
return bfill.position();
}
void loop(){
// read the date from DS1307 TRC
RTC.getTime();
day=RTC.day;
month=RTC.month;
year=RTC.year;
// read the date from DS1307 TRC
hour=RTC.hour;
minute=RTC.minute;
second=RTC.second;
//read the temperature from DS18B20
sensors.requestTemperatures(); //request for read values of all the sensors in the wire. in this case only one Ds18B20
tempext=sensors.getTempCByIndex(0); //read the temperature from the fisrt sensor in the wire, Sensor 0 or master
// Print the results
Serial.print(day);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print(" ");
Serial.println(tempext);
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos){ // check if valid tcp data is received
ether.httpServerReply(PageGetTemp()); // send web page data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment