Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <RFM69.h>
  2. #include <RFM69_ATC.h>
  3. #include <SPIFlash.h>
  4. #include <SPI.h>
  5. #include "U8glib.h"
  6. U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);
  7.  
  8. #define NODEID 1 //unique for each node on same network
  9. #define NETWORKID 100 //the same on all nodes that talk to each other
  10. #define FREQUENCY RF69_433MHZ
  11. #define ENCRYPTKEY "syauqiEncryptKey" //exactly the same 16 characters/bytes on all nodes!
  12. #define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
  13. #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
  14. #define SERIAL_BAUD 115200
  15.  
  16.  
  17. #if defined (MOTEINO_M0) && defined(SERIAL_PORT_USBVIRTUAL)
  18. #define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
  19.  
  20. #endif
  21.  
  22. #ifdef ENABLE_ATC
  23. RFM69_ATC radio;
  24. #else
  25. RFM69 radio;
  26. #endif
  27.  
  28. bool promiscuousMode = false;
  29. byte ackCount = 0;
  30. uint32_t packetCount = 0;
  31.  
  32. unsigned long currentMillis;
  33. unsigned long node1_last, node2_last, node3_last;
  34. unsigned long node_periode = 1500;
  35.  
  36. String notif[3];
  37. int urutan = 0;
  38.  
  39. void(* reset)(void) = 0;
  40.  
  41. void setup() {
  42. pinMode (10, OUTPUT);
  43. digitalWrite (10, LOW);
  44. Serial.begin(SERIAL_BAUD);
  45. delay(10);
  46. radio.initialize(FREQUENCY,NODEID,NETWORKID);
  47. #ifdef IS_RFM69HW_HCW
  48. radio.setHighPower(); //must include this only for RFM69HW/HCW!
  49. #endif
  50. radio.encrypt(ENCRYPTKEY);
  51. radio.promiscuous(promiscuousMode);
  52. pinMode(8, OUTPUT);
  53. char buff[50];
  54. }
  55.  
  56. void draw(void) {
  57. String forshow = "";
  58. for(int i = 0; i < urutan; i++){
  59. forshow += notif[i];
  60. forshow += " ";
  61. }
  62. u8g.setFont(u8g_font_unifont);
  63. u8g.drawStr( 0, 10, "Syauqi TA 2019");
  64. u8g.drawStr( 0, 30, forshow.c_str());
  65. }
  66.  
  67. void updateNotif(String node, int state){
  68. if(state == 1){
  69. bool allow = true;
  70.  
  71. for(int i = 0; i < urutan; i++){
  72. if(notif[i].equals(node)){
  73. allow = false;
  74. }
  75. }
  76.  
  77. if(allow){
  78. if(urutan < 3){
  79. notif[urutan] = node;
  80. urutan++;
  81. }
  82. }
  83. }
  84. else if(state == 0){
  85. bool allow = false;
  86.  
  87. for(int i = 0; i < urutan; i++){
  88. if(notif[i].equals(node)){
  89. allow = true;
  90. }
  91. }
  92.  
  93. if(allow){
  94. if(urutan > 0){
  95. int found = 0;
  96. for(int i = 0; i < urutan; i++){
  97. if(notif[i].equals(node)){
  98. found = i;
  99. }
  100. break;
  101. }
  102. notif[found] = "";
  103. String temp[3];
  104. int tambah = 0;
  105. for(int j = 0; j < urutan; j++){
  106. if(!notif[j].equals("")){
  107. temp[tambah] = notif[j];
  108. tambah++;
  109. }
  110. }
  111.  
  112. for(int k = 0; k < tambah; k++){
  113. notif[k] = temp[k];
  114. }
  115. urutan--;
  116. }
  117. }
  118. }
  119. }
  120.  
  121. void showNotif(){
  122. for(int i = 0; i < urutan; i++){
  123. Serial.print(notif[i]);
  124. Serial.print(" ");
  125. }
  126. Serial.println("");
  127. }
  128.  
  129. void loop() {
  130. currentMillis = millis();
  131. if((currentMillis - node1_last) >= node_periode){
  132. updateNotif("A",1);
  133. digitalWrite (10, HIGH);
  134. }
  135. else{
  136. updateNotif("A",0);
  137. digitalWrite (10, LOW);
  138. }
  139.  
  140. if((currentMillis - node2_last) >= node_periode){
  141. updateNotif("B",1);
  142. digitalWrite (10, HIGH);
  143. }
  144. else{
  145. updateNotif("B",0);
  146. digitalWrite (10, LOW);
  147. }
  148.  
  149. if((currentMillis - node3_last) >= node_periode){
  150. updateNotif("C",1);
  151. digitalWrite (10, HIGH);
  152. }
  153. else{
  154. updateNotif("C",0);
  155. digitalWrite (10, LOW);
  156. }
  157.  
  158. u8g.firstPage();
  159. do {
  160. draw();
  161. } while( u8g.nextPage() );
  162.  
  163.  
  164. if (radio.receiveDone())
  165. {
  166. Serial.print(++packetCount);
  167.  
  168. Serial.print(" Node:[");
  169. Serial.print(radio.SENDERID, DEC);
  170. Serial.println("] ");
  171.  
  172. int node_id = radio.SENDERID;
  173. switch(node_id){
  174. case 2:
  175. node1_last = currentMillis;
  176. break;
  177. case 3:
  178. node2_last = currentMillis;
  179. break;
  180. case 4:
  181. node3_last = currentMillis;
  182. break;
  183. }
  184. for (byte i = 0; i < radio.DATALEN; i++)
  185. Serial.print((char)radio.DATA[i]);
  186. Serial.print(" [RSSI:");Serial.print(radio.RSSI);Serial.print("]");
  187. Serial.println();
  188. Blink(LED_BUILTIN,3);
  189. }
  190. }
  191.  
  192. void Blink(byte PIN, int DELAY_MS)
  193. {
  194. pinMode(PIN, OUTPUT);
  195. digitalWrite(PIN,HIGH);
  196. delay(DELAY_MS);
  197. digitalWrite(PIN,LOW);
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement