#include #include "nRF24L01.h" #include "RF24.h" #include "printf.h" /* Définition des Pins CE, CSN et connexion de la LED sur ARDUINO */ #define RF_CE 9 #define RF_CSN 10 #define LED_PIN 6 RF24 radio(RF_CE,RF_CSN); /* Définition de l'id de l'Arduino */ byte myID = 0x01; byte pipes[][7] = {"master","slave","idle"}; struct payload_request_t { uint8_t number; uint8_t destination; char message[14]; }; struct payload_general_t { uint8_t number; char message[15]; }; payload_request_t incoming; payload_general_t outgoing; // Guy Avril 2016 Temperature 1wire readings sent to radio #include char buffer[255]; boolean NoMoreAddress; OneWire ds(8); // on pin 8 (a 4.7K resistor is necessary) void ReadOneWireTemp(void) { byte addr[8]; byte i; byte present = 0; byte type_s; byte data[12]; float celsius; if ( !ds.search(addr)) { Serial.print("No more addresses.\n"); ds.reset_search(); NoMoreAddress = true; return; } NoMoreAddress = false; Serial.print("ROM ="); for( i = 0; i < 8; i++) { Serial.write(' '); Serial.print(addr[i], HEX); } if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.print("CRC is not valid!\n"); return; } // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: Serial.println(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: Serial.println(" Chip = DS18B20"); type_s = 0; break; case 0x22: Serial.println(" Chip = DS1822"); type_s = 0; break; default: Serial.println("Device is not a DS18x20 family device."); return; } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad Serial.print(" Data = "); Serial.print(present,HEX); Serial.print(" "); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); Serial.print(data[i], HEX); Serial.print(" "); } Serial.print(" CRC="); Serial.print( OneWire::crc8( data, 8), HEX); Serial.println(); // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } celsius = (float)raw / 16.0; Serial.print(" Temperature = "); Serial.print(celsius); Serial.print(" Celsius, "); dtostrf(celsius, 6, 4, buffer); } /* Envoi de la réponse */ /* ******************* */ void sendReply() { /* Message visible si la console est ouverte */ Serial.print("Message de retour de la commande \""); Serial.print(outgoing.message); Serial.println("\""); /* Retour de la reponse au demandeur */ radio.stopListening(); radio.openWritingPipe(pipes[1]); delay(10); radio.write(&outgoing,sizeof(payload_general_t)+1); delay(10); radio.startListening(); } /* Par défaut, la LED est enregistrée à l'état "off" */ byte last = 0; /* Initialisation du programme */ /* *************************** */ void setup() { Serial.begin(9600); /* Définition de pin de la LED comme SORTIE */ pinMode(LED_PIN, OUTPUT); /* On met le pin à l'état éteind */ digitalWrite(LED_PIN, LOW); printf_begin(); radio.begin(); radio.setAutoAck(1); radio.setRetries(1,3); radio.enableDynamicPayloads(); radio.openWritingPipe(pipes[1]); radio.openReadingPipe(1, pipes[0]); radio.startListening(); radio.printDetails(); } /* Boucle de "travail" */ /* ******************* */ void loop(void) { if(radio.available()) { radio.read(&incoming, sizeof(payload_request_t)); /* Affichage de la commande demandée dans la console */ Serial.print("Commande recu \""); Serial.println(incoming.message); /* Aduino vérifie que c'est bien à son ID que la demande s'adresse */ if (incoming.destination==myID) { /* Si le message est "get", on retourne l'état enregistré de la LED */ if (strncmp(incoming.message, "get", 3) == 0) { if (last > 0) strcpy(outgoing.message, "on"); else strcpy(outgoing.message, "off"); /* Sinon, la commande est t'elle de type "set" */ } else if (strncmp(incoming.message, "set", 3) == 0) { if (strncmp(&incoming.message[4], "on", 2)==0) { /* Si la commande est "on", on allume la LED */ digitalWrite(LED_PIN, HIGH); last = 1; strcpy(outgoing.message, "ok"); } else if (strncmp(&incoming.message[4], "off", 3)==0) { /* Si la commande est "off", on éteind la LED */ digitalWrite(LED_PIN, LOW); last = 0; strcpy(outgoing.message, "ok"); } else /* Sinon, Commande inconnue, ni"on", ni "off" */ strcpy(outgoing.message, "?"); /* Sinon, la commande est t'elle de type "tmp" (temperature) */ } else if (strncmp(incoming.message, "tmp", 3) == 0) { ReadOneWireTemp(); if (!NoMoreAddress) { strcpy(outgoing.message, buffer); } else strcpy(outgoing.message, "No more address"); /* Sinon, Commande inconnue, ni "get", ni "set" */ } else { strcpy(outgoing.message, "?"); } /* On retourne le résultat du traitement */ outgoing.number = incoming.number; sendReply(); } } delay(100); }