I have a problem how to program

i have a problem how to post temperature and humidity data into cloud. anybody can help me how to write a phyton program. i have a draf as below. but not working

from datetime import datetime
import Adafruit_DHT
import time
import network

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

You can generate a Token from the “Tokens Tab” in the UI

token = “xxxxxxxxxxxxxxx”
org = “xx”
bucket = “DHT22”

client = InfluxDBClient(url=“https://us-central1-1.gcp.cloud2.influxdata.com”, token=token)

write_api = client.write_api(write_options=SYNCHRONOUS)

data = “room1,host=raspberry1 temperature=43.43234543”
data = “room1,host=raspberry1 humidity=83.43234543”
write_api.write(bucket, org, data)

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
    print("Temp={0:0.1f}*C  Humidity={1:0.1f}% ".format(temperature, humidity))
    print(temperature, humidity)
else:
    print("Failed to retrieve data from humidity sensor")

time.sleep(3)

@shahrilmajid - welcome to the community! What error are you seeing? You didn’t explain what is not working. You appear to be using the InfluxDBClient correctly except you are only writing the humidity value because you are overwriting data. Use two calls to write_api.write(bucket, org, data). If you are having problems with the Adafruit library, we likely won’t be able to help you with those here.

i have a problem how to compile this together. data wont appear on influxdb

from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

You can generate a Token from the “Tokens Tab” in the UI

token = “gXtXDdB-j3GKQi8BdASZs1o9s6YAnYbSe4-yUPY4TCnixlw8V4HGYFE5eBMqdykiqUJQTDApdFgi2tQuET2hGw==”
org = “oem”
bucket = “DHT22”

client = InfluxDBClient(url=“https://us-central1-1.gcp.cloud2.influxdata.com”, token=token)

write_api = client.write_api(write_options=SYNCHRONOUS)

#----------------------------------------------------------------
import Adafruit_DHT
import time
import network

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
    print("Temp={0:0.1f}*C  Humidity={1:0.1f}% ".format(temperature, humidity))
    print(temperature, humidity)
else:
    print("Failed to retrieve data from humidity sensor")

time.sleep(3)
#___--

data = “room1,host=raspberry1 temperature=(temperature)”
write_api.write(bucket, org, data)

Are you getting an error or problem message? Please share any error messages.

Please also create a new token since you’ve shared it in a public forum.

emp=31.2*C Humidity=75.1%
Traceback (most recent call last):
File “/home/pi/mu_code/influx.py”, line 30, in
write_api.write(bucket, org, data)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/client/write_api.py”, line 250, in write
results = list(map(write_payload, payloads.items()))
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/client/write_api.py”, line 248, in write_payload
return self._post_write(_async_req, bucket, org, final_string, payload[0])
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/client/write_api.py”, line 362, in _post_write
**kwargs)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/service/write_service.py”, line 64, in post_write
(data) = self.post_write_with_http_info(org, bucket, body, **kwargs) # noqa: E501
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/service/write_service.py”, line 183, in post_write_with_http_info
urlopen_kw=urlopen_kw)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/api_client.py”, line 345, in call_api
_preload_content, _request_timeout, urlopen_kw)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/api_client.py”, line 174, in __call_api
_request_timeout=_request_timeout, **urlopen_kw)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/api_client.py”, line 392, in request
**urlopen_kw)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/rest.py”, line 309, in POST
**urlopen_kw)
File “/home/pi/.local/lib/python3.7/site-packages/influxdb_client/rest.py”, line 252, in request
raise ApiException(http_resp=r)
influxdb_client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Tue, 01 Sep 2020 13:45:42 GMT’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘Content-Length’: ‘120’, ‘Connection’: ‘keep-alive’, ‘x-platform-error-code’: ‘invalid’, ‘Strict-Transport-Security’: ‘max-age=15724800; includeSubDomains’})
HTTP response body: {“code”:“invalid”,“message”:“unable to parse ‘room1, host=raspberry1, temperature=31.200000762939453’: missing tag key”}

https://1drv.ms/u/s!AnOgJvdaf1-thEhLpAXcE1cShc58?e=p93amQ

Per the error message, you have an extra in the line protocol right after room1,. The tag key host has to come right after the measurement name and comma like room1,host=raspberry1 ...

Check out: InfluxDB line protocol tutorial | InfluxDB OSS v1 Documentation