Here it is
i'm sorry if it's messy but i'm quite new at programming, and had no time to clean it...
here is the first part, since it won't fit in the 9500 chars limit of the forum
here is a detailed description
this project takes the readings from 2 1wire sensors (for now, but it will be 3 or 4) and logs it in the sd with timestamp (timestamp not implemented now)
it also displays it graphically on a servo used as a gauge, with a button to switch between sensors, (a led change color based on first or second sensor)
the readings are presented also via http server
a fan automatically turns on when a certain sensor raises in temperature and turns off if the temperature goes down (that part is present but actually disabled)
the final project is meant for a small server room, other arduino pins would be wired on the air conditioner ir command to control it and some fans would spin up based on general temperature or specifically for temperatures of some equipment that will have sensors attached.
Thank you!
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//-------------------------ETHERNET-----------------//
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0xD5, 0xB6 }; //physical mac address proto shield
byte ip[] = {
192, 168, 5, 235 }; // ip in lan
byte gateway[] = {
192, 168, 5, 3 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
byte mydns[] = {
192, 168, 5, 3 };
EthernetServer server(80); //server port
IPAddress timeServer(132, 163, 4, 101);
EthernetUDP Udp;
unsigned int localPort = 8888;
const int timeZone = 1;
String readString;
File MioFile;
Servo myServo; // create a servo object
double angle=0; // variable to hold the angle for the servo motor
double tempind=14;
int selectsens=0;
double temp[]={
0,0};
int sensnum=0;
double diff=0;
int time=450;
int servopin=8;
int fanpin=3;
int redled=7;
int greenled=6;
int led[]={
greenled,redled};
int button=5;
time_t prevDisplay = 0;
//------------------------------SETUP----------------------------//
void setup(void)
{
int x=14;
//pinMode(fanpin,OUTPUT);
pinMode(redled,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(button,INPUT);
Serial.begin(9600);
Serial.println("INIZIALIZZO");
myServo.attach(servopin); // attaches the servo on pin 9 to the servo object
Ethernet.begin(mac, ip, mydns, gateway);
Udp.begin(localPort);
setSyncProvider(getNtpTime);
Serial.println("done");
server.begin();
sensors.begin();
delay(5500);
SD.begin(4);
myServo.write(angle);
// analogWrite(fanpin,100);
// while(x<45)
// {
//
// angle = map_double(x, 15, 45, 0, 179);
// myServo.write(angle);
// x=x+5;
// delay(time);
//
//}
digitalWrite(led[selectsens],HIGH);
// analogWrite(fanpin,0);
}
//----------------------------------LOOP----------------------//
void loop(void)
{
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
digitalClockDisplay();
}
}
if(sensnum==0)
{
temp[sensnum]=tempTest(sensnum);
// Serial.print("sensore ");
// Serial.print(sensnum);
// Serial.print(" - ");
// Serial.print(temp[sensnum]);
// Serial.println("C");
delay(500);
sensnum=1;
}
if(sensnum==1)
{
temp[sensnum]=tempTest(sensnum);
// Serial.print("sensore ");
// Serial.print(sensnum);
// Serial.print(" - ");
// Serial.print(temp[sensnum]);
// Serial.println("C");
delay(500);
sensnum=0;
}
if (digitalRead(button)==1)
{
switch (selectsens) {
case 0: // your hand is on the sensor
digitalWrite(led[selectsens],LOW);
selectsens=1;
digitalWrite(led[selectsens],HIGH);
delay(150);
break;
case 1: // your hand is close to the sensor
digitalWrite(led[selectsens],LOW);
selectsens=0;
digitalWrite(led[selectsens],HIGH);
delay(150);
}
}
servo(temp[selectsens]);
writeLog();
//servePage();
}
//--------------------WRITELOG-----------------------//
void writeLog(void)
{
MioFile = SD.open("log.txt", FILE_WRITE);
if (MioFile)
{
while (MioFile.available())
{
Serial.write(MioFile.read());
}
Serial.println("writing");
MioFile.print(temp[0]);
MioFile.print(",");
MioFile.println(temp[1]);
MioFile.close();
}
else {
Serial.println("error opening datalog.txt");
}
}
//-------------------------TEMPTEST------------------------//
double tempTest(int x)
{
double y=0;
sensors.requestTemperatures(); // Send the command to get temperatures
y=sensors.getTempCByIndex(x);
return y;
}
//---------------------------FANACT----------------------------//
void fanAct(void)
{
if(temp[sensnum]>tempind){
tempind=temp[sensnum];
// analogWrite(fanpin,200);
Serial.println("maggiore");
}
if(temp[sensnum]<tempind){
tempind=temp[sensnum];
analogWrite(fanpin,0);
Serial.println("minore");
}
if(temp[sensnum]==tempind){
// tempind=temp[sensnum];
Serial.println("uguale");
}
}
//----------------------------------SERVO-------------------------//
void servo(int x)
{
double angle;
angle = map_double(x, 15, 45, 0, 179);
myServo.write(angle);
}
float map_double(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}