0

I use the below code and put a right Master Api key from the setting of personal xively site but the result is "SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)" . don't know why it occurred.

from time import localtime, strftime

import time

import requests

import random

Base URL for the requests

url = 'https://api.xively.com'

Replace with your master API key and feed IDs of your devices

MASTER_API_KEY = '***********'

feed_ID_1 = '******'

feed_ID_2 = '******'

feed_ID_3 = '******'

Function for updating all channels of a device using the requests library

:param feed_id: feed ID of a device

:param temperature: temperature value to be updated to a channel

:param humidity: humidity value to be updated to a channel

:param lux: lux value to be updated to a channel

def update_channels(feed_id, temperature, humidity, light):

url_feed =url + '/v2/feeds/' + str(feed_id) + '.json' headers = {'X-ApiKey': str(MASTER_API_KEY)}

payload = '{ \ "version":"1.0.0", \ "datastreams" : [ { \ "id" : "humidity", \ "current_value" : '+str(humidity)+' \ }, \ { \ "id" : "light", \ "current_value" : '+str(light)+' \ }, \ { \ "id" : "temperature", \ "current_value" : '+str(temperature)+' \ } \ ] \ } '\

try: r = requests.put(url=url_feed, headers=headers, data=payload) print (strftime("%a, %d %b %Y %H:%M:%S", localtime()))
print (payload)
print (r.text)
print (r.status_code)
except Exception as e:
print(e)

Loop for updating 3 devices with random values from a given ranges

while True:
update_channels(feed_ID_1,random.uniform(20.0,22.0),random.uniform(50.0,51.0),random.uniform(320.0,325.0)) update_channels(feed_ID_2,random.uniform(22.0,24.0),random.uniform(51.0,52.0),random.uniform(325.0,330.0)) update_channels(feed_ID_3,random.uniform(24.0,26.0),random.uniform(52.0,53.0),random.uniform(330.0,335.0)) time.sleep(1)

  • on your exception put the following: except Exception as e: print(e) In order to get a better view on why requests is raising an exception, then update your question with the result from the exception, because most likely is not a connection failed error – Ilhicas Apr 27 '17 at 14:02
  • ok done it. here the error: SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645 – user7364359 Apr 27 '17 at 15:53
  • Refer to this answer then, the problem might be in ssl certificate of the api you are trying to access http://stackoverflow.com/questions/34503206/ssl-certificate-verify-failed-python – Ilhicas Apr 27 '17 at 16:21

0 Answers0