Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ask_rxtx.ino - V2
- AUTHOR: Edward Comer, USA
- Open Source Licenesed under Creative Commons Attribution 4.0 International (CC BY 4.0)
- See: https://creativecommons.org/licenses/by/4.0/
- ------------
- Uses RadioHead library for messaging via a simple ASK transmitter and
- receiver, i.e., FS1000A and MX-RM-5V, etc.
- For RadioHead information, see:
- http://www.airspayce.com/mikem/arduino/RadioHead/classRH__ASK.html#details
- ------------
- Controls both transmitter AND receiver
- Receiver is powered by inverted PTTPIN, which powers down receiver when
- transmitting in order to prevent swamping the receiver.
- Receiver MX-RM-5v Working current:≤5.5mA (5.0VDC) nominally 4ma
- Arduino DC Current per I/O Pin: 40.0 mA
- +-------+ +---------+
- |FS1000A| |XD-RF-5V |
- |D 5 G | | 5 D G |
- ++-+-+--+ +-+-+--+--+
- | | | | | |
- | + + | | +
- | | |
- +-+------------+-+--+
- | 1 1 1 |
- | 2 0 1 |
- | ARDUINO |
- +-------------------+
- */
- #include <RH_ASK.h>
- #include <stdint.h>
- #include <SPI.h> // Not actually used but needed to compile
- #define SPEED 2000 // The desired bit rate in bits per second
- #define RXPIN 11 // The pin that is used to get data from the receiver
- #define TXPIN 12 // The pin that is used to send data to the transmitter
- #define PTTPIN 10 // Push To Talk pin
- #define PTTINVERTED true // true = LOW when transmitting, otherwise HIGH
- RH_ASK driver(SPEED, RXPIN, TXPIN, PTTPIN, PTTINVERTED);
- char kbdbuf[RH_ASK_MAX_MESSAGE_LEN];
- char rxbuf[RH_ASK_MAX_MESSAGE_LEN];
- uint8_t rxbuflen = sizeof(rxbuf);
- const int ledPin = 13; // onboard LED
- void setup()
- {
- digitalWrite(ledPin, HIGH);
- Serial.begin(19200);
- while (!Serial); // Wait for serial connect
- if (!driver.init())
- {
- Serial.println("Radiohead driver.init failed - system halting");
- while (true) {} // do nothing further until reset
- }
- digitalWrite(ledPin, LOW);
- Serial.println("");
- Serial.print("Maximum message length requested of this Driver is ");
- Serial.print(RH_ASK_MAX_MESSAGE_LEN);
- Serial.println("");
- Serial.print("Maximum message length available in this Driver is ");
- Serial.print(driver.maxMessageLength());
- Serial.println("");
- Serial.println("Setup complete.");
- Serial.println("");
- Serial.println("Enter New message: ");
- }
- /*
- loop, contstantly checking for either a string to transmit or
- a received string. This loop is predominantly non-blocking. The receiver
- is powered down when the transmitter is sending to prevent swamping the
- receiver. Power down/up is accomplished by powering the receiver with
- the RadioHead PTT pin (pin 10). The receiver uses 5V @ 4ma and the
- Arduino can source 40ma.
- NOTE: There is no immediate local echo of characters typed
- */
- void loop()
- {
- if (readline(Serial.read(), kbdbuf, RH_ASK_MAX_MESSAGE_LEN) > 0)
- {
- Serial.print("You entered: [");
- Serial.print(kbdbuf);
- Serial.println("]");
- // Now, Send user typed string via radio
- driver.send(kbdbuf, strlen(kbdbuf));
- driver.waitPacketSent();
- Serial.println("Transmit complete");
- delay(500); // settling time for RX turnon
- memset(kbdbuf,0,sizeof(kbdbuf));
- }
- // Check for data received via radio
- if (driver.recv(rxbuf, &rxbuflen)) // Non-blocking
- {
- // Message with a good checksum received, dump it.
- //driver.printBuffer("Received:", buf, QtyRead);
- Serial.write(0x07); // Ring bell on Putty console
- Serial.print("Received: ");
- Serial.println(rxbuf);
- Serial.println("-------------");
- memset(rxbuf,0,sizeof(rxbuf));
- rxbuflen = sizeof(rxbuf);
- Serial.println("Enter New message: ");
- }
- }
- /**
- Non-blocking string read from terminal
- name: readline(int readchar, char* buffer, int len)
- @param readchar is the character read
- @param buffer is storage for accumulated string
- @param len is maximum length input string allowed
- @return returns length of string read, otherwise negative value
- Inspired by Majenko's Hardware Hacking Blog
- http://tinyurl.com/hmb8vwa
- */
- int readline(int readchar, char* buffer, int len)
- {
- static int pos = 0;
- int ret_pos;
- if (readchar > 0) {
- switch (readchar) {
- case '\r': // Terminate with CR or LF
- case '\n':
- ret_pos = pos;
- pos = 0; // Reset position index ready for next entry
- return ret_pos;
- default:
- if (pos < len - 1) {
- buffer[pos++] = readchar;
- buffer[pos] = 0;
- } else { // else buffer is full
- buffer[pos] = 0;
- ret_pos = pos;
- pos = 0; // Reset position index ready for next entry
- return ret_pos;
- }
- }
- }
- // No end of line has been found, so return -1.
- return -1;
- }
Add Comment
Please, Sign In to add comment