Esp8266 Http Client als Arduino Tab.
HttpClient.ino
// ****************************************************************
// Sketch Esp8266 Http Client Modular(Tab)
// created: Jens Fleischer, 2018-08-20
// last mod: Jens Fleischer, 2020-01-02
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266
// Software: Esp8266 Arduino Core 2.5.2 / 2.6.2 / 2.6.3
// Getestet auf: Nodemcu, Wemos D1 Mini Pro
/******************************************************************
Copyright (c) 2018 Jens Fleischer. All rights reserved.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*******************************************************************/
// Diese Version von HttpClient sollte als Tab eingebunden werden.
// #include <ESP8266WebServer.h> oder #include <ESP8266WiFi.h> muss im Haupttab aufgerufen werden
// Die Funktion "Connect();" muss im Setup eingebunden sein.
// Die Funktion "httpClient();" kann nach dem Verbindungsaufbau aufgerufen werden.
/**************************************************************************************/
#include <ESP8266HTTPClient.h>
const char* URL = "http://192.168.178.33/bme280"; // trage hier die URL deines ESP... mit Bme280 Sketch ein
void httpClient() {
WiFiClient client;
HTTPClient http;
if (http.begin(client, URL)) { // für Core Version 2.4.x Zeile austauschen if (http.begin(URL)) {
Serial.printf("\n%s meldet in Zeile: %d -> HttpClient Begin\n\n", __PRETTY_FUNCTION__, __LINE__);
int16_t httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP / 1.1 %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Antwort: " + payload);
int8_t tempInt = payload.substring(2, payload.length()).toInt();
Serial.printf("Aussentemperatur: %d°C\n", tempInt); // Temperatur als Ganzzahl
float tempFloat = payload.substring(2, payload.length()).toFloat();
Serial.printf("Aussentemperatur: %.1f°C\n", tempFloat); // Temperatur als Gleitkommazahl mit einer Nachkommastelle
}
}
else {
Serial.println("Fehler: " + http.errorToString(httpCode));
}
http.end();
}
}
Du kannst den Http Client in jedem beliebigen Programmteil aufrufen.
Du kannst "httpClient();"nach dem Verbindungsaufbau in deinem Sketch einbinden.
*************** einmalig im setup ****************
httpClient();
Beispiel:
******************** in loop *********************
const unsigned long interval = 1000UL * 20; // Interval in Sekunden einstellen
static unsigned long letzteMillis = 0;
unsigned long aktuelleMillis = millis();
if (aktuelleMillis - letzteMillis >= interval) {
letzteMillis = aktuelleMillis;
httpClient();
}