It try to store data (temperature and humidity) from a sensor DHT11 to a cloud service (ThingSpeak)
(https://thingspeak.com/)in witch I've create my account (free) create my fields....I have my write API KEY and so on......
My program do not work , I try an other one (below) from"ELEKTOR" joined with my MAKEPYTHON ESP32 DEVELOPMENT KIT
Code: Select all
#-----------------------------------------------------------------------------
# TEMPERATURE AND HUMIDITY ON THE CLOUD
# =====================================
#
# The temperature and humidity sensor DHT11 is connected to IO14
# of the development board. The project reads the temperature
# and humidity and sends to the cloud where it can be accessed
# from anywhere. In addition, change of the temperature and the
# humidity can be plotted in the cloud.
#
#
# The program uses the Thingspeak cloud service.MakePython ESP32
# must be connected to Wi-Fi router before data is sent to the cloud
#
# Author: Dogan Ibrahim
# File : Cloud.py
# Date : January, 2021
#------------------------------------------------------------------------------
import network
import socket
import time
import dht
from machine import Pin
APIKEY = "DHLZXHLPALHV12Q3" # my API Write Key !!!!!!
#
# This function connects to the Wi-Fi
#
def Connect_WiFi():
net = network.WLAN(network.STA_IF)
if not net.isconnected():
net.active(True)
net.connect(“My_SSID”, “My_PassWord”)
print(net.ifconfig())
#
# This function returns the temperature and humidity
#
def TempHun():
d = dht.DHT11(Pin(14)) # DHT11 at IO14
d.measure()
t = d.temperature()
h = d.humidity()
return t, h
#
# Send data to Thingspeak. This function sends the temperature and
# humidity data to the cloud every 30 seconds
#
while True:
Connect_WiFi()
sock = socket.socket()
[color=#FF0000][b] addr = socket.getaddrinfo("api.thingspeak.com",80)[0][-1][/b][/color]
sock.connect(addr)
(t, h) = TempHun()
host = "api.thingspeak.com"
path = "api_key="+APIKEY+"&field1="+str(t)+"&field2="+str(h)
sock.send(bytes("GET /update?%s HTTP/1.0\r\nHost: %s\r\n\r\n" %(path,host),"utf8"))
sock.close()
time.sleep(30)
the instruction seems to be wrong:
line 54 ===> addr = socket.getaddrinfo("api.thingspeak.com",80)[0][-1]
ERROR
Traceback (most recent call last):
File "<stdin>", line54, in <module>
OSError: -202
I have not found what is wrong !!!!
can you help me to understand this problem
Thanks very much
Best regards