1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include <SPI.h>
#include <Ethernet2.h>
#include <Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h> //Librairie du capteur
char myserver[] = "www.myserver.com";
char c;
int secure_time = 0;
int secure_time_max = 15; //attente hors connexion de 10s ouverture de tous les relais
// RELAY PIN (10, 11, 12, 13 affected to ETHERNET SHIELD)
int light_relay = 2;
int spray_relay = 3;
int air_temp_relay = 4;
int water_temp_relay = 5;
OneWire oneWire(6); //Sonde de température en pin 6
String readString;
String main_status = "0";
String light_status = "0";
String spray_status = "0";
String temp_status = "0";
float air_temp_min = 23;
float DS18B20_temperature;
float air_temp_max = 25;
DallasTemperature sensors(&oneWire); //Utilistion du bus Onewire pour les capteurs
DeviceAddress sensorDeviceAddress; //Vérifie la compatibilité des capteurs avec la librairie
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 178 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
EthernetClient client;
void setup() {
//Activation de la sonde de température
sensors.begin();
sensors.getAddress(sensorDeviceAddress, 0); //Demande l'adresse du capteur à l'index 0 du bus
sensors.setResolution(sensorDeviceAddress, 12); //Résolutions possibles: 9,10,11,12
// INITIALISATION DES RELAIS
pinMode(light_relay, OUTPUT);
pinMode(spray_relay, OUTPUT);
pinMode(air_temp_relay, OUTPUT);
pinMode(water_temp_relay, OUTPUT);
digitalWrite(light_relay, HIGH);
digitalWrite(spray_relay, HIGH);
digitalWrite(air_temp_relay, HIGH);
digitalWrite(water_temp_relay, HIGH);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("Arduino ip: ");
Serial.println(Ethernet.localIP());
Serial.println("Try to connect...");
delay(1000);
}
//Fonction raccourcies pour les relais
void SPRAY_ON() {
digitalWrite(spray_relay, LOW);
}
void SPRAY_OFF() {
digitalWrite(spray_relay, HIGH);
}
void LIGHT_ON() {
digitalWrite(light_relay, LOW);
}
void LIGHT_OFF() {
digitalWrite(light_relay, HIGH);
}
void WARM_ON() {
digitalWrite(air_temp_relay, LOW);
}
void WARM_OFF() {
digitalWrite(air_temp_relay, HIGH);
}
// Début du programme
void loop()
{
// Réception des données sur le réseau ethernet
EthernetClient client = server.available();
if (client.available())
{
for(int i=0; i <= 20; i++)
{
char c = client.read();
if (readString.length() < 20)
{
readString += c;
}
}
client.stop();
//Parse de readString pour récupérer les bits après 192.168.1.178?
main_status = readString[6];
light_status = readString[7];
spray_status = readString[8];
temp_status = readString[9];
// Gestion des relais
if(main_status == "0")
{
light_status = "0";
spray_status = "0";
temp_status = "0";
LIGHT_OFF(); SPRAY_OFF(); WARM_OFF();
}
if(main_status == "1")
{
if (light_status == "0") {
LIGHT_OFF();
}
if (light_status == "1") {
LIGHT_ON();
}
if (spray_status == "0") {
SPRAY_OFF();
}
if (spray_status == "1") {
SPRAY_ON();
}
// Acquisition de la température
sensors.requestTemperatures();
DS18B20_temperature = sensors.getTempCByIndex(0);
if(temp_status == "1")
{
if ( DS18B20_temperature < air_temp_min)
{
WARM_ON();
}
if (DS18B20_temperature > air_temp_max)
{
WARM_OFF();
}
}
if(temp_status == "0")
{
WARM_OFF();
}
//Envoi de la température à myserver
int checkConnect = client.connect(myserver, 80);
Serial.print("checkConnect: ");
Serial.println(checkConnect); //permet de connaitre l'état de client.connect(myserver, 80);
if (client.connect(myserver, 80))
{
Serial.print("Connected to ");
Serial.println(myserver);
client.print("GET /arduino/ajax_controller.php");
client.print("air_temp=");
client.print(DS18B20_temperature);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(myserver);
client.println("Connection: close");
client.println();
client.println();
}
else
{
Serial.println("Connection failed to Server");
}
}
secure_time = 0; //réinitialisation de secure_time
}
else //Pas de connection ethernet, début du compte de secure_time avant ouverture de tous les relais.
{
if(secure_time > secure_time_max)
{
SPRAY_OFF();
LIGHT_OFF();
WARM_OFF();
Serial.println("WARNING !!! Server connexion failed. Check all wires and internet access !");
}
delay(1000);
secure_time++;
}
readString = ""; // on réinitilaise readString
}
//fin de loop() |