|
| 1 | +/* |
| 2 | +40000000 / year |
| 3 | +6.342 / 5 sec |
| 4 | +1.268 / sec |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <SPI.h> |
| 8 | +#include <ESP8266WiFi.h> |
| 9 | +#include <ESP8266HTTPClient.h> |
| 10 | + |
| 11 | +#define MAX7219_NUM_CHIPS 1 // 3 chips = 24 digits |
| 12 | +#define MAX7219_CS_PIN 15 // pin the MAX7219's CS pin is connected to |
| 13 | +#define MAX7219_BRIGHTNESS 3 // brightness level 0-15 |
| 14 | + |
| 15 | +#include <MAX7219_8_Digit_Driver.h> |
| 16 | + |
| 17 | +MAX7219_8_Digit_Driver my_display(MAX7219_CS_PIN, MAX7219_NUM_CHIPS); |
| 18 | + |
| 19 | +// Wifi |
| 20 | +#include "mySettings.h" |
| 21 | + |
| 22 | +// Let'sEncrypt root cert |
| 23 | +const char IRG_Root_X1 [] PROGMEM = R"CERT( |
| 24 | +-----BEGIN CERTIFICATE----- |
| 25 | +MIIFYDCCBEigAwIBAgIQQAF3ITfU6UK47naqPGQKtzANBgkqhkiG9w0BAQsFADA/ |
| 26 | +MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT |
| 27 | +DkRTVCBSb290IENBIFgzMB4XDTIxMDEyMDE5MTQwM1oXDTI0MDkzMDE4MTQwM1ow |
| 28 | +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh |
| 29 | +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwggIiMA0GCSqGSIb3DQEB |
| 30 | +AQUAA4ICDwAwggIKAoICAQCt6CRz9BQ385ueK1coHIe+3LffOJCMbjzmV6B493XC |
| 31 | +ov71am72AE8o295ohmxEk7axY/0UEmu/H9LqMZshftEzPLpI9d1537O4/xLxIZpL |
| 32 | +wYqGcWlKZmZsj348cL+tKSIG8+TA5oCu4kuPt5l+lAOf00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STC5jcmwwHQYDVR0OBBYEFHm0WeZ7tuXkAXOACIjIGlj26Ztu |
| 48 | +MA0GCSqGSIb3DQEBCwUAA4IBAQAKcwBslm7/DlLQrt2M51oGrS+o44+/yQoDFVDC |
| 49 | +5WxCu2+b9LRPwkSICHXM6webFGJueN7sJ7o5XPWioW5WlHAQU7G75K/QosMrAdSW |
| 50 | +9MUgNTP52GE24HGNtLi1qoJFlcDyqSMo59ahy2cI2qBDLKobkx/J3vWraV0T9VuG |
| 51 | +WCLKTVXkcGdtwlfFRjlBz4pYg1htmf5X6DYO8A4jqv2Il9DjXA6USbW1FzXSLr9O |
| 52 | +he8Y4IWS6wY7bCkjCWDcRQJMEhg76fsO3txE+FiYruq9RUWhiF1myv4Q6W+CyBFC |
| 53 | +Dfvp7OOGAN6dEOM4+qR9sdjoSYKEBpsr6GtPAQw4dy753ec5 |
| 54 | +-----END CERTIFICATE----- |
| 55 | +)CERT"; |
| 56 | + |
| 57 | +X509List cert(IRG_Root_X1); |
| 58 | + |
| 59 | +int counter = 0; |
| 60 | + |
| 61 | +#include <time.h> |
| 62 | + |
| 63 | +void setup() { |
| 64 | + Serial.begin(115200); delay(10); |
| 65 | + Serial.println(); |
| 66 | + |
| 67 | + my_display.String_To_Buffer(" H1 ", MAX7219_BRIGHTNESS); |
| 68 | + |
| 69 | + // ssid & pass in settings.ino - DO NOT CHECK IN! |
| 70 | + WiFi.begin(ssid, pass); |
| 71 | + Serial.print("Connecting to "); Serial.print(ssid); Serial.println(" ..."); |
| 72 | + |
| 73 | + while (WiFi.status() != WL_CONNECTED) { |
| 74 | + Serial.print("."); |
| 75 | + delay(1000); |
| 76 | + } |
| 77 | + |
| 78 | + Serial.print("Connected, IP: "); Serial.println(WiFi.localIP()); |
| 79 | + |
| 80 | + my_display.String_To_Buffer(WiFi.localIP().toString(), MAX7219_BRIGHTNESS); |
| 81 | + delay(3000); |
| 82 | + |
| 83 | + configTime(3*3600, 0, "pool.ntp.org", "time.nist.gov"); |
| 84 | + Serial.print("Waiting for NTP time sync: "); |
| 85 | + |
| 86 | + time_t now = time(nullptr); |
| 87 | + |
| 88 | + while (now < 8*3600*2) { |
| 89 | + delay(500); |
| 90 | + Serial.print("."); |
| 91 | + now = time(nullptr); |
| 92 | + } |
| 93 | + Serial.println(); |
| 94 | + |
| 95 | + struct tm timeinfo; |
| 96 | + gmtime_r(&now, &timeinfo); |
| 97 | + Serial.print("Current time: "); |
| 98 | + Serial.print(asctime(&timeinfo)); |
| 99 | + |
| 100 | + WiFiClientSecure client; |
| 101 | + |
| 102 | + // Wait for WiFi connection |
| 103 | + if ((WiFi.status() == WL_CONNECTED)) { |
| 104 | + |
| 105 | + client.setTrustAnchors(&cert); |
| 106 | + |
| 107 | + HTTPClient https; |
| 108 | + |
| 109 | + Serial.print("[HTTPS] Begin\n"); |
| 110 | + // API URL in settings.ino - DO NOT CHECK IN! |
| 111 | + if (https.begin(client, apiurl)) { // HTTPS |
| 112 | + |
| 113 | + Serial.print("[HTTPS] GET ... "); |
| 114 | + // Start connection and send HTTP header |
| 115 | + int httpCode = https.GET(); |
| 116 | + |
| 117 | + // httpCode will be negative on error |
| 118 | + if (httpCode > 0) { |
| 119 | + // HTTP header has been send and Server response header has been handled |
| 120 | + Serial.printf("code: %d\n", httpCode); |
| 121 | + |
| 122 | + // File found at server |
| 123 | + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { |
| 124 | + counter = https.getString().toInt(); |
| 125 | + Serial.printf("[HTTPS] GET value: %i\n", counter); |
| 126 | + } |
| 127 | + } else { |
| 128 | + Serial.printf("failed, error: %s\n", https.errorToString(httpCode).c_str()); |
| 129 | + my_display.String_To_Buffer("6Et FA1L", MAX7219_BRIGHTNESS); |
| 130 | + } |
| 131 | + |
| 132 | + https.end(); |
| 133 | + } else { |
| 134 | + Serial.printf("[HTTPS] Unable to connect\n"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + struct tm t; |
| 139 | + time_t t_of_day; |
| 140 | + t.tm_year = timeinfo.tm_year; |
| 141 | + t.tm_mon = 0; |
| 142 | + t.tm_mday = 1; |
| 143 | + t.tm_hour = 0; |
| 144 | + t.tm_min = 0; |
| 145 | + t.tm_sec = 0; |
| 146 | + t.tm_isdst = timeinfo.tm_isdst; // We already have the current time, get DST from that. |
| 147 | + t_of_day = mktime(&t) + 3*3600; // Add some hours to get to GMT. See also timegm() and timezone foo. |
| 148 | + Serial.println(t_of_day); |
| 149 | + |
| 150 | + |
| 151 | + //Serial.println(timest_jan1); |
| 152 | +} |
| 153 | + |
| 154 | +void loop() { |
| 155 | + // The decimal point does not count against the length of the character string |
| 156 | + my_display.String_To_Buffer(String(counter++), MAX7219_BRIGHTNESS); |
| 157 | + delay(1000); |
| 158 | +} |
0 commit comments