Je télécharge via Ethernet (CPL) ou Sshfs manager (mon Yun est trop loin pour le Wifi)
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <FileIO.h>
// Déclaration des variables
int DigitalPin[] = {2, 8, 9, 10, 11, 12, 13};
int AnalogPin[] = {0, 1, 2, 3};
// Relais Electroavannes
int evEst = 8;
int evSudEst = 9;
int evSud = 10;
int exSudOuest = 11;
int exOuest = 12;
int evGaG = 13;
// Sondes numériques
int pluviometre = 2;
// Sondes analogiques
int hygroEst = 0;
int hygroSud = 2;
int hygroOuest = 1;
int hygroGaG = 3;
YunServer server;
// Déclaration des fonctions
void readProbes();
void writeCrossData(String dataString);
// Initialisation
void setup() {
// Entrées numériques
pinMode(pluviometre,INPUT);
// Sortie numériques
pinMode(evEst, OUTPUT);
pinMode(evSudEst, OUTPUT);
pinMode(evSud, OUTPUT);
pinMode(exOuest, OUTPUT);
pinMode(exSudOuest, OUTPUT);
pinMode(evGaG, OUTPUT);
// Initialize the Bridge and the Serial
Bridge.begin();
FileSystem.begin();
server.listenOnLocalhost();
server.begin();
}
// Boucle infini sur attente d'évenement
void loop() {
// REST : attente d'évenment du serveur
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
// Mesure valeur sonde
readProbes();
// 50 ms
delay(50);
}
// Lire la commande reçue
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "digital") {
digitalCommand(client);
}
if (command == "analog") {
analogCommand(client);
}
if (command == "statut") {
statutCommand(client);
}
}
// Commande numérique
void digitalCommand(YunClient client) {
int pin, value;
// Lecture du numéro de Pin
pin = client.parseInt();
// Si valeur : Ecriture de la valeur
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
// Sinon Lecture de la valeur existante
else {
value = digitalRead(pin);
}
client.print(F("digital,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
// Commande analogique
void analogCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
analogWrite(pin, value);
}
else {
value = analogRead(pin);
}
client.print(F("analog,"));
client.print(pin);
client.print(F(","));
client.println(value);
}
// Commande lecture statut
void statutCommand(YunClient client) {
int pin, value;
String dataString;
dataString += " statut = ";
client.print(F("statut"));
// Lecture des entrées numériques DigitalPin[] = {2,
// et des relais (output) 8, 9, 10, 11}
for (int thisPin = 0; thisPin < 7; thisPin++) {
pin = DigitalPin[thisPin];
value = digitalRead(pin);
client.print(F("#"));
client.print(pin);
client.print(F("="));
client.print(value);
dataString += "," + String(pin) + "=" + value ;
}
for (int thisPin = 0; thisPin < 4; thisPin++) {
pin = AnalogPin[thisPin];
value = analogRead(pin);
client.print(F("#A"));
client.print(pin);
client.print(F("="));
client.print(value);
dataString += ",A" + String(thisPin) + "=" + value ;
}
client.println("");
writeCrossData(dataString);
}
void writeCrossData(String dataString) {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
File dataFile = FileSystem.open("/mnt/sd/arduino/www/ajax-cross-get.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
}
void readProbes() {
int pin, value;
String dataString = " statut = ";
// Lecture des entrées numériques DigitalPin[] = {2,
// et des relais (output) 8, 9, 10, 11}
for (int thisPin = 0; thisPin < 5; thisPin++) {
pin = DigitalPin[thisPin];
dataString += " ,Digital" + String(pin) + "=" + value;
}
for (int thisPin = 0; thisPin < 4; thisPin++) {
pin = AnalogPin[thisPin];
value = analogRead(pin);
dataString += " ,Analog" + String(pin) + "=" + value;
}
writeCrossData(dataString);
}