1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// WDT, permet le redémarrage automatique en cas de blocage inattendu, qui peut être causé par le décodage JPEG.
#define ENABLE_WDT
// select setting
#include "settings.h"
#include <time.h>
#include <WiFi.h>
#include <esp_jpg_decode.h>
#include <esp_task_wdt.h>
#include <SPI.h>
#include <HTTPClient.h>
/* display settings */
#include <Arduino_GFX_Library.h>
#define TFT_BL 22
Arduino_DataBus *bus = new Arduino_ESP32SPI(27 /* DC */, 5 /* CS */,18 /* SCK */, 23 /* MOSI */, GFX_NOT_DEFINED /* MISO */);
Arduino_GFX *gfx = new Arduino_ILI9341(bus,33 /* RST */, 1 /* rotation */);
static int len, offset;
HTTPClient http;
//***************************************//
// SETUP //
//***************************************//
void setup()
{
Serial.begin(115200);
gfx->begin();
gfx->fillScreen(BLACK);
Serial.println("Connecting WiFi .");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
#ifdef TFT_BL
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
#endif
#ifdef ENABLE_WDT
// set WDT timeout a little bit longer than HTTP timeout
esp_task_wdt_init((HTTPCLIENT_DEFAULT_TCP_TIMEOUT / 1000) + 2, true);
enableLoopWDT();
#endif
}
//******************************************//
// LOOP //
//******************************************//
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
// wait for WiFi connection
delay(500);
}else{
Serial.print("[HTTP] begin...\n");
//http.begin(url);
http.begin(URL);
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// HTTP header has been send and Server response header has been handled
if (httpCode <= 0)
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
else
{
if (httpCode != HTTP_CODE_OK)
{
Serial.printf("[HTTP] Not OK!\n");
delay(5000);
} else {
// get lenght of document (is -1 when Server sends no Content-Length header)
len = http.getSize();
Serial.printf("[HTTP] size: %d\n", len);
if (len <= 0)
{
Serial.printf("[HTTP] Unknow content size: %d\n", len);
} else {
// get tcp stream
WiFiClient *http_stream = http.getStreamPtr();
esp_jpg_decode(len, JPG_SCALE, http_stream_reader, tft_writer, http_stream /* arg */);
}
}
}
http.end();
delay(5000);
}
#ifdef ENABLE_WDT
// notify WDT still working
feedLoopWDT();
#endif
}
//*********************************************************************************
static size_t http_stream_reader(void *arg, size_t index, uint8_t *buf, size_t len)
{
WiFiClient *http_stream = (WiFiClient *)arg;
if (buf)
{
//Serial.printf("[HTTP] read: %d\n", len);
size_t a = http_stream->available();
size_t r = 0;
while (r < len)
{
r += http_stream->readBytes(buf + r, min((len - r), a));
}
return r;
}
else
{
// Serial.printf("[HTTP] skip: %d\n", len);
int l = len;
while (l--)
{
http_stream->read();
yield(); // <=========================================
}
return len;
}
}
//***********************************************************************************************
static bool tft_writer(void *arg, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t *data)
{
if (data)
{
//Serial.printf("%d, %d, %d, %d\n", x, y, w, h);
gfx->draw24bitRGBBitmap(x, y, data, w, h);
}
#ifdef ENABLE_WDT
// notify WDT still working
feedLoopWDT();
#endif
return true; // Continue to decompression
} |
Partager