Guest User

Untitled

a guest
Nov 28th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. // **********************************************************************************
  2. // Struct Send RFM69 Example
  3. // **********************************************************************************
  4. // Copyright Felix Rusu 2018, http://www.LowPowerLab.com/contact
  5. // **********************************************************************************
  6. // License
  7. // **********************************************************************************
  8. // This program is free software; you can redistribute it
  9. // and/or modify it under the terms of the GNU General
  10. // Public License as published by the Free Software
  11. // Foundation; either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will
  15. // be useful, but WITHOUT ANY WARRANTY; without even the
  16. // implied warranty of MERCHANTABILITY or FITNESS FOR A
  17. // PARTICULAR PURPOSE. See the GNU General Public
  18. // License for more details.
  19. //
  20. // Licence can be viewed at
  21. // http://www.gnu.org/licenses/gpl-3.0.txt
  22. //
  23. // Please maintain this license information along with authorship
  24. // and copyright notices in any redistribution of this code
  25. // **********************************************************************************
  26. #include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
  27. #include <RFM69_ATC.h> //get it here: https://www.github.com/lowpowerlab/rfm69
  28. #include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash
  29. #include <SPI.h> //included with Arduino IDE install (www.arduino.cc)
  30. //*********************************************************************************************
  31. //************ IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
  32. //*********************************************************************************************
  33. #define NODEID 99
  34. #define NETWORKID 100
  35. #define GATEWAYID 1
  36. //Match frequency to the hardware version of the radio on your Moteino (uncomment one):
  37. //#define FREQUENCY RF69_433MHZ
  38. //#define FREQUENCY RF69_868MHZ
  39. #define FREQUENCY RF69_915MHZ
  40. #define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
  41. #define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
  42. //*********************************************************************************************
  43. //Auto Transmission Control - dials down transmit power to save battery
  44. //Usually you do not need to always transmit at max output power
  45. //By reducing TX power even a little you save a significant amount of battery power
  46. //This setting enables this gateway to work with remote nodes that have ATC enabled to
  47. //dial their power down to only the required level
  48. #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
  49. //*********************************************************************************************
  50. #define SERIAL_BAUD 115200
  51.  
  52. #ifdef ENABLE_ATC
  53. RFM69_ATC radio;
  54. #else
  55. RFM69 radio;
  56. #endif
  57.  
  58. SPIFlash flash(10, 0xEF30); //EF40 for 16mbit windbond chip
  59.  
  60. int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)
  61. byte sendSize=0;
  62. boolean requestACK = false;
  63.  
  64. typedef struct {
  65. int nodeId; //store this nodeId
  66. unsigned long uptime; //uptime in ms
  67. float temp; //temperature maybe?
  68. } Payload;
  69. Payload theData;
  70.  
  71. void setup() {
  72. Serial.begin(SERIAL_BAUD);
  73. radio.initialize(FREQUENCY,NODEID,NETWORKID);
  74. #ifdef IS_RFM69HW_HCW
  75. radio.setHighPower(); //must include this only for RFM69HW/HCW!
  76. #endif
  77. radio.encrypt(ENCRYPTKEY);
  78. char buff[50];
  79. sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  80. Serial.println(buff);
  81.  
  82. if (flash.initialize())
  83. Serial.println("SPI Flash Init OK!");
  84. else
  85. Serial.println("SPI Flash Init FAIL! (is chip present?)");
  86. }
  87.  
  88. long lastPeriod = -1;
  89. void loop() {
  90. //process any serial input
  91. if (Serial.available() > 0)
  92. {
  93. char input = Serial.read();
  94. if (input >= 48 && input <= 57) //[0,9]
  95. {
  96. TRANSMITPERIOD = 100 * (input-48);
  97. if (TRANSMITPERIOD == 0) TRANSMITPERIOD = 1000;
  98. Serial.print("\nChanging delay to ");
  99. Serial.print(TRANSMITPERIOD);
  100. Serial.println("ms\n");
  101. }
  102.  
  103. if (input == 'r') //d=dump register values
  104. radio.readAllRegs();
  105. //if (input == 'E') //E=enable encryption
  106. // radio.encrypt(ENCRYPTKEY);
  107. //if (input == 'e') //e=disable encryption
  108. // radio.encrypt(null);
  109.  
  110. if (input == 'd') //d=dump flash area
  111. {
  112. Serial.println("Flash content:");
  113. int counter = 0;
  114.  
  115. while(counter<=256){
  116. Serial.print(flash.readByte(counter++), HEX);
  117. Serial.print('.');
  118. }
  119. while(flash.busy());
  120. Serial.println();
  121. }
  122. if (input == 'e')
  123. {
  124. Serial.print("Erasing Flash chip ... ");
  125. flash.chipErase();
  126. while(flash.busy());
  127. Serial.println("DONE");
  128. }
  129. if (input == 'i')
  130. {
  131. Serial.print("DeviceID: ");
  132. word jedecid = flash.readDeviceId();
  133. Serial.println(jedecid, HEX);
  134. }
  135. }
  136.  
  137. //check for any received packets
  138. if (radio.receiveDone())
  139. {
  140. Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
  141. for (byte i = 0; i < radio.DATALEN; i++)
  142. Serial.print((char)radio.DATA[i]);
  143. Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]");
  144.  
  145. if (radio.ACKRequested())
  146. {
  147. radio.sendACK();
  148. Serial.print(" - ACK sent");
  149. delay(10);
  150. }
  151. Blink(LED_BUILTIN,5);
  152. Serial.println();
  153. }
  154.  
  155. int currPeriod = millis()/TRANSMITPERIOD;
  156. if (currPeriod != lastPeriod)
  157. {
  158. //fill in the struct with new values
  159. theData.nodeId = NODEID;
  160. theData.uptime = millis();
  161. theData.temp = 91.23; //it's hot!
  162.  
  163. Serial.print("Sending struct (");
  164. Serial.print(sizeof(theData));
  165. Serial.print(" bytes) ... ");
  166. if (radio.sendWithRetry(GATEWAYID, (const void*)(&theData), sizeof(theData)))
  167. Serial.print(" ok!");
  168. else Serial.print(" nothing...");
  169. Serial.println();
  170. Blink(LED_BUILTIN,3);
  171. lastPeriod=currPeriod;
  172. }
  173. }
  174.  
  175. void Blink(byte PIN, int DELAY_MS)
  176. {
  177. pinMode(PIN, OUTPUT);
  178. digitalWrite(PIN,HIGH);
  179. delay(DELAY_MS);
  180. digitalWrite(PIN,LOW);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment