main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "logger.h"
  2. #include <Arduino.h>
  3. #include <WiFi.h>
  4. #include <TinyGPS++.h>
  5. #include <XPowersLib.h>
  6. #include "display.h"
  7. #include "pins.h"
  8. #include "RadioLib.h"
  9. #define RADIO_SCLK_PIN 5
  10. #define RADIO_MISO_PIN 19
  11. #define RADIO_MOSI_PIN 27
  12. #define RADIO_CS_PIN 18
  13. #define RADIO_DIO0_PIN 26
  14. #define RADIO_RST_PIN 23
  15. #define RADIO_DIO1_PIN 33
  16. #define RADIO_BUSY_PIN 32
  17. SX1278 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
  18. logging::Logger logger;
  19. TinyGPSPlus gps;
  20. char nmea[512];
  21. HardwareSerial ss(1);
  22. char trameg[128] = "#G";
  23. XPowersAXP2101 power;
  24. #define XPOWERS_CHIP_AXP2101
  25. #define PMU_IRQ 35
  26. #define PMU_SDA 21
  27. #define PMU_SCK 22
  28. void setup() {
  29. Serial.begin(115200);
  30. /*
  31. logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "MAIN", "This is a error");
  32. logger.log(logging::LoggerLevel::LOGGER_LEVEL_WARN, "MAIN", "This is a warning");
  33. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "MAIN", "This is a info");
  34. logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "MAIN", "This is a debug");
  35. */
  36. setup_display();
  37. show_display("PIOTest", "GPS and other stuffs", 2000);
  38. /* Scan for WiFi networks
  39. * On commence par scanner les réseaux WiFi disponibles
  40. * On attend que le scan soit terminé et on affiche les réseaux trouvés
  41. show_display("WiFi", "Scanning around", "Please wait...", 2000);
  42. WiFi.scanNetworks();
  43. int n = WiFi.scanComplete();
  44. if (n == -2) {
  45. WiFi.scanNetworks();
  46. }
  47. while (n == -1) {
  48. delay(100);
  49. n = WiFi.scanComplete();
  50. }
  51. if (n == 0) {
  52. logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "WIFI", "No networks found");
  53. show_display("WiFi", "No networks found", 2000);
  54. } else {
  55. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "WIFI", "Found %d networks", n);
  56. show_display("WiFi", "Found networks", "Please wait...", 2000);
  57. for (int i = 0; i < n; ++i) {
  58. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "WIFI", "Network: %s", WiFi.SSID(i).c_str());
  59. show_display("WiFi", WiFi.SSID(i).c_str(), 2000);
  60. }
  61. }
  62. */
  63. /*
  64. * Initialize the PMU
  65. */
  66. bool result = power.begin(Wire, AXP2101_SLAVE_ADDRESS, PMU_SDA, PMU_SCK);
  67. if (result == false) {
  68. logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "PMU", "Failed to initialize PMU");
  69. }
  70. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "PMU", "PMU initialized");
  71. /* Initialize the GPS
  72. */
  73. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "Initializing GPS");
  74. show_display("GPS", "Initializing GPS", "Please wait...", 2000);
  75. ss.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
  76. // setup GPS for only GPRMC sentences, but don't work anyway...
  77. ss.println("PMTK314,0,0,0,1,0,0,0,0,0,0,0,0,0,0");
  78. /* Initialize the LoRa radio
  79. */
  80. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "RADIO", "Initializing LoRa radio");
  81. radio.beginFSK(435);
  82. }
  83. void loop() {
  84. int size = 0;
  85. char * pch;
  86. while (ss.available() > 0) {
  87. char c = ss.read();
  88. gps.encode(c);
  89. // add char to trame
  90. nmea[size] = c;
  91. //
  92. size++;
  93. }
  94. nmea[size] = '\0';
  95. if (gps.location.isValid()) {
  96. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "Full NMEA:");
  97. pch = strtok (nmea,"\n");
  98. while (pch != NULL) {
  99. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\t%s", pch);
  100. pch = strtok (NULL, "\n");
  101. }
  102. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "Parsing NMEA:");
  103. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tLatitude: %f", gps.location.lat());
  104. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tLongitude: %f", gps.location.lng());
  105. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tNumber of satellites : %d", gps.satellites.value());
  106. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tAltitude: %f", gps.altitude.meters());
  107. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tSpeed: %f", gps.speed.kmph());
  108. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tCourse: %f", gps.course.deg());
  109. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tDate: %d/%d/%d", gps.date.day(), gps.date.month(), gps.date.year());
  110. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tTime: %d:%d:%d", gps.time.hour(), gps.time.minute(), gps.time.second());
  111. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "\tHDOP: %f", gps.hdop.hdop());
  112. }
  113. else {
  114. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "No GPS data available");
  115. }
  116. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "GPS", "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
  117. // erase trameg with \0
  118. memset(trameg, 0, sizeof(trameg));
  119. strcat(trameg, "#G");
  120. char strday[4];
  121. char strmonth[4];
  122. char stryear[6];
  123. char stryear2[4];
  124. char strhour[4];
  125. char strminute[4];
  126. char strsecond[4];
  127. sprintf(strday, "%u", gps.date.day());
  128. sprintf(strmonth, "%u", gps.date.month());
  129. sprintf(stryear, "%u", gps.date.year());
  130. sprintf(strhour, "%u", gps.time.hour());
  131. sprintf(strminute, "%u", gps.time.minute());
  132. sprintf(strsecond, "%u", gps.time.second());
  133. // Trame = "#GDDMMYY;HHMMSS;LATITUDE;LONGITUDE;ALTITUDE;SATELLITES;*RSSI%\r\n"
  134. strcat(trameg, strday);
  135. strcat(trameg, strmonth);
  136. strcat(trameg, stryear);
  137. // stryear2[0] = stryear[2];
  138. // stryear2[1] = stryear[3];
  139. strcat(trameg, stryear2);
  140. strcat(trameg, ";");
  141. strcat(trameg, strhour);
  142. strcat(trameg, strminute);
  143. strcat(trameg, strsecond);
  144. strcat(trameg, ";");
  145. char strlat[16];
  146. char strlng[16];
  147. dtostrf(gps.location.lat(), 8, 6, strlat);
  148. dtostrf(gps.location.lng(), 8, 6, strlng);
  149. strcat(trameg, strlat);
  150. strcat(trameg, ";");
  151. strcat(trameg, strlng);
  152. strcat(trameg, ";");
  153. char stralt[16];
  154. dtostrf(gps.altitude.meters(), 8, 2, stralt);
  155. strcat(trameg, stralt);
  156. strcat(trameg, ";");
  157. char strsat[4];
  158. sprintf(strsat, "%u", gps.satellites.value());
  159. strcat(trameg, strsat);
  160. strcat(trameg, "*");
  161. strcat(trameg, "RSSSSSSSSI");
  162. strcat(trameg, "%");
  163. strcat(trameg, "\r\n");
  164. strcat(trameg, "\0");
  165. // count bytes until \0
  166. int sizestr = 0;
  167. while (trameg[sizestr] != '\0') {
  168. sizestr++;
  169. }
  170. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "TRAME_BUILD", "Trame: %s", trameg);
  171. int state = radio.transmit(trameg, sizestr);
  172. //int state = radio.transmit(nmea, strlen(nmea));
  173. if (state == RADIOLIB_ERR_NONE) {
  174. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "RADIO", "Trame sent. Size: %d bytes", strlen(trameg));
  175. // print trame
  176. logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "RADIO", "Trame: %s", trameg);
  177. } else {
  178. logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "RADIO", "Failed to send trame");
  179. }
  180. delay(1000);
  181. }