Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <stdio.h>
  3.  
  4. // Librerias agregadas //
  5. #include <SFE_BMP180.h>
  6. #include <Wire.h>
  7. SFE_BMP180 bmp180;
  8.  
  9.  
  10. SoftwareSerial loraSerial(10, 11); //RX TX
  11.  
  12. /// Variables del programa ///
  13. String bandera= "";
  14. String str;
  15. String eui= "";
  16.  
  17. int inicializacion = 0;
  18.  
  19. // Variables modificables //
  20. String str2 = "02"; // Llenar con el valor de bandera seleccionado
  21. String decimales = "2"; // Llenar con el valor de decimales seleccionado
  22. String str1 = ""; //Se va a tomar de datos
  23.  
  24. /// Variables Agregadas ///
  25. long presion;
  26.  
  27. void setup() {
  28.  
  29. Serial.println("Iniciando LoRa");
  30. Serial.begin(57600);
  31. loraSerial.begin(9600);
  32. loraSerial.setTimeout(100); // Revisar..
  33. lora_autobaud();
  34.  
  35. loraSerial.listen();
  36. str = loraSerial.readStringUntil('\n');
  37. Serial.println(str);
  38.  
  39. LoRa("sys get ver");
  40. LoRa("mac pause");
  41. LoRa("radio set freq 902000000");
  42. LoRa("radio set mod lora");
  43. LoRa("radio set pwr 14");
  44. LoRa("radio set sf sf7");
  45. LoRa("radio set afcbw 41.7");
  46. LoRa("radio set rxbw 125");
  47. LoRa("radio set prlen 8");
  48. LoRa("radio set crc on");
  49. LoRa("radio set iqi off");
  50. LoRa("radio set cr 4/5");
  51. LoRa("radio set sync 12");
  52. LoRa("radio set bw 125");
  53.  
  54. // Setup del sensor //
  55. bmp180.begin();
  56.  
  57. Serial.println("Comienza el ciclo");
  58.  
  59. }
  60.  
  61. void loop() {
  62.  
  63. /////////////////// Recepcion ///////////////////////////////
  64. while (bandera != str2){
  65.  
  66. bandera= "";
  67. loraSerial.println("radio rx 0");
  68. str = loraSerial.readStringUntil('\n');
  69. //Serial.println(str);
  70.  
  71. if ( str.indexOf("ok") == 0 )
  72. {
  73. str = String("");
  74. while (str == "")
  75. {
  76. str = loraSerial.readStringUntil('\n');
  77. }
  78. if ( str.indexOf("radio_rx") == 0 )
  79. {
  80. toggle_led();
  81. //Serial.println(str);
  82. }
  83. else
  84. {
  85. Serial.println("Received nothing");
  86. }
  87. }
  88. else
  89. {
  90. Serial.println("radio not going into receive mode");
  91. delay(100);
  92. }
  93.  
  94. int var1 = 3;
  95. while (var1 > 1) {
  96. bandera= bandera+ str[str.length() - var1] ;
  97. var1--;
  98. }
  99. }
  100.  
  101. if (inicializacion == 0){
  102. loraSerial.println("sys get hweui");
  103. str = loraSerial.readStringUntil('\n');
  104. int var2 = 0;
  105. while (var2 < str.length()-1) {
  106. eui = eui + str[var2] ;
  107. var2++;
  108. }
  109. str1 = eui;
  110. //Serial.println(str1);
  111. }
  112. else {
  113. //////////// Datos //////////////
  114. char status;
  115. double T,P;
  116. status = bmp180.startTemperature(); //Inicio de lectura de temperatura
  117. if (status != 0){
  118. delay(status); //Pausa para que finalice la lectura
  119. status = bmp180.getTemperature(T); //Obtener la temperatura
  120. if (status != 0){
  121. status = bmp180.startPressure(3); //Inicio lectura de presión
  122. if (status != 0){
  123. delay(status); //Pausa para que finalice la lectura
  124. status = bmp180.getPressure(P,T); //Obtenemos la presión
  125. if (status != 0){
  126. P = P + 275;
  127. int var2 = 0;
  128. while (var2 < 7) {
  129. if (var2 == 4){ // A4 (SDA) and A5 (SCL)
  130. var2++;
  131. }
  132. else {
  133. str1 = str1 + String(P)[var2] ;
  134. var2++;
  135. }
  136. }
  137. Serial.println(str1);
  138. }}}}}
  139.  
  140. inicializacion = 1;
  141.  
  142. ///////// Transmision ////////////
  143.  
  144. if (bandera == str2){
  145. delay(100);
  146. loraSerial.print("radio tx ");
  147. loraSerial.print(str1);
  148. loraSerial.print(bandera);
  149. loraSerial.println(decimales);
  150. str = loraSerial.readStringUntil('\n');
  151. delay(100);
  152. str = loraSerial.readStringUntil('\n');
  153.  
  154. str1 = String(str1.toInt() + 1);
  155. }
  156.  
  157. eui = "";
  158. bandera= "";
  159. str1 = "";
  160.  
  161. }
  162.  
  163. void lora_autobaud()
  164. {
  165. String response = "";
  166. while (response == "")
  167. {
  168. delay(1000);
  169. loraSerial.write((byte)0x00);
  170. loraSerial.write(0x55);
  171. loraSerial.println();
  172. loraSerial.println("sys get ver");
  173. response = loraSerial.readStringUntil('\n');
  174. }
  175. }
  176. void LoRa(String ini)
  177. {
  178. loraSerial.println(ini);
  179. str = loraSerial.readStringUntil('\n');
  180. }
  181. void toggle_led()
  182. {
  183. digitalWrite(13, !digitalRead(13));
  184. }
Add Comment
Please, Sign In to add comment