Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.00 KB | None | 0 0
  1. // PTB Zentrale Station
  2. #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
  3. #include <SPI.h>
  4.  
  5. #define TFT_GREY 0x5AEB // New colour
  6.  
  7. TFT_eSPI tft = TFT_eSPI(); // Invoke library
  8.  
  9. // ------------------------------------------------
  10. // SD Card Includes
  11. // ------------------------------------------------
  12. #include <SD.h>
  13.  
  14. // ------------------------------------------------
  15. // GPS Includes
  16. // ------------------------------------------------
  17. //#include <SoftwareSerial.h>
  18. #include "HardwareSerial.h"
  19. #include <TinyGPS++.h>
  20.  
  21. // ------------------------------------------------
  22. // ESP Now Includes
  23. // ------------------------------------------------
  24. #include <esp_now.h>
  25. #include <WiFi.h>
  26.  
  27. //-------------------------------------------------
  28. // SD Globals
  29. //-------------------------------------------------
  30. File myFile;
  31. File root;
  32.  
  33. //-------------------------------------------------
  34. // GPS Globals
  35. //-------------------------------------------------
  36. static const int RX2Pin = 17, TX2Pin = 16;
  37. TinyGPSPlus gps;
  38. //SoftwareSerial gpsSerial(RX2Pin, TX2Pin);
  39. HardwareSerial gpsSerial(2);
  40.  
  41. //-------------------------------------------------
  42. // ESP Now Globals
  43. //-------------------------------------------------
  44. const char* ssid = "PTB_Station";
  45. const char* password = "ptb_station_2020";
  46.  
  47. #define CHANNEL 1
  48.  
  49. // keep in sync with sensor struct
  50. struct SENSOR_DATA {
  51. float temp;
  52. float hum;
  53. float t;
  54. float pressure;
  55. };
  56.  
  57. //---------------------------------------------------
  58. // SD Funktions
  59. //---------------------------------------------------
  60. void initSDCard()
  61. {
  62. if (!SD.begin(5)) {
  63. Serial.println("Card Mount Failed");
  64. return;
  65. }
  66. uint8_t cardType = SD.cardType();
  67.  
  68. if (cardType == CARD_NONE) {
  69. Serial.println("No SD card attached");
  70. return;
  71. }
  72. if (!SD.exists("/tid.txt")) {
  73. myFile = SD.open("/tid.txt", FILE_WRITE);
  74. myFile.close();
  75. }
  76.  
  77. char tid[15] = {0};
  78. // re-open the file for reading:
  79. myFile = SD.open("/tid.txt");
  80. if (myFile) {
  81. Serial.println("tid.txt:");
  82. // read from the file until there's nothing else in it:
  83. int counter = 0;
  84. while (myFile.available()) {
  85. char ltr = myFile.read();
  86. if (ltr != '\n') {
  87. tid[counter] = ltr;
  88. }
  89. counter ++;
  90. }
  91. Serial.println(tid);
  92. // close the file:
  93. myFile.close();
  94. } else {
  95. // if the file didn't open, print an error:
  96. Serial.println("tid.txt konnte nicht geöffnet werden!");
  97. }
  98.  
  99. root = SD.open("/");
  100. printDirectory(root, 0);
  101. }
  102.  
  103. void printDirectory(File dir, int numTabs) {
  104. while (true) {
  105.  
  106. File entry = dir.openNextFile();
  107. if (! entry) {
  108. // no more files
  109. break;
  110. }
  111. for (uint8_t i = 0; i < numTabs; i++) {
  112. Serial.print('\t');
  113. }
  114. Serial.print(entry.name());
  115. if (entry.isDirectory()) {
  116. Serial.println("/");
  117. printDirectory(entry, numTabs + 1);
  118. } else {
  119. // files have sizes, directories do not
  120. Serial.print("\t\t");
  121. Serial.println(entry.size(), DEC);
  122. }
  123. entry.close();
  124. }
  125. }
  126.  
  127. //---------------------------------------------------
  128. // GPS Functions
  129. //---------------------------------------------------
  130. char* stringToCharArray(String str){
  131. // Length (with one extra character for the null terminator)
  132. int str_len = str.length() + 1;
  133.  
  134. // Prepare the character array (the buffer)
  135. char char_array[str_len];
  136.  
  137. // Copy it over
  138. str.toCharArray(char_array, str_len);
  139.  
  140. return char_array;
  141. }
  142.  
  143. void checkGPS() {
  144. while (gpsSerial.available() > 0)
  145. if (gps.encode(gpsSerial.read())){
  146. //showGPSData();
  147. serialGPSData();
  148. }
  149.  
  150. if (millis() > 5000 && gps.charsProcessed() < 10)
  151. {
  152. Serial.println(F("No GPS detected: check wiring."));
  153. while(true);
  154. }
  155. }
  156.  
  157. void showGPSData()
  158. {
  159. if (gps.location.isValid()){
  160. //GPS Coords
  161. String serialData = "";
  162. serialData = String(gps.location.lat())+","+String(gps.location.lng());
  163. } else {
  164.  
  165. }
  166.  
  167. //Date / Time
  168. String serialData2 = "";
  169. if (gps.date.isValid())
  170. {
  171. serialData2 = String(gps.date.day())+"."+String(gps.date.month())+"."+String(gps.date.year())+" / ";
  172. }
  173. else
  174. {
  175. serialData2 = String(millis())+" / ";
  176. }
  177.  
  178. if (gps.time.isValid())
  179. {
  180. if (gps.time.hour() < 10) serialData2 += "0";
  181. serialData2 += String(gps.time.hour());
  182. serialData2 += ":";
  183. if (gps.time.minute() < 10) serialData2 += "0";
  184. serialData2 += String(gps.time.minute());
  185. serialData2 += ":";
  186. if (gps.time.second() < 10) serialData2 += "0";
  187. serialData2 += String(gps.time.second());
  188. } else {
  189. serialData2 += String(millis());
  190. }
  191. //Serial.println(serialData2);
  192. char __serialdata2[31];
  193. serialData2.toCharArray(__serialdata2, sizeof(__serialdata2));
  194. }
  195.  
  196. void serialGPSData()
  197. {
  198. Serial.print(F("Location: "));
  199. if (gps.location.isValid())
  200. {
  201. Serial.print(gps.location.lat(), 6);
  202. Serial.print(F(","));
  203. Serial.print(gps.location.lng(), 6);
  204. }
  205. else
  206. {
  207. Serial.print(F("INVALID"));
  208. }
  209.  
  210. Serial.print(F(" Date/Time: "));
  211. if (gps.date.isValid())
  212. {
  213. Serial.print(gps.date.month());
  214. Serial.print(F("/"));
  215. Serial.print(gps.date.day());
  216. Serial.print(F("/"));
  217. Serial.print(gps.date.year());
  218. }
  219. else
  220. {
  221. Serial.print(F("INVALID"));
  222. }
  223.  
  224. Serial.print(F(" "));
  225. if (gps.time.isValid())
  226. {
  227. if (gps.time.hour() < 10) Serial.print(F("0"));
  228. Serial.print(gps.time.hour());
  229. Serial.print(F(":"));
  230. if (gps.time.minute() < 10) Serial.print(F("0"));
  231. Serial.print(gps.time.minute());
  232. Serial.print(F(":"));
  233. if (gps.time.second() < 10) Serial.print(F("0"));
  234. Serial.print(gps.time.second());
  235. Serial.print(F("."));
  236. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  237. Serial.print(gps.time.centisecond());
  238. }
  239. else
  240. {
  241. Serial.print(F("INVALID"));
  242. }
  243.  
  244. Serial.println();
  245. }
  246.  
  247. //---------------------------------------------------
  248. // ESP Now Functions
  249. //---------------------------------------------------
  250. // Init ESP Now with fallback
  251. void InitESPNow() {
  252. WiFi.disconnect();
  253. if (esp_now_init() == ESP_OK) {
  254. Serial.println("ESPNow Init Success");
  255. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk initialisiert\n");
  256. }
  257. else {
  258. Serial.println("ESPNow Failed");
  259. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk konnte nicht initalisiert werden!\n");
  260. // Retry InitESPNow, add a counte and then restart?
  261. // InitESPNow();
  262. // or Simply Restart
  263. ESP.restart();
  264. }
  265. }
  266.  
  267. void configDeviceAP() {
  268. String Prefix = "Slave:";
  269. String Mac = WiFi.macAddress();
  270. String SSID = Prefix + Mac;
  271. String Password = "123456789";
  272. bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
  273. if (!result) {
  274. Serial.println("AP Config failed.");
  275. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration fehlgeschlagen\n");
  276. } else {
  277. Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
  278. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration erfolgreich\n");
  279. char __ca[sizeof(SSID)];
  280. SSID.toCharArray(__ca, sizeof(__ca));
  281. //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,__ca);
  282. }
  283. }
  284.  
  285. // callback when data is recv from Master
  286. void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  287. char macStr[18];
  288. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  289. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  290. Serial.print("Last Packet Recv from: "); Serial.println(macStr);
  291. Serial.print("Last Packet Recv Data: "); Serial.println(*data);
  292. Serial.println("");
  293. }
  294.  
  295.  
  296. //---------------------------------------------------
  297. // SETUP
  298.  
  299. void setup()
  300. {
  301. Serial.begin(9600);
  302. gpsSerial.begin(9600, SERIAL_8N1, RX2Pin, TX2Pin);
  303. //LED Background
  304. pinMode(25, OUTPUT);
  305. digitalWrite(25, HIGH);
  306. //TFT
  307. tft.init();
  308. tft.setRotation(3);
  309. //WiFi
  310. Serial.println("Wifi Config");
  311. /*WiFi.mode(WIFI_AP);
  312. configDeviceAP();
  313. String MacAd = WiFi.softAPmacAddress();
  314. char __ca[sizeof(MacAd)];
  315. MacAd.toCharArray(__ca, sizeof(__ca));
  316. Serial.println ("MacAd "+MacAd);
  317. // Once ESPNow is successfully Init, we will register for recv CB to
  318. // get recv packer info.
  319. esp_now_register_recv_cb(OnDataRecv);*/
  320. // Wait for USB Serial
  321. //delay(1000); // NOTE: Some devices require a delay after Serial.begin() before serial port can be used
  322.  
  323. initSDCard();
  324.  
  325. }
  326.  
  327. // -----------------------------------
  328. // Main event loop
  329. // -----------------------------------
  330. void loop()
  331. {
  332. checkGPS();
  333.  
  334. // Fill screen with grey so we can see the effect of printing with and without
  335. // a background colour defined
  336. tft.fillScreen(TFT_GREY);
  337.  
  338. // Set "cursor" at top left corner of display (0,0) and select font 2
  339. // (cursor will move to next line automatically during printing with 'tft.println'
  340. // or stay on the line is there is room for the text with tft.print)
  341. tft.setCursor(0, 0, 2);
  342. // Set the font colour to be white with a black background, set text size multiplier to 1
  343. tft.setTextColor(TFT_WHITE,TFT_BLACK); tft.setTextSize(1);
  344. // We can now plot text on screen using the "print" class
  345. tft.println("Hello World!");
  346.  
  347. // Set the font colour to be yellow with no background, set to font 7
  348. tft.setTextColor(TFT_YELLOW); tft.setTextFont(7);
  349. tft.println(1234.56);
  350.  
  351. // Set the font colour to be red with black background, set to font 4
  352. tft.setTextColor(TFT_RED,TFT_BLACK); tft.setTextFont(4);
  353. //tft.println(3735928559L, HEX); // Should print DEADBEEF
  354.  
  355. // Set the font colour to be green with black background, set to font 4
  356. tft.setTextColor(TFT_GREEN,TFT_BLACK);
  357. tft.setTextFont(4);
  358. tft.println("Groop");
  359. tft.println("I implore thee,");
  360.  
  361. // Change to font 2
  362. tft.setTextFont(2);
  363. tft.println("my foonting turlingdromes.");
  364. tft.println("And hooptiously drangle me");
  365. tft.println("with crinkly bindlewurdles,");
  366. // This next line is deliberately made too long for the display width to test
  367. // automatic text wrapping onto the next line
  368. tft.println("Or I will rend thee in the gobberwarts with my blurglecruncheon, see if I don't!");
  369.  
  370. // Test some print formatting functions
  371. float fnumber = 123.45;
  372. // Set the font colour to be blue with no background, set to font 4
  373. tft.setTextColor(TFT_BLUE); tft.setTextFont(4);
  374. tft.print("Float = "); tft.println(fnumber); // Print floating point number
  375. tft.print("Binary = "); tft.println((int)fnumber, BIN); // Print as integer value in binary
  376. tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal
  377. delay(10000);
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement