wiki:SujetTP8-2016

Projet Réseau ce capteur

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

Connexion Arduino - Arduinos

L'application suivante fait communiquer trois arduinos. Deux sont des capteurs et un est une base. Dans le projet final, la base sera sur le RaspberryPI. Dans l'immédiat pour simplifier la base sera sur l'arduino mais se comptera presque comme la basen, à ceci près que le server http sera simplement le terminal.

L'API de commande du module NRF est la même sur Arduino et sur RaspberryPI et c'est grâce à ça qu'il est possible d'envisager de commencer par 100% Arduino avant de faire intervenir le RaspberryPI.

Capteur

#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] = {"1seFW","2seFW"};
byte addresses_rx[][6] = {"1baFW","2baFW"};

// 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.openWritingPipe(addresses_tx[1]);
  radio.openReadingPipe(1,addresses_rx[1]);
  radio.printDetails();
}

void loop() {
  Lumi (0,100000, PIN_LUMI, &lumi, &lumiFull);  
  SensRF (&lumi, &lumiFull);
  RcvTSC (&tsc, &tscFull);
  Oled1 (&tsc, &tscFull);
}

Base

#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] = {"1baFW","2baFW"};
byte addresses_rx[][6] = {"1seFW","2seFW"};

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

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

void SendTSC ( byte instance, byte timer, unsigned long period, byte *dest) {
  static 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, byte *pipe) {
  static byte value[2];
  if (! (*full) ) return;
  *full = 0;
  value[(*pipe) % 2] = *mess;
  display.clearDisplay();
  display.setCursor(0,0);
  display.print("Lumiere 1 : ");
  display.println(value[0]);
  display.print("Lumiere 2 : ");
  display.println(value[1]);
  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);
  display.display();

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

void loop() {
  byte timer=0;
  byte instSendTSC=0;
  BaseRF(&lumi, &lumiFull, &pipe);  
  Oled1 (&lumi, &lumiFull, &pipe);
  SendTSC (instSendTSC++, timer++, 1000000, addresses_tx[0]);
  SendTSC (instSendTSC++, timer++, 500000,  addresses_tx[1]);
}
Last modified 8 years ago Last modified on Apr 6, 2016, 12:45:42 PM