Write data to influxDB cloud with MicroPython

Hey folks,
i want to write some sensor data with my raspberry pico w to my influxDB cloud.
I saw there is a way to manage that by using the influxDB client with python. As far as I know there is no way doing that withe the MicroPython/PicoW combination right?

But is there another way to get it going maybe through http requests ore something?

Hello @Florian99,
Hello, I can’t think of an example using MicroPython with the Python Client library. I don’t know if its compatible, but I suspect it isnt. You should be able to make http requests though. You can use urequests which is a micropython version of the requests library:

import urequests

url = "https://us-west-2-1.aws.cloud2.influxdata.com/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns"
headers = {
    "Authorization": "Token YOUR_API_TOKEN",
    "Content-Type": "text/plain; charset=utf-8",
    "Accept": "application/json"
}
data = '''
    airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
    airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
'''

response = urequests.post(url, headers=headers, data=data)

print(response.text)
response.close()

I hope that helps!

Also as an aside and for users looking for something similary:
There are examples for arduino and ESP but not Raspberry PI.
With the Arduino client library

#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif

#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
#define INFLUXDB_URL "https://us-west-2-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "UWixzECaEBqW1QU8r-ChF-SvVknccF39qCeMg7jzIogZyV9ZjgMBCnZ-K-LgJdoH77fRkthOmE7lbHQRfNahIA=="
#define INFLUXDB_ORG "0437f6d51b579000"
#define INFLUXDB_BUCKET "06021398b2fa0000"

#define TZ_INFO "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00"
#define DHTPIN 12 // D7 on node mcu.  Other pins get set HIGH/LOW during boot and seem to stop the sensor working until replugged.
#define DHTTYPE DHT22

InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
Point influx_sensor("dht22"); // Set up Influx data point for sensor
DHT_Unified dht(DHTPIN, DHTTYPE); // create dht temperature sensor

void setup() {
  Serial.begin(115200);
  while(!Serial) { };
  // Setup wifi
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("\n\nConnecting to wifi");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }
  
  Serial.println(F("Wifi Started"));
  Serial.print(F("IP Address: "));
  Serial.println(WiFi.localIP());
  Serial.println(F("-------------------------------------"));

  timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  //client.validateConnection(); // test to see if it fires an exception
  // https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/issues/86
  // Check server connection //  This was throwing exceptions all over the place for me
//  if (client.validateConnection()) {
//    Serial.print("Connected to InfluxDB: ");
//    Serial.println(client.getServerUrl());
//  } else {
//    Serial.print("InfluxDB connection failed: ");
//    Serial.println(client.getLastErrorMessage());
//  }

  dht.begin();
  sensor_t temp_sensor;
  dht.temperature().getSensor(&temp_sensor);
  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("°C"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("°C"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("°C"));
  Serial.println(F("------------------------------------"));
  
  dht.humidity().getSensor(&temp_sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print  (F("Sensor Type: ")); Serial.println(temp_sensor.name);
  Serial.print  (F("Driver Ver:  ")); Serial.println(temp_sensor.version);
  Serial.print  (F("Unique ID:   ")); Serial.println(temp_sensor.sensor_id);
  Serial.print  (F("Max Value:   ")); Serial.print(temp_sensor.max_value); Serial.println(F("%"));
  Serial.print  (F("Min Value:   ")); Serial.print(temp_sensor.min_value); Serial.println(F("%"));
  Serial.print  (F("Resolution:  ")); Serial.print(temp_sensor.resolution); Serial.println(F("%"));
  Serial.println(F("------------------------------------"));  

  influx_sensor.addTag("user", "cool_plant_lady");
}

void loop() {  
  Serial.println("Wait 10s");
  delay(10000);
  
  sensors_event_t event; // DHT temperature sensor
  
  dht.temperature().getEvent(&event);
  if(isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
    } else {
      Serial.print(F("Temperature: "));
      Serial.print(event.temperature);
      Serial.println(F("°C"));
      influx_sensor.addField("temperature", event.temperature); // I dont like using "temp" for temperature
    }
  
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
    influx_sensor.addField("humidity", event.temperature);
  }


  // Print what are we exactly writing
  Serial.print("Writing: ");
  Serial.println(influx_sensor.toLineProtocol());
  // If no Wifi signal, try to reconnect it
  if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED)) {
    Serial.println("Wifi connection lost");
  }
  // Write point
  Serial.println("XX");
  if (!client.writePoint(influx_sensor)) {
    Serial.print("InfluxDB write failed: ");
    Serial.println(client.getLastErrorMessage());
  }
influx_sensor.clearFields();
}

Thanks you i think it could work but i 'd like to intgrate some variable in my data fot example:

data = ‘’’
sensorName+‘,Capteur_id=’+sensorId+’ Dioxyde_carbone=‘+str(Moy_List)+’,Temperature=‘+str(var_temp)+’,Humidite=‘+str(humidity)
‘’’

it’s retur me a erreur because the format of the data it’s wrong i’m suppose thanks you if you could to help me and thanks you for your propose

Im working on the same thing. As soon i got working code i wil let you know

Find a simple micropython based client here:
micropythonInfluxDB

Use request or ureqest and and I’m just in a test phase on one of my ESP32 boards.

GIT repository will be updated soon.