Hi,
I tried to add “Events” screen to ThingPulse ESP8266 Weather Station Color. But I have a problem.
Events should be loaded from my Google Calendar. So, I set Google script for sending data from my calendar and then I wrote this code to get Google calendar data using HTTPS Redirect library. Everything is working if the screen is not initialized, I can get data from my calendar. But when the screen is initialized by ThingPulse MiniGrafx library I can’t connect to Google server (“Could not connect to server: …”) and I don’t know where is the problem. Does anyone have also this problem?
test.ino
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include "DebugMacros.h"
#include "settings.h"
#include <SPI.h>
#include <ILI9341_SPI.h>
#include <MiniGrafx.h>
#include <Carousel.h>
#define MINI_BLACK 0
#define MINI_WHITE 1
#define MINI_YELLOW 2
#define MINI_BLUE 3
// defines the colors usable in the paletted 16 color frame buffer
uint16_t palette[] = {ILI9341_BLACK, // 0
ILI9341_WHITE, // 1
ILI9341_YELLOW, // 2
0x7E3C
}; //3
int SCREEN_WIDTH = 240;
int SCREEN_HEIGHT = 320;
// Limited to 4 colors due to memory constraints
int BITS_PER_PIXEL = 2; // 2^2 = 4 colors
ADC_MODE(ADC_VCC);
ILI9341_SPI tft = ILI9341_SPI(TFT_CS, TFT_DC);
MiniGrafx gfx = MiniGrafx(&tft, BITS_PER_PIXEL, palette);
Carousel carousel(&gfx, 0, 0, 240, 100);
const char* host = "script.google.com";
const int httpsPort = 443; //the https port is same
String url = String("/macros/s/") + GOOGLE_SCRIPT_ID + "/exec";
HTTPSRedirect* client = nullptr;
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH); // HIGH to Turn on;
// --------------- !! Code for getting data from Google script is working as expected if the display is not initialized, when the screen is initialized I can't connect to the Google server.
gfx.init();
gfx.fillBuffer(MINI_BLACK);
gfx.commit();
connectWifi();
getGoogleEvents();
}
void loop() {
}
void connectWifi() {
if (WiFi.status() == WL_CONNECTED) return;
Serial.print("Connecting to WiFi" + String(WIFI_SSID) + "/" + String(WIFI_PASS));
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.hostname(WIFI_HOSTNAME);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.println();
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (i > 80) i = 0;
i += 10;
Serial.print(".");
}
Serial.println("Connected...");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// get data from Google script
void getGoogleEvents() {
// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
Serial.println("Connecting to: " + String(host));
// Try to connect for a maximum of 5 times then exit
bool flag = false;
for (int i = 0; i < 5; i++) {
int retval = client->connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println("Connection failed. Retrying...");
}
if (!flag) {
Serial.print("Could not connect to server: " + String(host) + " Exiting...");
return;
}
Serial.println("\nGET: Google Calendar Data:");
client->GET(url, host);
String googleResponseData = client->getResponseBody();
// delete HTTPSRedirect object
delete client;
client = nullptr;
}
settings.h
#include <simpleDSTadjust.h>
// Setup
#define WIFI_SSID "WIFI-NAME"
#define WIFI_PASS "WIFI-PASS"
#define WIFI_HOSTNAME ""
// Google Script Settings
const char *GOOGLE_SCRIPT_ID = "google-script-id"; // Replace with your own google script id
// echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
const char* fingerprint = "fingeprint";
// Pins for the ILI9341
#define TFT_DC D2
#define TFT_CS D1
#define TFT_LED D8
Google script
function doGet(e) {
var cal = CalendarApp.getCalendarsByName('Calendar-Name')[0]; // 0 is subcalendar ID, mostly "0"
if (cal == undefined) {
return ContentService.createTextOutput("no access to calendar");
}
const now = new Date();
var start = new Date(); start.setHours(0, 0, 0); // start at midnight
const oneday = 24*3600000; // [msec]
const stop = new Date(start.getTime() + 14 * oneday); //get appointments for the next 14 days
var events = cal.getEvents(start, stop); //pull start/stop time
var str = '';
for (var ii = 0; ii < events.length; ii++) {
var event=events[ii];
var myStatus = event.getMyStatus();
// define valid entryStatus to populate array
switch(myStatus) {
case CalendarApp.GuestStatus.OWNER:
case CalendarApp.GuestStatus.YES:
case CalendarApp.GuestStatus.NO:
case CalendarApp.GuestStatus.INVITED:
case CalendarApp.GuestStatus.MAYBE:
default:
break;
}
// Show just every entry regardless of GuestStatus to also get events from shared calendars where you haven't set up the appointment on your own
str += event.getStartTime() + ';' +
//event.isAllDayEvent() + '\t' +
//event.getPopupReminders()[0] + '\t' +
event.getTitle() +';' +
event.isAllDayEvent() + ';';
}
return ContentService.createTextOutput(str);
}