quotes.ino
1 // SPDX-FileCopyrightText: 2017 Evandro Copercini 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 /* 6 Wifi secure connection example for ESP32 7 Running on TLS 1.2 using mbedTLS 8 2017 - Evandro Copercini - Apache 2.0 License. 9 */ 10 11 #include <WiFiClientSecure.h> 12 #include <ArduinoJson.h> 13 #include "Adafruit_ThinkInk.h" 14 #include "Adafruit_NeoPixel.h" 15 #include <Fonts/FreeSans9pt7b.h> 16 17 const char* ssid = "adafruit"; // your network SSID (name of wifi network) 18 const char* password = "ffffffff"; // your network password 19 20 const char* server = "www.adafruit.com"; 21 const char* path = "/api/quotes.php"; 22 23 WiFiClientSecure client; 24 ThinkInk_290_Grayscale4_T5 display(EPD_DC, EPD_RESET, EPD_CS, -1, -1); 25 Adafruit_NeoPixel intneo = Adafruit_NeoPixel(4, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); 26 27 28 void deepSleep() { 29 pinMode(NEOPIXEL_POWER, OUTPUT); 30 pinMode(SPEAKER_SHUTDOWN, OUTPUT); 31 digitalWrite(SPEAKER_SHUTDOWN, LOW); // off 32 digitalWrite(NEOPIXEL_POWER, HIGH); // off 33 digitalWrite(EPD_RESET, LOW); // off (yes required to save a few mA) 34 pinMode(13, OUTPUT); 35 digitalWrite(13, LOW); 36 37 esp_sleep_enable_timer_wakeup(60 * 1000000); // 60 seconds 38 esp_deep_sleep_start(); 39 } 40 41 void setup() { 42 //Initialize serial and wait for port to open: 43 Serial.begin(115200); 44 //while (!Serial) delay(10); 45 46 pinMode(BUTTON_A, INPUT_PULLUP); 47 pinMode(BUTTON_B, INPUT_PULLUP); 48 pinMode(BUTTON_C, INPUT_PULLUP); 49 pinMode(BUTTON_D, INPUT_PULLUP); 50 pinMode(EPD_BUSY, INPUT); 51 pinMode(13, OUTPUT); 52 digitalWrite(13, HIGH); 53 54 55 display.begin(THINKINK_GRAYSCALE4); 56 57 Serial.print("Attempting to connect to SSID: "); 58 Serial.println(ssid); 59 60 display.clearBuffer(); 61 display.setFont(&FreeSans9pt7b); 62 display.setTextSize(1); 63 display.setTextColor(EPD_BLACK); 64 display.setCursor(10, 30); 65 display.print("Connecting to SSID "); 66 display.println(ssid); 67 display.display(); 68 69 WiFi.begin(ssid, password); 70 71 // attempt to connect to Wifi network: 72 while (WiFi.status() != WL_CONNECTED) { 73 Serial.print("."); 74 delay(100); 75 } 76 77 Serial.print("Connected to "); 78 Serial.println(ssid); 79 80 //client.setCACert(test_root_ca); 81 //client.setCertificate(test_client_key); // for client verification 82 //client.setPrivateKey(test_client_cert); // for client verification 83 84 // Neopixel power 85 pinMode(NEOPIXEL_POWER, OUTPUT); 86 digitalWrite(NEOPIXEL_POWER, LOW); // on 87 intneo.fill(25, 0, 0); 88 intneo.show(); 89 90 Serial.println("\nStarting connection to server..."); 91 client.setInsecure(); 92 if (!client.connect(server, 443)) { 93 Serial.println("Connection failed!"); 94 deepSleep(); 95 } 96 97 intneo.fill(25, 25, 0); 98 intneo.show(); 99 100 Serial.println("Connected to server!"); 101 // Make a HTTP request: 102 client.print("GET "); client.print(path); client.println(" HTTP/1.1"); 103 client.print("Host: "); client.println(server); 104 client.println("Connection: close"); 105 client.println(); 106 107 // Check HTTP status 108 char status[32] = {0}; 109 client.readBytesUntil('\r', status, sizeof(status)); 110 if (strcmp(status, "HTTP/1.1 200 OK") != 0) { 111 Serial.print(F("Unexpected response: ")); 112 Serial.println(status); 113 deepSleep(); 114 } 115 116 intneo.fill(0, 25, 0); 117 intneo.show(); 118 while (client.connected()) { 119 String line = client.readStringUntil('\n'); 120 if (line == "\r") { 121 Serial.println("headers received"); 122 break; 123 } 124 } 125 126 intneo.fill(0, 25, 25); 127 intneo.show(); 128 while (client.peek() != '[') { 129 client.read(); 130 } 131 132 intneo.fill(0, 0, 25); 133 intneo.show(); 134 135 // Allocate the JSON document 136 // Use arduinojson.org/v6/assistant to compute the capacity. 137 const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(8) + 200; 138 DynamicJsonDocument doc(capacity); 139 140 // Parse JSON object 141 DeserializationError error = deserializeJson(doc, client); 142 if (error) { 143 Serial.print(F("deserializeJson() failed: ")); 144 Serial.println(error.c_str()); 145 deepSleep(); 146 } 147 148 intneo.fill(0, 25, 25); 149 intneo.show(); 150 151 // Extract values 152 JsonObject root_0 = doc[0]; 153 Serial.println(F("Response:")); 154 const char* root_0_text = root_0["text"]; 155 const char* root_0_author = root_0["author"]; 156 157 Serial.print("Quote: "); Serial.println(root_0_text); 158 Serial.print("Author: "); Serial.println(root_0_author); 159 160 display.clearBuffer(); 161 display.setFont(&FreeSans9pt7b); 162 display.setTextSize(1); 163 display.setTextWrap(true); 164 display.setTextColor(EPD_BLACK); 165 display.setCursor(10, 30); 166 display.println(root_0_text); 167 display.setTextColor(EPD_DARK); 168 display.setCursor(40, 120); 169 display.println(root_0_author); 170 display.display(); 171 while (!digitalRead(EPD_BUSY)) { 172 delay(10); 173 } 174 175 while (client.available() > 0) 176 { 177 //read back one line from the server 178 String line = client.readStringUntil('\r'); 179 Serial.println(line); 180 } 181 182 // disconnect 183 client.stop(); 184 deepSleep(); 185 } 186 187 void loop() { 188 189 }