Remote TX using NUCLEO_L152RE + AUREL RTX-MID-3V (Sub1GHz module). This SW working in conjunction of the RX part that is: Remote RX using NUCLEO_L152RE + AUREL RTX-MID-3V (Sub1GHz module)

Dependencies:   mbed

Committer:
emcu
Date:
Sun Jan 08 11:13:12 2017 +0000
Revision:
1:c11167ef6bd1
Parent:
0:6877fadef7d5
Child:
2:a03e59525e5b
Improvement: retransmit in case of error.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emcu 0:6877fadef7d5 1 /*
emcu 0:6877fadef7d5 2
emcu 0:6877fadef7d5 3 TX for remote Thermostat
emcu 0:6877fadef7d5 4
emcu 0:6877fadef7d5 5 By: www.emcu.eu
emcu 0:6877fadef7d5 6 Date: 04-January-2017
emcu 0:6877fadef7d5 7
emcu 0:6877fadef7d5 8 NUCLEO-L152RE and AUREL RTX-MID-3V (433.92 MHz - mod. ASK - 3Vcc)
emcu 0:6877fadef7d5 9
emcu 0:6877fadef7d5 10 AUREL RTX-MID-3V
emcu 0:6877fadef7d5 11 Italian AUREL manual is here: http://www.aurelwireless.com/wp-content/uploads/manuale-uso/650201033G_mu.pdf
emcu 0:6877fadef7d5 12 English AUREL manual is here: http://www.aurelwireless.com/en/radiomodem-data-transceivers/
emcu 0:6877fadef7d5 13 https://www.futurashop.it/index.php?route=product/product&filter_name=RTX-MID-3V&product_id=2788
emcu 0:6877fadef7d5 14
emcu 0:6877fadef7d5 15 CONNECTIONS from AUREL RTX-MID-3V to NUCLEO L152RE
emcu 0:6877fadef7d5 16 PIN1 Antenna 17cm
emcu 0:6877fadef7d5 17 PIN2 GND
emcu 0:6877fadef7d5 18 PIN4 TX connected to TX on NUCLEO L152RE
emcu 0:6877fadef7d5 19 PIN5 Tx/Rx connected to PA_10 on NUCLEO L152RE
emcu 0:6877fadef7d5 20 PIN6 ENABLE connected to PB_3 on NUCLEO L152RE
emcu 0:6877fadef7d5 21 PIN7 GND
emcu 0:6877fadef7d5 22 PIN8 Analog Out not connected
emcu 0:6877fadef7d5 23 PIN9 RX connected to RX on NUCLEO L152RE
emcu 0:6877fadef7d5 24 PIN10 VCC connected to 3,3V on NUCLEO L152RE
emcu 0:6877fadef7d5 25
emcu 0:6877fadef7d5 26 NOTE: between GND and VCC put a capacitor of 0,1uF and a second capacitor
emcu 0:6877fadef7d5 27 of 10uF
emcu 0:6877fadef7d5 28
emcu 0:6877fadef7d5 29
emcu 0:6877fadef7d5 30 ********** IMPORTANT IMPORTANT IMPORTANT IMPORTANT **********
emcu 0:6877fadef7d5 31
emcu 0:6877fadef7d5 32 This project use the USART for this reason you must do the below
emcu 0:6877fadef7d5 33 modifications on NUCLEO-L152RE, more info are here:
emcu 0:6877fadef7d5 34 http://www.emcu.it/NUCLEOevaBoards/U2andL152/U2andL152.html#USART2_for_communication_with_shield_or
emcu 0:6877fadef7d5 35
emcu 0:6877fadef7d5 36 Put a jumper on SB62 and on SB63
emcu 0:6877fadef7d5 37 Remove a 0 ohm resistor from SB13 and SB14
emcu 0:6877fadef7d5 38
emcu 0:6877fadef7d5 39 ATTENTION:
emcu 0:6877fadef7d5 40 After this modifications, you lost the possibility to use the virtual comm
emcu 0:6877fadef7d5 41 to connect the NUCLEO to the PC .
emcu 0:6877fadef7d5 42
emcu 0:6877fadef7d5 43 *************************************************************
emcu 0:6877fadef7d5 44
emcu 0:6877fadef7d5 45
emcu 0:6877fadef7d5 46 ********** EXPLANATIONS:
emcu 0:6877fadef7d5 47
emcu 0:6877fadef7d5 48 Build two boards one with RX software and another with TX software.
emcu 0:6877fadef7d5 49 When you press Blue button (and hold it)
emcu 0:6877fadef7d5 50 on the board_TX, the green LED must turn ON, the same 
emcu 0:6877fadef7d5 51 on the board_RX the green LED must turn ON.
emcu 0:6877fadef7d5 52 If you release the Blue button (on the board_TX) the green LED must go OFF
emcu 0:6877fadef7d5 53 on both on the boards.
emcu 0:6877fadef7d5 54
emcu 0:6877fadef7d5 55 *
emcu 0:6877fadef7d5 56 * Permission is hereby granted, free of charge, to any person obtaining a copy
emcu 0:6877fadef7d5 57 * of this software and associated documentation files (the "Software"), to deal
emcu 0:6877fadef7d5 58 * in the Software without restriction, including without limitation the rights
emcu 0:6877fadef7d5 59 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emcu 0:6877fadef7d5 60 * copies of the Software, and to permit persons to whom the Software is
emcu 0:6877fadef7d5 61 * furnished to do so, subject to the following conditions:
emcu 0:6877fadef7d5 62 *
emcu 0:6877fadef7d5 63 * The above copyright notice and this permission notice shall be included in
emcu 0:6877fadef7d5 64 * all copies or substantial portions of the Software.
emcu 0:6877fadef7d5 65 *
emcu 0:6877fadef7d5 66 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emcu 0:6877fadef7d5 67 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emcu 0:6877fadef7d5 68 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emcu 0:6877fadef7d5 69 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emcu 0:6877fadef7d5 70 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emcu 0:6877fadef7d5 71 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
emcu 0:6877fadef7d5 72 * THE SOFTWARE.
emcu 0:6877fadef7d5 73
emcu 0:6877fadef7d5 74
emcu 0:6877fadef7d5 75 */
emcu 0:6877fadef7d5 76
emcu 0:6877fadef7d5 77
emcu 0:6877fadef7d5 78 #include "mbed.h"
emcu 0:6877fadef7d5 79
emcu 0:6877fadef7d5 80 Serial pc(SERIAL_TX, SERIAL_RX); // tx, rx
emcu 0:6877fadef7d5 81 DigitalOut RTX_DIR(PA_10); // 1 == TX 0 == RX
emcu 0:6877fadef7d5 82 DigitalOut RTX_Enable(PB_3); // 1 == RTX enable 0 == RTX disable
emcu 0:6877fadef7d5 83
emcu 0:6877fadef7d5 84 DigitalOut myled(LED1); // This LED is on NUCLEO-L152RE
emcu 0:6877fadef7d5 85 DigitalIn BlueButton(USER_BUTTON); // This is Blue-Button and is on NUCLEO-L153RE
emcu 0:6877fadef7d5 86
emcu 0:6877fadef7d5 87 #define Pressed 0
emcu 0:6877fadef7d5 88 #define NotPressed 1
emcu 0:6877fadef7d5 89 #define Enable 1
emcu 0:6877fadef7d5 90 #define Disable 0
emcu 0:6877fadef7d5 91 #define ACK 2
emcu 0:6877fadef7d5 92 #define OK 1
emcu 0:6877fadef7d5 93 #define FAIL 0
emcu 0:6877fadef7d5 94 #define Received 0
emcu 0:6877fadef7d5 95 #define NotReceived 1
emcu 0:6877fadef7d5 96
emcu 0:6877fadef7d5 97 #define ON 1
emcu 0:6877fadef7d5 98 #define OFF 0
emcu 0:6877fadef7d5 99 #define TX 1
emcu 0:6877fadef7d5 100 #define RX 0
emcu 0:6877fadef7d5 101
emcu 0:6877fadef7d5 102 #define Baud 1200 // 9600
emcu 0:6877fadef7d5 103 #define DlyForEndTX 220 // 220
emcu 0:6877fadef7d5 104 #define MaxReTX 40 // 40
emcu 0:6877fadef7d5 105 #define DebTime 200 // Debounce time
emcu 0:6877fadef7d5 106 #define WaitBeforeToDoRX 200 // 100
emcu 0:6877fadef7d5 107
emcu 0:6877fadef7d5 108 int Car='\0';
emcu 0:6877fadef7d5 109 int RisultatoRX=FAIL;
emcu 0:6877fadef7d5 110 int MemBlueButton=10;
emcu 0:6877fadef7d5 111 int MemOkFail=FAIL;
emcu 0:6877fadef7d5 112 int MemOK=FAIL;
emcu 0:6877fadef7d5 113 int MemOFF=NotReceived;
emcu 0:6877fadef7d5 114 int MemON=NotReceived;
emcu 0:6877fadef7d5 115
emcu 0:6877fadef7d5 116 int MemBB = NotPressed;
emcu 0:6877fadef7d5 117
emcu 0:6877fadef7d5 118
emcu 0:6877fadef7d5 119 int TestRX_Data(void);
emcu 0:6877fadef7d5 120 void RTX_StartUp(void);
emcu 0:6877fadef7d5 121 void RTX_PowerDown(void);
emcu 0:6877fadef7d5 122 void RTX_PowerON(void);
emcu 0:6877fadef7d5 123 void RTX_TX(void);
emcu 0:6877fadef7d5 124 void RTX_RX(void);
emcu 0:6877fadef7d5 125
emcu 0:6877fadef7d5 126 int CarStr[3];
emcu 0:6877fadef7d5 127 int PuntCar = 0;
emcu 0:6877fadef7d5 128
emcu 0:6877fadef7d5 129 void callback() {
emcu 0:6877fadef7d5 130 // Note: you need to actually read from the serial to clear the RX interrupt
emcu 0:6877fadef7d5 131 Car = pc.getc();
emcu 0:6877fadef7d5 132 if (Car == '#' & PuntCar == 0)
emcu 0:6877fadef7d5 133 {
emcu 0:6877fadef7d5 134 PuntCar = 0;
emcu 0:6877fadef7d5 135 CarStr[PuntCar] = Car;
emcu 0:6877fadef7d5 136 PuntCar++;
emcu 0:6877fadef7d5 137 }
emcu 0:6877fadef7d5 138 else if (PuntCar > 0 & PuntCar < 3)
emcu 0:6877fadef7d5 139 {
emcu 0:6877fadef7d5 140 CarStr[PuntCar] = Car;
emcu 0:6877fadef7d5 141 PuntCar++;
emcu 0:6877fadef7d5 142 }
emcu 0:6877fadef7d5 143 }
emcu 0:6877fadef7d5 144
emcu 0:6877fadef7d5 145
emcu 0:6877fadef7d5 146 //
emcu 0:6877fadef7d5 147 // MAIN *****************************************************************
emcu 0:6877fadef7d5 148 //
emcu 0:6877fadef7d5 149 int main()
emcu 0:6877fadef7d5 150 {
emcu 0:6877fadef7d5 151 int n=0;
emcu 0:6877fadef7d5 152 int Rip=3;
emcu 0:6877fadef7d5 153
emcu 0:6877fadef7d5 154 pc.baud(Baud);
emcu 0:6877fadef7d5 155 pc.attach(&callback, pc.RxIrq); // It is for reading under Interrupt the
emcu 0:6877fadef7d5 156 // RX from USART
emcu 0:6877fadef7d5 157
emcu 0:6877fadef7d5 158 // Start Up of the RTX-MID and put it in RX mode
emcu 0:6877fadef7d5 159 RTX_StartUp();
emcu 0:6877fadef7d5 160
emcu 0:6877fadef7d5 161 MemBB = Pressed;
emcu 0:6877fadef7d5 162 MemOFF = NotReceived;
emcu 0:6877fadef7d5 163 MemON = NotReceived;
emcu 0:6877fadef7d5 164
emcu 0:6877fadef7d5 165 while (1)
emcu 0:6877fadef7d5 166 {
emcu 0:6877fadef7d5 167 // Test the status of the Blue button (TX) ************************
emcu 0:6877fadef7d5 168 if ((BlueButton == Pressed) && (MemBB == NotPressed))
emcu 0:6877fadef7d5 169 {
emcu 0:6877fadef7d5 170 wait_ms(DebTime); // Debounce
emcu 0:6877fadef7d5 171 if (BlueButton == Pressed)
emcu 0:6877fadef7d5 172 {
emcu 0:6877fadef7d5 173 MemBB = Pressed;
emcu 0:6877fadef7d5 174 n=0;
emcu 0:6877fadef7d5 175 while (TestRX_Data() != OK)
emcu 0:6877fadef7d5 176 {
emcu 0:6877fadef7d5 177 RTX_TX(); // RTX-MID on TX mode
emcu 0:6877fadef7d5 178 // Send Message
emcu 0:6877fadef7d5 179 pc.printf("#1@"); // Send Command ON
emcu 0:6877fadef7d5 180 wait_ms(DlyForEndTX);
emcu 0:6877fadef7d5 181 RTX_RX(); // RTX-MID on RX mode
emcu 0:6877fadef7d5 182 wait_ms(WaitBeforeToDoRX); // 100
emcu 0:6877fadef7d5 183 for (Rip=0; Rip<40; Rip++) // 40
emcu 0:6877fadef7d5 184 {
emcu 0:6877fadef7d5 185 if (TestRX_Data() == OK)
emcu 0:6877fadef7d5 186 {
emcu 0:6877fadef7d5 187 myled = ON;
emcu 0:6877fadef7d5 188 goto EndBBpressed;
emcu 0:6877fadef7d5 189 // break;
emcu 0:6877fadef7d5 190 }
emcu 0:6877fadef7d5 191 }
emcu 0:6877fadef7d5 192 n++;
emcu 0:6877fadef7d5 193 if (n > MaxReTX)
emcu 0:6877fadef7d5 194 break;
emcu 0:6877fadef7d5 195 }
emcu 0:6877fadef7d5 196 }
emcu 0:6877fadef7d5 197 // myled = ON;
emcu 0:6877fadef7d5 198 wait(2);
emcu 1:c11167ef6bd1 199 RTX_StartUp();
emcu 1:c11167ef6bd1 200 wait(1);
emcu 0:6877fadef7d5 201 MemBB = NotPressed;
emcu 0:6877fadef7d5 202 EndBBpressed:
emcu 0:6877fadef7d5 203 RTX_RX(); // RTX-MID on RX mode
emcu 0:6877fadef7d5 204 }
emcu 0:6877fadef7d5 205
emcu 0:6877fadef7d5 206
emcu 0:6877fadef7d5 207 if ((BlueButton == NotPressed) && (MemBB == Pressed))
emcu 0:6877fadef7d5 208 {
emcu 0:6877fadef7d5 209 wait_ms(DebTime); // Debounce
emcu 0:6877fadef7d5 210 if (BlueButton == NotPressed)
emcu 0:6877fadef7d5 211 {
emcu 0:6877fadef7d5 212 MemBB = NotPressed;
emcu 0:6877fadef7d5 213 n=0;
emcu 0:6877fadef7d5 214 while (TestRX_Data() != ACK)
emcu 0:6877fadef7d5 215 {
emcu 0:6877fadef7d5 216 RTX_TX(); // RTX-MID on TX mode
emcu 0:6877fadef7d5 217 // Send Message
emcu 0:6877fadef7d5 218 pc.printf("#2@"); // Send Command OFF
emcu 0:6877fadef7d5 219 wait_ms(DlyForEndTX);
emcu 0:6877fadef7d5 220 RTX_RX(); // RTX-MID on RX mode
emcu 0:6877fadef7d5 221 wait_ms(WaitBeforeToDoRX); // 100
emcu 0:6877fadef7d5 222 for (Rip=0; Rip<40; Rip++) // 40
emcu 0:6877fadef7d5 223 {
emcu 0:6877fadef7d5 224 if (TestRX_Data() == ACK)
emcu 0:6877fadef7d5 225 {
emcu 0:6877fadef7d5 226 myled = OFF;
emcu 0:6877fadef7d5 227 goto EndBBnotpressed;
emcu 0:6877fadef7d5 228 // break;
emcu 0:6877fadef7d5 229 }
emcu 0:6877fadef7d5 230 }
emcu 0:6877fadef7d5 231 n++;
emcu 0:6877fadef7d5 232 if (n > MaxReTX)
emcu 0:6877fadef7d5 233 break;
emcu 0:6877fadef7d5 234 }
emcu 0:6877fadef7d5 235 }
emcu 0:6877fadef7d5 236 // myled = OFF;
emcu 0:6877fadef7d5 237 wait(2);
emcu 1:c11167ef6bd1 238 RTX_StartUp();
emcu 1:c11167ef6bd1 239 wait(1);
emcu 0:6877fadef7d5 240 MemBB = Pressed;
emcu 0:6877fadef7d5 241 EndBBnotpressed:
emcu 0:6877fadef7d5 242 RTX_RX(); // RTX-MID on RX mode
emcu 0:6877fadef7d5 243 }
emcu 0:6877fadef7d5 244
emcu 0:6877fadef7d5 245 // END Test the status of the Blue button (TX) ************************
emcu 0:6877fadef7d5 246
emcu 0:6877fadef7d5 247
emcu 0:6877fadef7d5 248
emcu 0:6877fadef7d5 249 }
emcu 0:6877fadef7d5 250 }
emcu 0:6877fadef7d5 251
emcu 0:6877fadef7d5 252 //
emcu 0:6877fadef7d5 253 // END MAIN *************************************************************
emcu 0:6877fadef7d5 254 //
emcu 0:6877fadef7d5 255
emcu 0:6877fadef7d5 256
emcu 0:6877fadef7d5 257 //
emcu 0:6877fadef7d5 258 // Decode Message
emcu 0:6877fadef7d5 259 //
emcu 0:6877fadef7d5 260 // REMEMBER:
emcu 0:6877fadef7d5 261 // before to use this function must put in RX mode the RTX
emcu 0:6877fadef7d5 262 //
emcu 0:6877fadef7d5 263 int TestRX_Data(void)
emcu 0:6877fadef7d5 264 {
emcu 0:6877fadef7d5 265 int OkFail=0; // 1==OK==ON 2==ACK==OFF
emcu 0:6877fadef7d5 266
emcu 0:6877fadef7d5 267 if (CarStr[0]=='#' & CarStr[1]=='1' & CarStr[2]=='@')
emcu 0:6877fadef7d5 268 OkFail=1; // OK==ON
emcu 0:6877fadef7d5 269 else if (CarStr[0]=='#' & CarStr[1]=='2' & CarStr[2]=='@')
emcu 0:6877fadef7d5 270 OkFail=2; // ACK==OFF
emcu 0:6877fadef7d5 271 else;
emcu 0:6877fadef7d5 272
emcu 0:6877fadef7d5 273 if (PuntCar >= 3) // Reset the CarStr & PuntCar
emcu 0:6877fadef7d5 274 {
emcu 0:6877fadef7d5 275 CarStr[0] = '0';
emcu 0:6877fadef7d5 276 CarStr[1] = '1';
emcu 0:6877fadef7d5 277 CarStr[2] = '2';
emcu 0:6877fadef7d5 278 PuntCar = 0;
emcu 0:6877fadef7d5 279 }
emcu 0:6877fadef7d5 280
emcu 0:6877fadef7d5 281 return OkFail;
emcu 0:6877fadef7d5 282 }
emcu 0:6877fadef7d5 283
emcu 0:6877fadef7d5 284 void RTX_StartUp(void)
emcu 0:6877fadef7d5 285 {
emcu 0:6877fadef7d5 286 RTX_Enable = Disable;
emcu 0:6877fadef7d5 287 wait_ms(10);
emcu 0:6877fadef7d5 288 RTX_Enable = Enable;
emcu 0:6877fadef7d5 289 wait_ms(1);
emcu 0:6877fadef7d5 290 RTX_DIR = TX; // RTX-MID in TX mode
emcu 0:6877fadef7d5 291 wait_ms(1); // Wait the stabilizzation of the transceiver
emcu 0:6877fadef7d5 292 RTX_Enable = Disable;
emcu 0:6877fadef7d5 293 wait_ms(10);
emcu 0:6877fadef7d5 294 RTX_Enable = Enable;
emcu 0:6877fadef7d5 295 wait_ms(1);
emcu 0:6877fadef7d5 296 RTX_DIR = RX; // RTX-MID in RX mode
emcu 0:6877fadef7d5 297 wait_ms(1); // Wait the stabilizzation of the transceiver
emcu 0:6877fadef7d5 298 RTX_Enable = Disable;
emcu 0:6877fadef7d5 299 wait_ms(1);
emcu 0:6877fadef7d5 300 RTX_Enable = Enable;
emcu 0:6877fadef7d5 301 wait_ms(1);
emcu 0:6877fadef7d5 302 }
emcu 0:6877fadef7d5 303
emcu 0:6877fadef7d5 304 void RTX_PowerDown(void)
emcu 0:6877fadef7d5 305 {
emcu 0:6877fadef7d5 306 RTX_Enable = Disable;
emcu 0:6877fadef7d5 307 wait_ms(10);
emcu 0:6877fadef7d5 308 }
emcu 0:6877fadef7d5 309
emcu 0:6877fadef7d5 310 void RTX_PowerON(void)
emcu 0:6877fadef7d5 311 {
emcu 0:6877fadef7d5 312 RTX_Enable = Disable;
emcu 0:6877fadef7d5 313 wait_ms(1);
emcu 0:6877fadef7d5 314 }
emcu 0:6877fadef7d5 315
emcu 0:6877fadef7d5 316 void RTX_TX(void)
emcu 0:6877fadef7d5 317 {
emcu 0:6877fadef7d5 318 // RTX_Enable = Disable;
emcu 0:6877fadef7d5 319 // wait_ms(10);
emcu 0:6877fadef7d5 320 // RTX_Enable = Enable;
emcu 0:6877fadef7d5 321 // wait_ms(1);
emcu 0:6877fadef7d5 322 RTX_DIR = TX; // RTX-MID in TX mode
emcu 0:6877fadef7d5 323 wait_us(400); // Wait the stabilizzation of the transceiver
emcu 0:6877fadef7d5 324 }
emcu 0:6877fadef7d5 325
emcu 0:6877fadef7d5 326 void RTX_RX(void)
emcu 0:6877fadef7d5 327 {
emcu 0:6877fadef7d5 328 // RTX_Enable = Disable;
emcu 0:6877fadef7d5 329 // wait_ms(10);
emcu 0:6877fadef7d5 330 // RTX_Enable = Enable;
emcu 0:6877fadef7d5 331 // wait_ms(1);
emcu 0:6877fadef7d5 332 // RTX_DIR = TX; // RTX-MID in TX mode
emcu 0:6877fadef7d5 333 // wait_ms(1); // Wait the stabilizzation of the transceiver
emcu 0:6877fadef7d5 334 RTX_DIR = RX; // RTX-MID in RX mode
emcu 0:6877fadef7d5 335 wait_us(40);
emcu 0:6877fadef7d5 336 RTX_Enable = Disable;
emcu 0:6877fadef7d5 337 wait_us(20);
emcu 0:6877fadef7d5 338 RTX_Enable = Enable;
emcu 0:6877fadef7d5 339 wait_us(200);
emcu 0:6877fadef7d5 340 }
emcu 0:6877fadef7d5 341
emcu 0:6877fadef7d5 342