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:
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();
}