celem

ask_rxtx.ino

Nov 20th, 2016
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. /*
  2.   ask_rxtx.ino - V2
  3.   AUTHOR: Edward Comer, USA
  4.   Open Source Licenesed under Creative Commons Attribution 4.0 International (CC BY 4.0)
  5.   See: https://creativecommons.org/licenses/by/4.0/
  6.   ------------
  7.   Uses RadioHead library for messaging via a simple ASK transmitter and
  8.   receiver, i.e., FS1000A and MX-RM-5V, etc.
  9.  
  10.   For RadioHead information, see:
  11.   http://www.airspayce.com/mikem/arduino/RadioHead/classRH__ASK.html#details
  12.   ------------
  13.   Controls both transmitter AND receiver
  14.   Receiver is powered by inverted PTTPIN, which powers down receiver when
  15.   transmitting in order to prevent swamping the receiver.
  16.   Receiver MX-RM-5v Working current:≤5.5mA (5.0VDC) nominally 4ma
  17.   Arduino DC Current per I/O Pin: 40.0 mA
  18.  
  19.    +-------+   +---------+
  20.    |FS1000A|   |XD-RF-5V |
  21.    |D 5 G  |   | 5 D  G  |
  22.    ++-+-+--+   +-+-+--+--+
  23.     | | |        | |  |
  24.     | + +        | |  +
  25.     |            | |
  26.   +-+------------+-+--+
  27.   | 1            1 1  |
  28.   | 2            0 1  |
  29.   |    ARDUINO        |
  30.   +-------------------+
  31. */
  32.  
  33.  
  34. #include <RH_ASK.h>
  35. #include <stdint.h>
  36. #include <SPI.h>    // Not actually used but needed to compile
  37.  
  38. #define SPEED 2000  // The desired bit rate in bits per second
  39. #define RXPIN 11    // The pin that is used to get data from the receiver
  40. #define TXPIN 12    // The pin that is used to send data to the transmitter
  41. #define PTTPIN 10   // Push To Talk pin
  42. #define PTTINVERTED true // true = LOW when transmitting, otherwise HIGH
  43.  
  44. RH_ASK driver(SPEED, RXPIN, TXPIN, PTTPIN, PTTINVERTED);
  45.  
  46. char kbdbuf[RH_ASK_MAX_MESSAGE_LEN];
  47. char rxbuf[RH_ASK_MAX_MESSAGE_LEN];
  48. uint8_t rxbuflen = sizeof(rxbuf);
  49.  
  50.  
  51. const int ledPin = 13; // onboard LED
  52.  
  53. void setup()
  54. {
  55.   digitalWrite(ledPin, HIGH);
  56.   Serial.begin(19200);
  57.   while (!Serial); // Wait for serial connect
  58.   if (!driver.init())
  59.   {
  60.     Serial.println("Radiohead driver.init failed - system halting");
  61.     while (true) {}  // do nothing further until reset
  62.   }
  63.   digitalWrite(ledPin, LOW);
  64.   Serial.println("");
  65.   Serial.print("Maximum message length requested of this Driver is ");
  66.   Serial.print(RH_ASK_MAX_MESSAGE_LEN);
  67.   Serial.println("");
  68.   Serial.print("Maximum message length available in this Driver is ");
  69.   Serial.print(driver.maxMessageLength());
  70.   Serial.println("");
  71.   Serial.println("Setup complete.");
  72.   Serial.println("");
  73.   Serial.println("Enter New message: ");
  74. }
  75.  
  76. /*
  77.    loop, contstantly checking for either a string to transmit or
  78.    a received string. This loop is predominantly non-blocking. The receiver
  79.    is powered down when the transmitter is sending to prevent swamping the
  80.    receiver. Power down/up is accomplished by powering the receiver with
  81.    the RadioHead PTT pin (pin 10). The receiver uses 5V @ 4ma and the
  82.    Arduino can source 40ma.
  83.  
  84.    NOTE: There is no immediate local echo of characters typed
  85. */
  86. void loop()
  87. {
  88.   if (readline(Serial.read(), kbdbuf, RH_ASK_MAX_MESSAGE_LEN) > 0)
  89.   {
  90.     Serial.print("You entered: [");
  91.     Serial.print(kbdbuf);
  92.     Serial.println("]");
  93.     // Now, Send user typed string via radio
  94.     driver.send(kbdbuf, strlen(kbdbuf));
  95.     driver.waitPacketSent();
  96.     Serial.println("Transmit complete");
  97.     delay(500);  // settling time for RX turnon
  98.     memset(kbdbuf,0,sizeof(kbdbuf));
  99.   }
  100.  
  101.   // Check for data received via radio
  102.   if (driver.recv(rxbuf, &rxbuflen)) // Non-blocking
  103.   {
  104.     // Message with a good checksum received, dump it.
  105.     //driver.printBuffer("Received:", buf, QtyRead);
  106.     Serial.write(0x07); // Ring bell on Putty console
  107.     Serial.print("Received: ");
  108.     Serial.println(rxbuf);
  109.     Serial.println("-------------");
  110.     memset(rxbuf,0,sizeof(rxbuf));
  111.     rxbuflen = sizeof(rxbuf);
  112.     Serial.println("Enter New message: ");
  113.   }
  114. }
  115.  
  116. /**
  117.    Non-blocking string read from terminal
  118.    name: readline(int readchar, char* buffer, int len)
  119.    @param readchar is the character read
  120.    @param buffer is storage for accumulated string
  121.    @param len is maximum length input string allowed
  122.    @return returns length of string read, otherwise negative value
  123.  
  124.    Inspired by Majenko's Hardware Hacking Blog
  125.    http://tinyurl.com/hmb8vwa
  126. */
  127. int readline(int readchar, char* buffer, int len)
  128. {
  129.   static int pos = 0;
  130.   int ret_pos;
  131.  
  132.   if (readchar > 0) {
  133.     switch (readchar) {
  134.       case '\r': // Terminate with CR or LF
  135.       case '\n':
  136.         ret_pos = pos;
  137.         pos = 0; // Reset position index ready for next entry
  138.         return ret_pos;
  139.       default:
  140.         if (pos < len - 1) {
  141.           buffer[pos++] = readchar;
  142.           buffer[pos] = 0;
  143.         } else {    // else buffer is full
  144.           buffer[pos] = 0;
  145.           ret_pos = pos;
  146.           pos = 0; // Reset position index ready for next entry
  147.           return ret_pos;
  148.         }
  149.     }
  150.   }
  151.   // No end of line has been found, so return -1.
  152.   return -1;
  153. }
Add Comment
Please, Sign In to add comment