Mishan150

Reciever_test

Jul 16th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <LoRa.h>
  3.  
  4.  
  5. const int OK_code = 0xFF;
  6. const int Write_code = 0xAC;
  7. int32_t addr = 0;
  8. float val = 325.87;
  9. int send_step = 10950;
  10.  
  11. typedef union union4byte_t {
  12.   uint8_t Bytes[4];
  13.   int32_t Int;
  14.   int16_t SmalInt[2];
  15.   float Float;
  16. };
  17. union4byte_t BufferUnion;
  18. const size_t BufferSize = 8;
  19. uint8_t Buffer[BufferSize];
  20.  
  21. void Reciever() {
  22.   // try to parse packet
  23.   int packetSize = LoRa.parsePacket();
  24.   if (packetSize) {
  25.     Serial.print(" recieved packet: ");
  26.     while (LoRa.available()) {
  27.       for (size_t i = 0; i < BufferSize; ++i) {
  28.         Buffer[i] = LoRa.read();
  29.       }
  30.       Serial.write(Buffer, BufferSize);
  31.     }
  32.     Serial.println();
  33.     //Send();
  34.   }  
  35. }
  36.  
  37.  
  38. void Send() {
  39.   LoRa.setTxPower(20);
  40.   //Serial.print("Sending answer ");
  41.   for (int i = 0; i < 10; i++) {
  42.     BufferUnion.SmalInt[0] = OK_code;
  43.     BufferUnion.SmalInt[1] = send_step;
  44.     Buffer[4] = BufferUnion.Bytes[0];
  45.     Buffer[5] = BufferUnion.Bytes[1];
  46.     Buffer[6] = BufferUnion.Bytes[2];
  47.     Buffer[7] = BufferUnion.Bytes[3];//*/    
  48.     LoRa.beginPacket();
  49.     LoRa.write(Buffer, BufferSize);
  50.     LoRa.endPacket();
  51.     delay(1);
  52.   }
  53.   //Serial.println(" Answer sended");
  54. }
  55.  
  56. void setup() {
  57.   Serial.begin(9600);
  58.   while (!Serial);
  59.  
  60.   if (!LoRa.begin(430E6)) {
  61.     Serial.println("Starting LoRa failed!");
  62.     while (1);
  63.   }
  64.   Serial.println("LoRa кусшумук starting");
  65.   LoRa.enableCrc();
  66. }
  67.  
  68. void loop() {
  69.   int packetSize = LoRa.parsePacket();
  70.   if (packetSize == 8) {
  71.     //Serial.print(" recieved packet: ");
  72.     while (LoRa.available()) {
  73.       for (size_t i = 0; i < BufferSize; ++i) {
  74.         Buffer[i] = LoRa.read();
  75.       }
  76.       Serial.write(Buffer, BufferSize);
  77.     }
  78.     //Serial.println();
  79.     Send();
  80.   }  
  81. }
Add Comment
Please, Sign In to add comment