Advertisement
193030

NS_SAT_01 16: 36 2 way simple communication

Sep 6th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  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_GS 0
  8. #define SEND_TO_GS 1
  9. // Create Amplitude Shift Keying Object
  10. RH_ASK rf_driver;
  11. String currentMsg = "";
  12. char *msgToSend = "SATELLITE|03|NULL|120|aa";
  13.  
  14.  
  15. void setup() {
  16. state = SEND_TO_GS;
  17. rf_driver.init();
  18. // Setup Serial Monitor
  19. Serial.begin(9600);
  20. Serial.println("serial is opening SAT");
  21.  
  22. }
  23. void sendMsg(char *inputMsgToSend, int times)
  24. {
  25. Serial.println("sending msg" );
  26.  
  27. for(int i =0; i<times; i++)
  28. {
  29. rf_driver.send((uint8_t *)inputMsgToSend,strlen(inputMsgToSend));
  30. rf_driver.waitPacketSent();
  31. delay(10);
  32. }
  33.  
  34. }
  35.  
  36. void readFromGs()
  37. {
  38. uint8_t buf[24];
  39. uint8_t buflen = sizeof(buf);
  40. // Check if received packet is correct size
  41. if (rf_driver.recv(buf, &buflen))
  42. {
  43.  
  44. // Message received with valid checksum
  45. Serial.print("Message Received: ");
  46. Serial.println((char*)buf);
  47. currentMsg = (char*)buf;
  48. }
  49. }
  50.  
  51. void loop() {
  52. switch(state)
  53. {
  54. case LISTEN_GS:
  55. readFromGs();
  56. break;
  57.  
  58. case SEND_TO_GS:
  59. sendMsg(msgToSend,3);
  60. state = LISTEN_GS;
  61.  
  62. break;
  63. }
  64. readFromGs();
  65. // Serial.println(curre/ntMsg);
  66.  
  67. }
  68.  
  69.  
  70. //-------------------------------------------------
  71. String gs_id_str;
  72. String gs_command_str;
  73. String gs_param_str;
  74.  
  75.  
  76. void getInfoFromMsg( char* inputMsg)
  77. {
  78. String tempStr = String(inputMsg);
  79.  
  80. // Serial.println(tempStr.substring(3,5));
  81. // Serial.println("info starts: ");
  82. int firstDel = tempStr.indexOf('|',2);
  83. String gsID = tempStr.substring(firstDel+1, firstDel+3);
  84. gs_id_str = gsID;
  85.  
  86.  
  87. int secondDel = tempStr.indexOf('|', firstDel +1 );
  88. int thirdDel = tempStr.indexOf('|', secondDel+1);
  89. String gsCommand = tempStr.substring(secondDel+1, thirdDel);
  90. gs_command_str = gsCommand;
  91.  
  92. //
  93. int fourthDel = tempStr.indexOf('|', thirdDel+1);
  94. String gsCommandParameter = tempStr.substring(thirdDel+1, fourthDel);
  95. Serial.print("GS id: ");
  96. Serial.println(gsID);
  97. Serial.print("Gs command: ");
  98. Serial.println(gsCommand);
  99. Serial.print("GS command parameter: ");
  100. Serial.print(gsCommandParameter); Serial.print(" ");
  101. gs_param_str = gsCommandParameter;
  102. // Serial.println();
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement