|
| 1 | +/** |
| 2 | + PostHttpsClient.ino |
| 3 | + Created on: 28.06.2021 |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <Arduino.h> |
| 7 | +#include <ESP8266WiFi.h> |
| 8 | +#include <ESP8266WiFiMulti.h> |
| 9 | +#include <WiFiClientSecureBearSSL.h> |
| 10 | +#include <ESP8266HTTPClient.h> |
| 11 | + |
| 12 | +// Fingerprint for demo URL, expires on September 5, 2021, needs to be updated well before this date |
| 13 | +const uint8_t fingerprint[20] = {0xbb, 0x88, 0x7f, 0x7c, 0x77, 0xc2, 0x59, 0x97, 0xb7, 0x00, 0x35, 0x74, 0x50, 0x47, 0x7e, 0x67, 0x42, 0x02, 0x2f, 0xf0}; |
| 14 | +const char* URL = "https://www.reseau-astuce.fr/fr/horaires-a-larret/28/StopTimeTable/NextDeparture"; |
| 15 | +const char* REQUEST = "destinations=%7B%221%22%3A%22Technop%C3%B4le+SAINT-ETIENNE-DU-ROUVRAY%22%7D&stopId=102154&lineId=175&sens=1"; |
| 16 | + |
| 17 | +const char* WIFI_SSID = "myWifiSSID"; |
| 18 | +const char* WIFI_PWD = "myWifiPassword"; |
| 19 | + |
| 20 | +ESP8266WiFiMulti WiFiMulti; |
| 21 | +HTTPClient https; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +void setup() { |
| 26 | + |
| 27 | + Serial.begin(115200); |
| 28 | + Serial.println(); |
| 29 | + Serial.println(); |
| 30 | + Serial.println(); |
| 31 | + |
| 32 | + WiFi.mode(WIFI_STA); |
| 33 | + WiFiMulti.addAP(WIFI_SSID, WIFI_PWD); |
| 34 | + |
| 35 | + Serial.println("[WIFI] Connecting to WiFi ..."); |
| 36 | + while (WiFiMulti.run() != WL_CONNECTED) { |
| 37 | + Serial.print('.'); |
| 38 | + delay(1000); |
| 39 | + } |
| 40 | + |
| 41 | + Serial.println(); |
| 42 | + Serial.print("[WIFI] Connected with IP : "); |
| 43 | + Serial.println(WiFi.localIP()); |
| 44 | + WiFi.setAutoReconnect(true); |
| 45 | + WiFi.persistent(true); |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +void loop() { |
| 52 | + |
| 53 | + if ((WiFiMulti.run() == WL_CONNECTED)) { |
| 54 | + Serial.println("[HTTPS] begin..."); |
| 55 | + |
| 56 | + std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure); |
| 57 | + client->setInsecure(); // Ignore SSL certificate |
| 58 | + //client->setFingerprint(fingerprint); //Use SSL |
| 59 | + HTTPClient https; |
| 60 | + |
| 61 | + if (https.begin(*client, URL)) { |
| 62 | + https.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"); |
| 63 | + https.addHeader("Accept-Language", "fr-FR,fr;q=0.9,en;q=0.8"); |
| 64 | + https.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); |
| 65 | + int httpCode = https.POST(REQUEST); |
| 66 | + |
| 67 | + // If response code is positive => no error |
| 68 | + if (httpCode > 0) { |
| 69 | + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { |
| 70 | + String payload = https.getString(); |
| 71 | + Serial.println("[HTTPS] POST... SUCCESS!"); |
| 72 | + Serial.println(payload); |
| 73 | + } |
| 74 | + |
| 75 | + // Error (response code is negative) |
| 76 | + } else { |
| 77 | + Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str()); |
| 78 | + } |
| 79 | + https.end(); |
| 80 | + |
| 81 | + // Unable to reach the server |
| 82 | + } else { |
| 83 | + Serial.println("[HTTPS] Unable to connect"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments