Connection to THINGSPEAK , IOT, ESP32 ,Cloud,DHT11

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Connection to THINGSPEAK , IOT, ESP32 ,Cloud,DHT11

Post by ixbo » Thu Jun 02, 2022 7:11 pm

Hello.....
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)
well connected to my box, DHT11 work well.....but no more

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

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Connection to THINGSPEAK , IOT, ESP32 ,Cloud,DHT11

Post by davef » Thu Jun 02, 2022 7:23 pm

I tried Google:

Code: Select all

OSError: 202
and got lots of hits BUT

Code: Select all

OSError: -202
did not. Weird!

The - sign is specific to Micopython. I looked at a few of the Micropython specific hits, do any of those provide any clues?

Good luck.

ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Re: Connection to THINGSPEAK , IOT, ESP32 ,Cloud,DHT11

Post by ixbo » Thu Jun 02, 2022 10:28 pm

Thank you for the answer

Since several days I try to find a solution....
I try a sample given by Micropython.org they don't work
I'm connected to WiFi
But....................

import socket
addr = socket.getaddrinfo("api.thingspeak.com",80)[0][-1]
print(addr)
IP MASQ GATEWAY DNS
('192.168.1.33', '255.255.255.0', '192.168.1.254', '32.1.8.97')
Traceback (most recent call last):
File "<stdin>", line 61, in <module>
OSError: -202
>>>




Now I replace "api.thingspeak.com" by "3.223.163.200"
witch is IP address now it's good
import socket
addr = socket.getaddrinfo("3.223.163.200",80)
print(addr)
IP MASQ GATEWAY DNS
('192.168.1.33',
'255.255.255.0', '192.168.1.254', '32.1.8.97')
[(2, 1, 0, '3.223.163.200', ('3.223.163.200', 80))]


thanks very much !!!!
Best regards

Post Reply