| 1 | = Projet Réseau ce capteur = |
| 2 | |
| 3 | Cette page est en construction, elle va s'étoffer dans les jours qui viennent... |
| 4 | |
| 5 | = Connexion Arduino - Arduinos = |
| 6 | |
| 7 | {{{#!c |
| 8 | #include <SPI.h> |
| 9 | #include <Wire.h> |
| 10 | #include <Adafruit_GFX.h> |
| 11 | #include <Adafruit_SSD1306.h> |
| 12 | #include "RF24.h" |
| 13 | #include "printf.h" |
| 14 | |
| 15 | // unsigned int waitFor(timer, period) |
| 16 | // Timer pour taches périodique |
| 17 | // arguments : |
| 18 | // - timer : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1 |
| 19 | // - period : période souhaitée |
| 20 | // retour : |
| 21 | // - nombre de période écoulée depuis le dernier appel |
| 22 | // |
| 23 | #define MAX_WAIT_FOR_TIMER 16 |
| 24 | unsigned int waitFor(int timer, unsigned long period){ |
| 25 | static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER]; |
| 26 | unsigned long newTime = micros() / period; |
| 27 | int delta = newTime - waitForTimer[timer]; |
| 28 | if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period); |
| 29 | if ( delta ) waitForTimer[timer] = newTime; |
| 30 | return delta; |
| 31 | } |
| 32 | |
| 33 | // Configuration des broches |
| 34 | #define PIN_LUMI 1 |
| 35 | #define OLED_RESET 4 |
| 36 | Adafruit_SSD1306 display(OLED_RESET); |
| 37 | RF24 radio(9,10); // radio(CE,CS) |
| 38 | byte addresses_tx[][6] = {"1sens","2Sens"}; |
| 39 | byte addresses_rx[][6] = {"1base","2base"}; |
| 40 | |
| 41 | // Variables globales pour la communication inter-taches |
| 42 | byte lumi, lumiFull; |
| 43 | byte tscFull; |
| 44 | unsigned long tsc; |
| 45 | |
| 46 | void Lumi (int timer, unsigned long period, byte pin, byte *lumi, byte *lumiFull) |
| 47 | { |
| 48 | if (!waitFor(timer,period)) return; |
| 49 | *lumi = map(analogRead(pin),0,1023,0,99); |
| 50 | *lumiFull = 1; |
| 51 | } |
| 52 | |
| 53 | void SensRF (byte *mess, byte *full) { |
| 54 | if (! (*full) ) return; |
| 55 | *full = 0; |
| 56 | Serial.println(*mess); |
| 57 | radio.stopListening(); |
| 58 | radio.write( mess, sizeof(byte) ); |
| 59 | radio.startListening(); |
| 60 | } |
| 61 | |
| 62 | void RcvTSC (unsigned long *tsc, byte *tscFull) |
| 63 | { |
| 64 | if (!radio.available()) return; |
| 65 | radio.read( tsc, sizeof(unsigned long) ); |
| 66 | *tscFull = 1; |
| 67 | } |
| 68 | |
| 69 | void Oled1 (unsigned long *mess, byte *full) { |
| 70 | if (! (*full) ) return; |
| 71 | *full = 0; |
| 72 | display.clearDisplay(); |
| 73 | display.setCursor(0,0); |
| 74 | display.println(*mess); |
| 75 | display.display(); |
| 76 | } |
| 77 | |
| 78 | void setup() { |
| 79 | Serial.begin(115200); |
| 80 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| 81 | display.clearDisplay(); |
| 82 | display.setTextSize(1); |
| 83 | display.setTextColor(WHITE); |
| 84 | |
| 85 | printf_begin(); |
| 86 | radio.begin(); |
| 87 | radio.setPALevel(RF24_PA_LOW); |
| 88 | radio.openWritingPipe(addresses_tx[0]); |
| 89 | radio.openReadingPipe(1,addresses_rx[0]); |
| 90 | radio.printDetails(); |
| 91 | } |
| 92 | |
| 93 | void loop() { |
| 94 | Lumi (0,1000000, PIN_LUMI, &lumi, &lumiFull); |
| 95 | SensRF (&lumi, &lumiFull); |
| 96 | RcvTSC (&tsc, &tscFull); |
| 97 | Oled1 (&tsc, &tscFull); |
| 98 | } |
| 99 | }}} |
| 100 | |
| 101 | {{{#!c |
| 102 | #include <SPI.h> |
| 103 | #include <Wire.h> |
| 104 | #include <Adafruit_GFX.h> |
| 105 | #include <Adafruit_SSD1306.h> |
| 106 | #include "RF24.h" |
| 107 | #include "printf.h" |
| 108 | |
| 109 | // unsigned int waitFor(timer, period) |
| 110 | // Timer pour taches périodique |
| 111 | // arguments : |
| 112 | // - timer : numéro de timer entre 0 et MAX_WAIT_FOR_TIMER-1 |
| 113 | // - period : période souhaitée |
| 114 | // retour : |
| 115 | // - nombre de période écoulée depuis le dernier appel |
| 116 | // |
| 117 | #define MAX_WAIT_FOR_TIMER 16 |
| 118 | unsigned int waitFor(byte timer, unsigned long period){ |
| 119 | static unsigned long waitForTimer[MAX_WAIT_FOR_TIMER]; |
| 120 | unsigned long newTime = micros() / period; |
| 121 | int delta = newTime - waitForTimer[timer]; |
| 122 | if ( delta < 0 ) delta += 1 + (0xFFFFFFFF / period); |
| 123 | if ( delta ) waitForTimer[timer] = newTime; |
| 124 | return delta; |
| 125 | } |
| 126 | |
| 127 | // Configuration des broches |
| 128 | #define PIN_LUMI 1 |
| 129 | #define OLED_RESET 4 |
| 130 | Adafruit_SSD1306 display(OLED_RESET); |
| 131 | RF24 radio(9,10); |
| 132 | byte addresses_tx[][6] = {"1base","2base"}; |
| 133 | byte addresses_rx[][6] = {"1Sens","2Sens"}; |
| 134 | |
| 135 | // Variables globales pour la communication inter-taches |
| 136 | byte lumi, lumiFull; |
| 137 | |
| 138 | void BaseRF (byte *lumi, byte *lumiFull) |
| 139 | { |
| 140 | if (!radio.available()) return; |
| 141 | radio.read( lumi, sizeof(byte) ); |
| 142 | *lumiFull = 1; |
| 143 | } |
| 144 | |
| 145 | void SendTSC ( byte instance, byte timer, unsigned long period, byte *dest) { |
| 146 | unsigned long TSC[2]; |
| 147 | if (!waitFor(timer, period)) return; |
| 148 | radio.openWritingPipe(dest); |
| 149 | radio.stopListening(); |
| 150 | radio.write( &TSC[instance], sizeof(unsigned long)); |
| 151 | TSC[instance]++; |
| 152 | radio.startListening(); |
| 153 | } |
| 154 | |
| 155 | void Oled1 (byte *mess, byte *full) { |
| 156 | if (! (*full) ) return; |
| 157 | *full = 0; |
| 158 | display.clearDisplay(); |
| 159 | display.setCursor(0,0); |
| 160 | display.print("Lumiere : "); |
| 161 | display.println(*mess); |
| 162 | display.display(); |
| 163 | } |
| 164 | |
| 165 | void setup() { |
| 166 | Serial.begin(115200); |
| 167 | |
| 168 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) |
| 169 | display.clearDisplay(); |
| 170 | display.setTextSize(1); |
| 171 | display.setTextColor(WHITE); |
| 172 | |
| 173 | printf_begin(); |
| 174 | radio.begin(); |
| 175 | radio.setPALevel(RF24_PA_LOW); |
| 176 | radio.openReadingPipe(1,addresses_rx[0]); |
| 177 | radio.openReadingPipe(2,addresses_rx[1]); |
| 178 | radio.startListening(); |
| 179 | radio.printDetails(); |
| 180 | } |
| 181 | |
| 182 | void loop() { |
| 183 | BaseRF(&lumi, &lumiFull); |
| 184 | Oled1 (&lumi, &lumiFull); |
| 185 | SendTSC (0, 0, 1000000, addresses_tx[0]); |
| 186 | SendTSC (1, 0, 500000, addresses_tx[1]); |
| 187 | } |
| 188 | }}} |