Advertisement
193030

NS_GS_01 16: 36 2 way simple communication

Sep 6th, 2021
1,005
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None
  1. // Arduino data pin - 11
  2. // Include RadioHead Amplitude Shift Keying Library
  3. #include <RH_ASK.h>
  4. // Include dependant SPI Library
  5. #include <SPI.h>
  6. int state = 0;
  7. #define LISTEN_SAT 0
  8. #define SEND_TO_SAT 1
  9. #define BUFF_LENGTH 30
  10. // Create Amplitude Shift Keying Object
  11. RH_ASK rf_driver;
  12. String currentMsg = "";
  13.  
  14. char *msgToSend = "AA|03|NULL|120|aa";
  15.  
  16.  
  17. void setup() {
  18.   state = SEND_TO_SAT;
  19.   rf_driver.init();
  20.     // Setup Serial Monitor
  21.   Serial.begin(9600);
  22.   Serial.println("serial is opening SAT");
  23.  
  24. }
  25. void readFromSat()
  26. {
  27. //    Serial.printl/n("reading state");
  28.     uint8_t buf[BUFF_LENGTH];
  29.     uint8_t buflen = sizeof(buf);
  30.     // Check if received packet is correct size
  31.     if (rf_driver.recv(buf, &buflen))
  32.     {
  33.      
  34.       // Message received with valid checksum
  35.       Serial.print("Message Received: ");
  36.       Serial.println((char*)buf);
  37.       currentMsg = (char*)buf;        
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. void sendMsg(char *inputMsgToSend, int times)
  44. {
  45.    Serial.println("sending msg" );
  46.    for(int i =0; i<times; i++)
  47.    {
  48.     rf_driver.send((uint8_t *)inputMsgToSend,strlen(inputMsgToSend));
  49.     rf_driver.waitPacketSent();
  50.     delay(10);
  51.    }
  52.    
  53. }
  54.  
  55.  
  56. void loop() {
  57.   switch(state)
  58.   {
  59.     case LISTEN_SAT:
  60.       readFromSat();
  61.     break;
  62.  
  63.     case SEND_TO_SAT:
  64.       Serial.println("State send");
  65.       sendMsg(msgToSend,5);
  66.       state = LISTEN_SAT;
  67.     break;
  68.   }
  69. //  Serial.println/(currentMsg);
  70.  
  71. }
Advertisement
RAW Paste Data Copied
Advertisement