MATLAB Answers

Configure header and connection address.

7 ビュー (過去 30 日間)
Yash Mehta
Yash Mehta 2020 年 8 月 19 日
コメント済み: Yash Mehta 2020 年 8 月 20 日
I need help with weatherstation code. I am new to ThingsSpeak. i have set up this code for bme280 to monitor room temprature. I have no idea what configure haeader and connection address is. i have highlited it in the code below please help me .
import time
from w1thermsensor import W1ThermSensor
import board
import busio
import adafruit_bme280
i2c = busio.I2C(board.SCL, board.SDA)
bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)
#import adafruit_ads1x15.ads1015 as ADS
#from adafruit_ads1x15.analog_in import AnalogIn
import RPi.GPIO as GPIO
import urllib
import http.client
key = "*MY KEY"
interval = 5 #How long we want to wait between loops (seconds)
while True:
#Pull Temperature from DS18B20
# temperature = ds18b20.get_temperature()
#Pull temperature from BME280
temperature = bme.temperature
#Pull pressure from BME280 Sensor & convert to kPa
pressure_pa = bme.pressure
pressure = pressure_pa / 10
#Pull humidity from BME280
humidity = bme.humidity
#Print the results
# print( 'Temperature: ' , temperature)
# print( 'Humidity: ' , humidity, '%')
# print( 'Pressure: ' , pressure, 'kPa')
# print( ' ')
params = urllib.parse.urlencode({'field1' : temperature, 'field2' : humidity, 'field3' : pressure})
####Configure header / connection address###
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = http.client.HTTPConnection("")
#Try to connect to ThingSpeak and send Data
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print( response.status, response.reason)
data =
#Catch the exception if the connection fails
print( "connection failed")

  0 件のコメント


回答 (1 件)

Christopher Stapels
Christopher Stapels 2020 年 8 月 20 日
The headers and connection address seem correct. I might take the ZZ out of "content-type".
You can see more information at the Write Data doc page.
Are you getting a success code for the response?

  3 件のコメント

Yash Mehta
Yash Mehta 2020 年 8 月 20 日
No Response. Script is running but no success response. Edit: even with the ZZ removed same thing. no response.
Christopher Stapels
Christopher Stapels 2020 年 8 月 20 日
I don't think its a ThingSpeak issue. You have several print statments before the ThingSpeak post that aren't hapeining. I'd take everything out of the code and add stuff back in one at a time to see if you can see what is breaking it.
Yash Mehta
Yash Mehta 2020 年 8 月 20 日
Alrigtht will try that. Thanks for your response. Appreciate it.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by