how to send image to server by post to http?

Good idea. I use the camera with a PIR sensor to be fully switched off to last for long time and when it is triggered it takes pictures, connect to the Wifi AP in a Raspberry PI 4 with Neural Nets to send the image for classification.
I was also considering also using the mpej_streamer.py example that works really fine and pick the pictures from the RPi. But it needs to run continuously in the other side (Rpi4) as the nc but seems to be a good solution. We might use php but better wget inside bash.Thanks

Alright I will be checking. As I commented to CammeraMan, I will try with the example “mpeg_streamer.py” and run in the other side a wget or a php to read the images.

My library will work fine for you then. You can choose the following interfaces:

WiFi - 1 to 2 Mbs in heavy wifi traffic area (using our WiFi shield)
UART - 7.5 Mbps
CAN - 1 Mbps
SPI - 20 to 80 Mbps
I2C - 1 Mbs
USB VCP - 12 Mbps

The library is self healing meaning that I designed it to be reliable if a part of the link dies. E.g. the library will timeout gracefully and then start working again once the link comes back - no lockups. We won’t have an example script for the RaspberryPi right away… but, the general purpose PC control script should run on the Pi as it’s just for sockets and USB. SPI/I2C/UART for the Pi will come later.

Um, so, to be honest, the lowest power thing to use for you would actually be the OpenMV Cam RPC via UART using a generic serial to RF adapter: Amazon.com - APC220 Radio Communication Module-DFRobot

These are much lower power than WiFi. You would give each camera one of them and then have my RPC code running on each camera. Then the RPi would use the RPC protocol over USB through the FTDI serial port. The range will be longer than WiFi too. However, I’m not sure how you handle multiple channels at once. Probably should research that.

Note: You might want to use better hardware than I linked to above. But, the idea is the same.

If I use a modem instead of a WiFi shield to upload images, do you suggest using POST to http or ftp, or something else? Given that you made the RPC tool, what is the easiest way to write something to get the image uploaded? I think you suggest using sockets but what are the basic steps between snapping an image and sending it? I presume I should use AT commands to open a socket, etc on the modem or is there a better way? How do I access the image to begin sending it in the first place?

The tool I built works on the socket level layer. It’s not FTP/HTTP/POST/etc.

If you want to use a higher level protocol you need to implement that on top of the sockets layer.

With the latest firmware various bugs with the WiFi shield have been fixed. So, you should find it very straight forward to use sockets to implement the above high level protocols. Standard Python code on how to use sockets to do the above protocols can be used.

Anyway, a thing to think about for your problem. So, If you just use the RPC library I made and put that on the remote server it’s possible to get streaming working to the remote server. This is the easiest way. However, keep in mind that the RPC timeouts aren’t very long so you might have to modify the library code.

Otherwise, if you want to implement a higher level protocol you just need to use python sockets to do it.

Not all the above is probably something you can do. But, most of it.

Anyway, as for sending image data… you should jpeg compress it and then append that to the end of the message.

We’ll add this to our firmware:

https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py

Yeah, we’ll add urequests and mqtt. Mqtt is already onboard. But, will document it with this:

https://makeblock-micropython-api.readthedocs.io/en/latest/public_library/Third-party-libraries/urequests.html
https://makeblock-micropython-api.readthedocs.io/en/latest/public_library/Third-party-libraries/mqtt.html

Actually, we can include this:

Do you still think the Wipy url-library like urequests useful for OpenMV?

Yes, as the socket interface is the same. You just have to fix any references to the WLAN interface.

Thanks, I searched for wlan in both urllib and urequests files, could not find anything, would you provide more specific directions?

However, I copied the files and imported them to OpenMV, and tried to run their code provided on their readme file;

import urllib
f = urllib.urlopen(‘http://www.micropython.org’)
f.code
f.headers
f.read()

Error: OSError: [Errno 107] ENOTCONN
Which is on line 24 of urllib.py which is socket.settimeout(5)

Btw I was able to connect to Wi-Fi board with your ntp example and get Hawaii time from ntpserver, so Wi-Fi shield is fine.

Let me share with you where our team is; We have a sample working request library in PC Python that you can test yourself but we wanna do the same in OpenMV.

Python code:
#you can run on your PC to upload an image with requests library:

import requests #task: make a post request to a webpage, and return the content of the response
url = ‘http://www.yourwebsite.com/upload.php/#url of the webpage
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {‘file’: open(‘Sample_Picture.png’, ‘rb’)} #open Picture from current directory to post
r = requests.post(url, files=file, headers=headers) #post request of file to url
print(r.status_code) #status code 200 means success
c = r.content #content of the response in bytes
print(c) #prints units

PhP code:
#you can make this PhP code on your test or local server and create uploads folder to receive the image sent from your PC to the server:

<?php echo "server is up!"; #fixes undefined array key warnings so they don't clutter the local server error_reporting(0); #$return = $lookup_table[$key]; #error_reporting(E_ALL); #return $return; if (isset($_POST)) { $file = $_FILES['file']; $file_name = $_FILES['file']['name']; #folder to upload files to webpage $uploaded_folder = "uploads/".$file_name; #move file $movefile = move_uploaded_file($_FILES["file"]["tmp_name"],$uploaded_folder); if($movefile) { echo "File Uploaded!"; } } #$fileExt = explode(',', $file_name); #takes file extension from file_name #$fileActualExt = strtolower(end($fileExt)); #takes file ext and turns to lower case #$allowed = array('jpg', 'jpeg', 'png'); #accepts only files with jpg, jpeg, and png ext ?>

Thank you,
Hope we can have urequests for OpenMV soon!
I am learning Python, I wish I could help!

Sorry this feature is not where you need it to be. I am swamped with other work to make this a priority right now. Need community support.

1 Like

Hi Kwagyeman,

Congrats on the WiFi debugging and pure thermal tasks!
Do you have plans to add the urequests from micropython to OpenMV?

I’ve finished the classification task using TFLite with OpenMV, which was fun, but I am still stuck relaying the images with WiFi.
I hope you add urequests in OpenMV soon!
Or if you have a tutorial on adding a module like urequests to OpenMV, please let me know.

Thanks,
MoZen

I tried using the urllib.urequest:

I added this to OpenMV USB Drive but it is not recognized inside IDE, any comments how to add requests to OpenMV?

Thanks,
MoZen

What was your import path? Also, did you ensure that the disk was flushed?

Dear Kwabena,

  1. I did not touch the import path, I just copied the files (setup.py and urequest.py) from the micropython link I shared previously to the OpenMV main USB flash directory. In setup.py it has import sys, sys.pass.pop(0), should I change it?
    It is like this in setup.py:

import sys
“Remove current dir from sys.path, otherwise setuptools will peek up our
module instead of system’s.”
sys.path.pop(0)
from setuptools import setup
sys.path.append(“…”)
import sdist_upip

I put setup.py near main.py. Also, I copied urllib folder there too, as is in the following zip file;
urllib.urequest.zip (2.0 KB)

You shared this link for importing modules in another thread;
https://wiki.micropython.org/Importing-Modules
It says On the pyboard, 0:/ refers to the internal flash, and 1:/ refers to the sdcard.
So is it similar in OpenMV? How should I configure sys.path in OpenMV?

  1. I flushed the disk by erasing the internal file system using firmware update in IDE then I added setup.py and urequest.py. But in IDE, the color of urequest is white as I import it, so I guess OpenMV does not recognize it as a library.

Thanks,
MoZen

Hi, you just want to copy the urequest.py file to your disk. The rest of the files have no use.

Then you just import urequest in your script.

The rest of the files are for dist management for linux micropython versions.

Thanks it does not give missing library error now, but still I can’s send the image.png to my website;

In Python in Spyder the following code works (and uploads image.png) with regular request library;
import requests
url = ‘http://www.mywebsite.com/upload.php/
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {‘file’: open(‘image.png’, ‘rb’)}
r = requests.post(url, files=file, headers=headers)
print(r.status_code) #status code 200 means success

Now in OpenMV IDE, I get attribute error for similar code;

import urequest #task: make a post request to a webpage, and return the content of the response
url = ‘http://www.mywebsite.com/upload.php/#url of the webpage
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {‘file’: open(‘image.png’, ‘rb’)} #open file to post
r = urequest.post(url, files=file, headers=headers)
print(r.status_code) #status code 200 means success

ERROR: AttributeError: ‘module’ object has no attribute ‘post’

However, looking at ureuest.py in line 4 it has Post as an attribute I guess;

def urlopen(url, data=None, method=“GET”):
if data is not None and method == “GET”:
method = “POST”
How should I modify my code in OpenMV so that it uploads the file.

Btw, I can send you my website address in person, and it has a gallery that you can check if picture is uploaded, if you like…

Thank you for your helps!

There’s no post method. Just a urlopen.

Use this library: GitHub - lucien2k/wipy-urllib - You want the urequest.py file. It has the post method.

Now, I don’t know if files/headers are supported. I would suggest using a different way of posting that doesn’t require the multi-part http header. If so, then you will need to construct that data payload as these libraries don’t do it for you.

Yes, lack of a good urequest package for MicroPython is a weakness. I want to work on this but I have so much else to do…

Please note that the code in the library is standard python. The error you saw above was quite straight forward.

Oh, now I read your full response, Ok I will try to upload it with other methods.