wiki:SujetTP8-2016

Version 1 (modified by franck, 8 years ago) (diff)

--

Projet Réseau ce capteur

Cette page est en construction, elle va s'étoffer dans les jours qui viennent...

Connexion Arduino - Arduinos

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RF24.h"
#include "printf.h"

// unsigned int waitFor(timer, period) 
// Timer pour taches périodique
// arguments :
//  - timer  : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1
//  - period : période souhaitée
// retour :
//  - nombre de période écoulée depuis le dernier appel
//
#define MAX_WAIT_FOR_TIMER 16
unsigned int waitFor(int timer, unsigned long period){
  static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER];
  unsigned long newTime = micros() / period;
  int delta = newTime - waitForTimer[timer];
  if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period);   
  if ( delta ) waitForTimer[timer] = newTime;
  return delta;
}

// Configuration des broches
#define PIN_LUMI 1
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
RF24 radio(9,10); // radio(CE,CS)
byte addresses_tx[][6] = {"1sens","2Sens"};
byte addresses_rx[][6] = {"1base","2base"};

// Variables globales pour la communication inter-taches
byte lumi, lumiFull;
byte tscFull;
unsigned long tsc;

void Lumi (int timer, unsigned long period, byte pin, byte *lumi, byte *lumiFull)
{
   if (!waitFor(timer,period)) return;
   *lumi = map(analogRead(pin),0,1023,0,99);
   *lumiFull = 1;   
}

void SensRF (byte *mess,  byte *full) {
  if (! (*full) ) return;
  *full = 0;
  Serial.println(*mess);
  radio.stopListening();
  radio.write( mess, sizeof(byte) );
  radio.startListening();
}

void RcvTSC (unsigned long *tsc, byte *tscFull)
{
   if (!radio.available()) return;
   radio.read( tsc, sizeof(unsigned long) );
   *tscFull = 1;   
}

void Oled1 (unsigned long *mess,  byte *full) {
  if (! (*full) ) return;
  *full = 0;
  display.clearDisplay();
  display.setCursor(0,0);
  display.println(*mess);
  display.display();
}

void setup() {
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  
  printf_begin();
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(addresses_tx[0]);
  radio.openReadingPipe(1,addresses_rx[0]);
  radio.printDetails();
}

void loop() {
  Lumi (0,1000000, PIN_LUMI, &lumi, &lumiFull);  
  SensRF (&lumi, &lumiFull);
  RcvTSC (&tsc, &tscFull);
  Oled1 (&tsc, &tscFull);
}
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RF24.h"
#include "printf.h"

// unsigned int waitFor(timer, period) 
// Timer pour taches périodique
// arguments :
//  - timer  : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1
//  - period : période souhaitée
// retour :
//  - nombre de période écoulée depuis le dernier appel
//
#define MAX_WAIT_FOR_TIMER 16
unsigned int waitFor(byte timer, unsigned long period){
  static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER];
  unsigned long newTime = micros() / period;
  int delta = newTime - waitForTimer[timer];
  if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period);   
  if ( delta ) waitForTimer[timer] = newTime;
  return delta;
}

// Configuration des broches
#define PIN_LUMI 1
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
RF24 radio(9,10);
byte addresses_tx[][6] = {"1base","2base"};
byte addresses_rx[][6] = {"1Sens","2Sens"};

// Variables globales pour la communication inter-taches
byte lumi, lumiFull;

void BaseRF (byte *lumi, byte *lumiFull)
{
   if (!radio.available()) return;
   radio.read( lumi, sizeof(byte) );
   *lumiFull = 1;   
}

void SendTSC ( byte instance, byte timer, unsigned long period, byte *dest) {
  unsigned long TSC[2];
  if (!waitFor(timer, period)) return;
  radio.openWritingPipe(dest);
  radio.stopListening();
  radio.write( &TSC[instance], sizeof(unsigned long));
  TSC[instance]++;
  radio.startListening();
}

void Oled1 (byte *mess,  byte *full) {
  if (! (*full) ) return;
  *full = 0;
  display.clearDisplay();
  display.setCursor(0,0);
  display.print("Lumiere : ");
  display.println(*mess);
  display.display();
}

void setup() {
  Serial.begin(115200);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  printf_begin();
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openReadingPipe(1,addresses_rx[0]);
  radio.openReadingPipe(2,addresses_rx[1]);
  radio.startListening();
  radio.printDetails();
}

void loop() {
  BaseRF(&lumi, &lumiFull);  
  Oled1 (&lumi, &lumiFull);
  SendTSC (0, 0, 1000000, addresses_tx[0]);
  SendTSC (1, 0, 500000,  addresses_tx[1]);
}