Skip to content

Instantly share code, notes, and snippets.

@cho0h5
Created September 30, 2023 08:31
Show Gist options
  • Save cho0h5/b5209b75a0c5d961e1034e044ee816d1 to your computer and use it in GitHub Desktop.
Save cho0h5/b5209b75a0c5d961e1034e044ee816d1 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define SSID "your-ssid"
#define PASSWORD "your-passwd"
#define INFLUXDB_URL "http://localhost:8086/api/v2/write?org=your-org&bucket=your-bucket"
#define INFLUXDB_TOKEN "Token akKEWNFwjbfalwkejfk-your-token-awlejhfklwjaehfkljawhefkhKLHWEUbkjwae=="
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(SSID, PASSWORD);
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, INFLUXDB_URL)) { // HTTP
http.addHeader("Content-Type", "application/vnd.flux");
http.addHeader("Accept", "application/csv");
http.addHeader("Authorization", INFLUXDB_TOKEN);
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.POST("weather,sensor=bmp180 temperature=25.5,pressure=101111.10");
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
http.writeToStream(&Serial);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("[HTTP] Unable to connect");
}
}
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment