Hello,
I need your help.
I would like to send a notification including an image to Pushover: (https://pushover.net/) with my arduino Nicla Vision.
I tested this code on my PC with Python and it works without image.
import requests
data = { "token": "my token", "user": "my user","message": " hello world",}
requests.post("https://api.pushover.net/1/messages.json", json=data)
I copy the same in he OpenMV IDE and unfortunatly it doesn’ work on MicroPyhon. No notification sended…
Do you have an idea of what I have to check.
In advance, many thanks
Thierry
Hi, what error messages do you get in the IDE about the code? You have to first connect the Nicla to the network and then execute this. The sample code you posted can’t be your whole program. You should receive a response code from the call indicating if there’s an error or not.
Hi,
No error messages from the IDE but no notification from Pushover.
Naturally I’m connected in my wlan network before he requests.post.
I’m a little confuse with using requests or urequests too?
What do you thing ?
In our latest firmware the library is called requests. Need to modify the documentation. The u-part was dropped.
The code of the library is here: openmv/scripts/libraries/requests.py at master · openmv/openmv
You can directly past it into your code in the IDE, call it, and see what things are returning. Typically there’s a pretty clear error code that will be returned from the functions.
Additionally, if you want to observe traffic on the network you can install wireshark which will allow you to see packets being sent.
I directly past (openmv/scripts/libraries/requests.py) into my code in the IDE, call
the function “post” instead of “requests”.
Is that what you recommend ?
That look like that :
data = { “token”: “my token”, “user”: “my user”, “message”: " hello world",}
r = post(“https://api.pushover.net/1/messages.json”, json=data)
print(r)
I don’t know if that correspond at the function corps ?
def request(method, url, data=None, json=None, files=None, headers={}, auth=None, stream=None)
In the terminal :
<Response object at 2000efa0>
No result from PushOver notification.
You need to print out the results of the response object. It’s a class: openmv/scripts/libraries/requests.py at master · openmv/openmv
I’m not a great programmer, sorry. Please could you give me an example how to use and implement this class Response in my code ?
Hi, please take advantage of ChatGPT for generic Python programming questions. It can provide help support far faster than I can for these basic questions.
Instead of:
print(r)
Do:
print(r.headers)
print(r.content)
Print(r.json)
Hello, you are right. Thanks to Chat GPT !
Here the code working to send notification to PushOver 
import network
import usocket
import ujson
import ssl
import time
# Configuration Wi-Fi
SSID = "Network SSID" # Network SSID
PASSWORD = "Network key" # Network key
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
pass
print("Connecté au Wi-Fi:", wlan.ifconfig())
# Fonction pour envoyer une notification à Pushover
def send_pushover_notification(user_key, app_token, message, title="Notification"):
url = "https://api.pushover.net/1/messages.json"
headers = {
"Content-Type": "application/json"
}
payload = {
"token": app_token,
"user": user_key,
"message": message,
"title": title
}
# Conversion des données en JSON
json_data = ujson.dumps(payload)
# Création de la requête HTTP POST
_, _, host, path = url.split("/", 3)
addr = usocket.getaddrinfo(host, 443)[0][-1]
s = usocket.socket()
s.connect(addr)
s = ssl.wrap_socket(s)
# Construction de la requête HTTP
request = "POST /{} HTTP/1.1\r\n".format(path)
request += "Host: {}\r\n".format(host)
request += "Content-Type: application/json\r\n"
request += "Content-Length: {}\r\n".format(len(json_data))
request += "Connection: close\r\n\r\n"
request += json_data
# Envoi de la requête
s.write(request.encode())
# Lecture de la réponse
response = s.read()
print("Réponse de Pushover:", response)
# Fermeture du socket
s.close()
# Informations Pushover
USER_KEY = "PushOverKey"
APP_TOKEN = "PushOverToken"
# Connexion au Wi-Fi et envoi de la notification
connect_wifi()
send_pushover_notification(USER_KEY, APP_TOKEN, "Hello depuis MicroPython !", title="Alerte OpenMV")