Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PTB Zentrale Station
- #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
- #include <SPI.h>
- #define TFT_GREY 0x5AEB // New colour
- TFT_eSPI tft = TFT_eSPI(); // Invoke library
- // ------------------------------------------------
- // SD Card Includes
- // ------------------------------------------------
- #include <SD.h>
- // ------------------------------------------------
- // GPS Includes
- // ------------------------------------------------
- //#include <SoftwareSerial.h>
- #include "HardwareSerial.h"
- #include <TinyGPS++.h>
- // ------------------------------------------------
- // ESP Now Includes
- // ------------------------------------------------
- #include <esp_now.h>
- #include <WiFi.h>
- //-------------------------------------------------
- // SD Globals
- //-------------------------------------------------
- File myFile;
- File root;
- //-------------------------------------------------
- // GPS Globals
- //-------------------------------------------------
- static const int RX2Pin = 17, TX2Pin = 16;
- TinyGPSPlus gps;
- //SoftwareSerial gpsSerial(RX2Pin, TX2Pin);
- HardwareSerial gpsSerial(2);
- //-------------------------------------------------
- // ESP Now Globals
- //-------------------------------------------------
- const char* ssid = "PTB_Station";
- const char* password = "ptb_station_2020";
- #define CHANNEL 1
- // keep in sync with sensor struct
- struct SENSOR_DATA {
- float temp;
- float hum;
- float t;
- float pressure;
- };
- //---------------------------------------------------
- // SD Funktions
- //---------------------------------------------------
- void initSDCard()
- {
- if (!SD.begin(5)) {
- Serial.println("Card Mount Failed");
- return;
- }
- uint8_t cardType = SD.cardType();
- if (cardType == CARD_NONE) {
- Serial.println("No SD card attached");
- return;
- }
- if (!SD.exists("/tid.txt")) {
- myFile = SD.open("/tid.txt", FILE_WRITE);
- myFile.close();
- }
- char tid[15] = {0};
- // re-open the file for reading:
- myFile = SD.open("/tid.txt");
- if (myFile) {
- Serial.println("tid.txt:");
- // read from the file until there's nothing else in it:
- int counter = 0;
- while (myFile.available()) {
- char ltr = myFile.read();
- if (ltr != '\n') {
- tid[counter] = ltr;
- }
- counter ++;
- }
- Serial.println(tid);
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("tid.txt konnte nicht geöffnet werden!");
- }
- root = SD.open("/");
- printDirectory(root, 0);
- }
- void printDirectory(File dir, int numTabs) {
- while (true) {
- File entry = dir.openNextFile();
- if (! entry) {
- // no more files
- break;
- }
- for (uint8_t i = 0; i < numTabs; i++) {
- Serial.print('\t');
- }
- Serial.print(entry.name());
- if (entry.isDirectory()) {
- Serial.println("/");
- printDirectory(entry, numTabs + 1);
- } else {
- // files have sizes, directories do not
- Serial.print("\t\t");
- Serial.println(entry.size(), DEC);
- }
- entry.close();
- }
- }
- //---------------------------------------------------
- // GPS Functions
- //---------------------------------------------------
- char* stringToCharArray(String str){
- // Length (with one extra character for the null terminator)
- int str_len = str.length() + 1;
- // Prepare the character array (the buffer)
- char char_array[str_len];
- // Copy it over
- str.toCharArray(char_array, str_len);
- return char_array;
- }
- void checkGPS() {
- while (gpsSerial.available() > 0)
- if (gps.encode(gpsSerial.read())){
- //showGPSData();
- serialGPSData();
- }
- if (millis() > 5000 && gps.charsProcessed() < 10)
- {
- Serial.println(F("No GPS detected: check wiring."));
- while(true);
- }
- }
- void showGPSData()
- {
- if (gps.location.isValid()){
- //GPS Coords
- String serialData = "";
- serialData = String(gps.location.lat())+","+String(gps.location.lng());
- } else {
- }
- //Date / Time
- String serialData2 = "";
- if (gps.date.isValid())
- {
- serialData2 = String(gps.date.day())+"."+String(gps.date.month())+"."+String(gps.date.year())+" / ";
- }
- else
- {
- serialData2 = String(millis())+" / ";
- }
- if (gps.time.isValid())
- {
- if (gps.time.hour() < 10) serialData2 += "0";
- serialData2 += String(gps.time.hour());
- serialData2 += ":";
- if (gps.time.minute() < 10) serialData2 += "0";
- serialData2 += String(gps.time.minute());
- serialData2 += ":";
- if (gps.time.second() < 10) serialData2 += "0";
- serialData2 += String(gps.time.second());
- } else {
- serialData2 += String(millis());
- }
- //Serial.println(serialData2);
- char __serialdata2[31];
- serialData2.toCharArray(__serialdata2, sizeof(__serialdata2));
- }
- void serialGPSData()
- {
- Serial.print(F("Location: "));
- if (gps.location.isValid())
- {
- Serial.print(gps.location.lat(), 6);
- Serial.print(F(","));
- Serial.print(gps.location.lng(), 6);
- }
- else
- {
- Serial.print(F("INVALID"));
- }
- Serial.print(F(" Date/Time: "));
- if (gps.date.isValid())
- {
- Serial.print(gps.date.month());
- Serial.print(F("/"));
- Serial.print(gps.date.day());
- Serial.print(F("/"));
- Serial.print(gps.date.year());
- }
- else
- {
- Serial.print(F("INVALID"));
- }
- Serial.print(F(" "));
- if (gps.time.isValid())
- {
- if (gps.time.hour() < 10) Serial.print(F("0"));
- Serial.print(gps.time.hour());
- Serial.print(F(":"));
- if (gps.time.minute() < 10) Serial.print(F("0"));
- Serial.print(gps.time.minute());
- Serial.print(F(":"));
- if (gps.time.second() < 10) Serial.print(F("0"));
- Serial.print(gps.time.second());
- Serial.print(F("."));
- if (gps.time.centisecond() < 10) Serial.print(F("0"));
- Serial.print(gps.time.centisecond());
- }
- else
- {
- Serial.print(F("INVALID"));
- }
- Serial.println();
- }
- //---------------------------------------------------
- // ESP Now Functions
- //---------------------------------------------------
- // Init ESP Now with fallback
- void InitESPNow() {
- WiFi.disconnect();
- if (esp_now_init() == ESP_OK) {
- Serial.println("ESPNow Init Success");
- //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk initialisiert\n");
- }
- else {
- Serial.println("ESPNow Failed");
- //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"Netzwerk konnte nicht initalisiert werden!\n");
- // Retry InitESPNow, add a counte and then restart?
- // InitESPNow();
- // or Simply Restart
- ESP.restart();
- }
- }
- void configDeviceAP() {
- String Prefix = "Slave:";
- String Mac = WiFi.macAddress();
- String SSID = Prefix + Mac;
- String Password = "123456789";
- bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0);
- if (!result) {
- Serial.println("AP Config failed.");
- //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration fehlgeschlagen\n");
- } else {
- Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
- //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,(char*)"AP Konfiguration erfolgreich\n");
- char __ca[sizeof(SSID)];
- SSID.toCharArray(__ca, sizeof(__ca));
- //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,__ca);
- }
- }
- // callback when data is recv from Master
- void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
- char macStr[18];
- snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
- mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
- Serial.print("Last Packet Recv from: "); Serial.println(macStr);
- Serial.print("Last Packet Recv Data: "); Serial.println(*data);
- Serial.println("");
- }
- //---------------------------------------------------
- // SETUP
- void setup()
- {
- Serial.begin(9600);
- gpsSerial.begin(9600, SERIAL_8N1, RX2Pin, TX2Pin);
- //LED Background
- pinMode(25, OUTPUT);
- digitalWrite(25, HIGH);
- //TFT
- tft.init();
- tft.setRotation(3);
- //WiFi
- Serial.println("Wifi Config");
- /*WiFi.mode(WIFI_AP);
- configDeviceAP();
- String MacAd = WiFi.softAPmacAddress();
- char __ca[sizeof(MacAd)];
- MacAd.toCharArray(__ca, sizeof(__ca));
- Serial.println ("MacAd "+MacAd);
- // Once ESPNow is successfully Init, we will register for recv CB to
- // get recv packer info.
- esp_now_register_recv_cb(OnDataRecv);*/
- // Wait for USB Serial
- //delay(1000); // NOTE: Some devices require a delay after Serial.begin() before serial port can be used
- initSDCard();
- }
- // -----------------------------------
- // Main event loop
- // -----------------------------------
- void loop()
- {
- checkGPS();
- // Fill screen with grey so we can see the effect of printing with and without
- // a background colour defined
- tft.fillScreen(TFT_GREY);
- // Set "cursor" at top left corner of display (0,0) and select font 2
- // (cursor will move to next line automatically during printing with 'tft.println'
- // or stay on the line is there is room for the text with tft.print)
- tft.setCursor(0, 0, 2);
- // Set the font colour to be white with a black background, set text size multiplier to 1
- tft.setTextColor(TFT_WHITE,TFT_BLACK); tft.setTextSize(1);
- // We can now plot text on screen using the "print" class
- tft.println("Hello World!");
- // Set the font colour to be yellow with no background, set to font 7
- tft.setTextColor(TFT_YELLOW); tft.setTextFont(7);
- tft.println(1234.56);
- // Set the font colour to be red with black background, set to font 4
- tft.setTextColor(TFT_RED,TFT_BLACK); tft.setTextFont(4);
- //tft.println(3735928559L, HEX); // Should print DEADBEEF
- // Set the font colour to be green with black background, set to font 4
- tft.setTextColor(TFT_GREEN,TFT_BLACK);
- tft.setTextFont(4);
- tft.println("Groop");
- tft.println("I implore thee,");
- // Change to font 2
- tft.setTextFont(2);
- tft.println("my foonting turlingdromes.");
- tft.println("And hooptiously drangle me");
- tft.println("with crinkly bindlewurdles,");
- // This next line is deliberately made too long for the display width to test
- // automatic text wrapping onto the next line
- tft.println("Or I will rend thee in the gobberwarts with my blurglecruncheon, see if I don't!");
- // Test some print formatting functions
- float fnumber = 123.45;
- // Set the font colour to be blue with no background, set to font 4
- tft.setTextColor(TFT_BLUE); tft.setTextFont(4);
- tft.print("Float = "); tft.println(fnumber); // Print floating point number
- tft.print("Binary = "); tft.println((int)fnumber, BIN); // Print as integer value in binary
- tft.print("Hexadecimal = "); tft.println((int)fnumber, HEX); // Print as integer number in Hexadecimal
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement