mirror of
https://github.com/0015/ThatProject.git
synced 2026-01-12 09:17:42 +03:00
NodeMCU(ESP8266) | GPS | ILI9488 - Showing Google Map with Device Position on Display🗺️
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
#define WIFI_SSID "<YOUR_WIFI_SSID_HERE>"
|
||||
#define PASSWORD "<YOUR_WIFI_PW_HERE>"
|
||||
#include <TJpg_Decoder.h>
|
||||
#include <CertStoreBearSSL.h>
|
||||
#include <time.h>
|
||||
|
||||
// Include SPIFFS
|
||||
#define FS_NO_GLOBALS
|
||||
#include <FS.h>
|
||||
|
||||
// Include WiFi and http client
|
||||
#ifdef ESP8266
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
#else
|
||||
#include "SPIFFS.h" // Required for ESP32 only
|
||||
#include <WiFi.h>
|
||||
#include <HTTPClient.h>
|
||||
#endif
|
||||
|
||||
// Load tabs attached to this sketch
|
||||
#include "List_SPIFFS.h"
|
||||
#include "Web_Fetch.h"
|
||||
#include "SPI.h"
|
||||
#include <TFT_eSPI.h> // Hardware-specific library
|
||||
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
|
||||
|
||||
BearSSL::CertStore certStore;
|
||||
|
||||
const char * host = "maps.googleapis.com";
|
||||
const char * path = "/maps/api/staticmap?center=40.714728,-73.998672&zoom=14&size=480x320&format=jpg-baseline&key=<YOUR_API_KEY_HERE>";
|
||||
const char * mapFile = "/map.jpg";
|
||||
|
||||
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
|
||||
{
|
||||
// Stop further decoding as image is running off bottom of screen
|
||||
if ( y >= tft.height() ) return 0;
|
||||
|
||||
// This function will clip the image block rendering automatically at the TFT boundaries
|
||||
tft.pushImage(x, y, w, h, bitmap);
|
||||
|
||||
// Return 1 to decode next block
|
||||
return 1;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("\n\n");
|
||||
|
||||
// Initialise SPIFFS
|
||||
if (!SPIFFS.begin()) {
|
||||
Serial.println("SPIFFS initialisation failed!");
|
||||
while (1) yield(); // Stay here twiddling thumbs waiting
|
||||
}
|
||||
Serial.println("\r\nInitialisation done.");
|
||||
|
||||
|
||||
// Initialise the TFT
|
||||
tft.begin();
|
||||
tft.fillScreen(TFT_BLACK);
|
||||
tft.setRotation(3);
|
||||
|
||||
// The jpeg image can be scaled by a factor of 1, 2, 4, or 8
|
||||
TJpgDec.setJpgScale(1);
|
||||
|
||||
// The byte order can be swapped (set true for TFT_eSPI)
|
||||
TJpgDec.setSwapBytes(true);
|
||||
|
||||
// The decoder must be given the exact name of the rendering function above
|
||||
TJpgDec.setCallback(tft_output);
|
||||
|
||||
WiFi.begin(WIFI_SSID, PASSWORD);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
setClock(); // Required for X.509 validation
|
||||
|
||||
int numCerts = certStore.initCertStore(SPIFFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
|
||||
Serial.printf("Number of CA certs read: %d\n", numCerts);
|
||||
if (numCerts == 0) {
|
||||
Serial.printf("No certs found. Did you run certs-from-mozilla.py and upload the SPIFFS directory before running?\n");
|
||||
return; // Can't connect to anything w/o certs!
|
||||
}
|
||||
|
||||
listSPIFFS();
|
||||
|
||||
if(getStaticMapImage(host, path, mapFile)){
|
||||
listSPIFFS();
|
||||
TJpgDec.drawFsJpg(0, 0, mapFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Set time via NTP, as required for x.509 validation
|
||||
void setClock() {
|
||||
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
|
||||
Serial.print("Waiting for NTP time sync: ");
|
||||
time_t now = time(nullptr);
|
||||
while (now < 8 * 3600 * 2) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
now = time(nullptr);
|
||||
}
|
||||
Serial.println("");
|
||||
struct tm timeinfo;
|
||||
gmtime_r(&now, &timeinfo);
|
||||
Serial.print("Current time: ");
|
||||
Serial.print(asctime(&timeinfo));
|
||||
}
|
||||
|
||||
bool getStaticMapImage(const char *host, const char *path, String fileName){
|
||||
if(WiFi.status() != WL_CONNECTED){
|
||||
return false;
|
||||
}
|
||||
|
||||
int contentLength = 0;
|
||||
int httpCode;
|
||||
|
||||
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
|
||||
// Integrate the cert store with this connection
|
||||
client->setCertStore(&certStore);
|
||||
client->connect(host, 443); //HTTPS
|
||||
|
||||
Serial.printf("Trying: %s:443...", host);
|
||||
|
||||
if(!client->connected()){
|
||||
client->stop();
|
||||
Serial.printf("*** Can't connect. ***\n-------\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.println("HTTPS Connected!");
|
||||
client->write("GET ");
|
||||
client->write(path);
|
||||
client->write(" HTTP/1.0\r\nHost: ");
|
||||
client->write(host);
|
||||
client->write("\r\nUser-Agent: ESP8266\r\n");
|
||||
client->write("\r\n");
|
||||
|
||||
while(client->connected()){
|
||||
String header = client-> readStringUntil('\n');
|
||||
if(header.startsWith(F("HTTP/1."))){
|
||||
httpCode = header.substring(9, 12).toInt();
|
||||
if(httpCode != 200){
|
||||
client->stop();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(header.startsWith(F("Content-Length: "))){
|
||||
contentLength = header.substring(15).toInt();
|
||||
}
|
||||
|
||||
if(header == F("\r")){
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!(contentLength > 0)){
|
||||
client->stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
fs::File f = SPIFFS.open(fileName, "w");
|
||||
if(!f){
|
||||
Serial.println(F("FILE OPEN FAILED"));
|
||||
client->stop();
|
||||
return false;
|
||||
}
|
||||
|
||||
int remaining = contentLength;
|
||||
int received;
|
||||
uint8_t buff[512] = {0};
|
||||
|
||||
while(client->available() && remaining > 0){
|
||||
received = client->readBytes(buff, ((remaining > sizeof(buff)) ? sizeof(buff) : remaining));
|
||||
f.write(buff, received);
|
||||
|
||||
if(remaining > 0){
|
||||
remaining -= received;
|
||||
}
|
||||
yield();
|
||||
}
|
||||
|
||||
f.close();
|
||||
client->stop();
|
||||
Serial.println("DOWNLOAD END");
|
||||
return (remaining == 0 ? true : false);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/***************************************************************************************
|
||||
** Function name: listSPIFFS
|
||||
** Description: Listing SPIFFS files
|
||||
***************************************************************************************/
|
||||
#ifdef ESP8266
|
||||
void listSPIFFS(void) {
|
||||
Serial.println(F("\r\nListing SPIFFS files:"));
|
||||
|
||||
fs::Dir dir = SPIFFS.openDir("/"); // Root directory
|
||||
|
||||
static const char line[] PROGMEM = "=================================================";
|
||||
Serial.println(FPSTR(line));
|
||||
Serial.println(F(" File name Size"));
|
||||
Serial.println(FPSTR(line));
|
||||
|
||||
while (dir.next()) {
|
||||
String fileName = dir.fileName();
|
||||
Serial.print(fileName);
|
||||
int spaces = 33 - fileName.length(); // Tabulate nicely
|
||||
if (spaces < 1) spaces = 1;
|
||||
while (spaces--) Serial.print(" ");
|
||||
|
||||
fs::File f = dir.openFile("r");
|
||||
String fileSize = (String) f.size();
|
||||
spaces = 10 - fileSize.length(); // Tabulate nicely
|
||||
if (spaces < 1) spaces = 1;
|
||||
while (spaces--) Serial.print(" ");
|
||||
Serial.println(fileSize + " bytes");
|
||||
}
|
||||
|
||||
Serial.println(FPSTR(line));
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
//====================================================================================
|
||||
|
||||
#elif defined ESP32
|
||||
|
||||
void listSPIFFS(void) {
|
||||
Serial.println(F("\r\nListing SPIFFS files:"));
|
||||
static const char line[] PROGMEM = "=================================================";
|
||||
|
||||
Serial.println(FPSTR(line));
|
||||
Serial.println(F(" File name Size"));
|
||||
Serial.println(FPSTR(line));
|
||||
|
||||
fs::File root = SPIFFS.open("/");
|
||||
if (!root) {
|
||||
Serial.println(F("Failed to open directory"));
|
||||
return;
|
||||
}
|
||||
if (!root.isDirectory()) {
|
||||
Serial.println(F("Not a directory"));
|
||||
return;
|
||||
}
|
||||
|
||||
fs::File file = root.openNextFile();
|
||||
while (file) {
|
||||
|
||||
if (file.isDirectory()) {
|
||||
Serial.print("DIR : ");
|
||||
String fileName = file.name();
|
||||
Serial.print(fileName);
|
||||
} else {
|
||||
String fileName = file.name();
|
||||
Serial.print(" " + fileName);
|
||||
// File path can be 31 characters maximum in SPIFFS
|
||||
int spaces = 33 - fileName.length(); // Tabulate nicely
|
||||
if (spaces < 1) spaces = 1;
|
||||
while (spaces--) Serial.print(" ");
|
||||
String fileSize = (String) file.size();
|
||||
spaces = 10 - fileSize.length(); // Tabulate nicely
|
||||
if (spaces < 1) spaces = 1;
|
||||
while (spaces--) Serial.print(" ");
|
||||
Serial.println(fileSize + " bytes");
|
||||
}
|
||||
|
||||
file = root.openNextFile();
|
||||
}
|
||||
|
||||
Serial.println(FPSTR(line));
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
// Fetch a file from the URL given and save it in SPIFFS
|
||||
// Return 1 if a web fetch was needed or 0 if file already exists
|
||||
bool getFile(String url, String filename) {
|
||||
|
||||
// If it exists then no need to fetch it
|
||||
if (SPIFFS.exists(filename) == true) {
|
||||
Serial.println("Found " + filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Serial.println("Downloading " + filename + " from " + url);
|
||||
|
||||
// Check WiFi connection
|
||||
if ((WiFi.status() == WL_CONNECTED)) {
|
||||
HTTPClient http;
|
||||
|
||||
Serial.print("[HTTP] begin...\n");
|
||||
|
||||
// Configure server and url
|
||||
http.begin(url);
|
||||
|
||||
Serial.print("[HTTP] GET...\n");
|
||||
// Start connection and send HTTP header
|
||||
int httpCode = http.GET();
|
||||
if (httpCode > 0) {
|
||||
fs::File f = SPIFFS.open(filename, "w+");
|
||||
if (!f) {
|
||||
Serial.println("file open failed");
|
||||
return 0;
|
||||
}
|
||||
// HTTP header has been send and Server response header has been handled
|
||||
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
|
||||
|
||||
// File found at server
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
|
||||
// Get length of document (is -1 when Server sends no Content-Length header)
|
||||
int total = http.getSize();
|
||||
int len = total;
|
||||
|
||||
// Create buffer for read
|
||||
uint8_t buff[128] = { 0 };
|
||||
|
||||
// Get tcp stream
|
||||
WiFiClient * stream = http.getStreamPtr();
|
||||
|
||||
// Read all data from server
|
||||
while (http.connected() && (len > 0 || len == -1)) {
|
||||
// Get available data size
|
||||
size_t size = stream->available();
|
||||
|
||||
if (size) {
|
||||
// Read up to 128 bytes
|
||||
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
|
||||
|
||||
// Write it to file
|
||||
f.write(buff, c);
|
||||
|
||||
// Calculate remaining bytes
|
||||
if (len > 0) {
|
||||
len -= c;
|
||||
}
|
||||
}
|
||||
yield();
|
||||
}
|
||||
Serial.println();
|
||||
Serial.print("[HTTP] connection closed or file end.\n");
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
else {
|
||||
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
return 1; // File was fetched from web
|
||||
}
|
||||
Reference in New Issue
Block a user