For an index to all my stories click this text
This story tells how to use a free (no sign-in) API to connect to an AI system with MicroPython. The program is tested both on a Raspberry Pi Pico W and Pico 2W. But it should run on an ESP8266 or ESP32 with MicroPython equally well.
Like I said in previous stories on this weblog: I love playing with AI.
In one of the previous stories I wrote how to install a LLama (a complete AI system) on a Raspberry Pi. You can read that story here:
https://lucstechblog.blogspot.com/2026/01/run-ai-models-local.html
That works great but the Llama's (Language models) are limited because the memory of the Raspberry Pi is limited. The AI systems we access through our browser like ChatGPT, Copilot, Gemini, Grok etc. are multi million dollar systems with hundreds of Gigabytes (GB) memory.
And then I found a very simple API that allowed connecting to one of the cloud based large models.
The APi is so simple that I could use it with MicroPython and with Javascript.
This story tells how to use it with MicroPython. The next story shows how to use it with Javascript.
The API
The API call looks like this:
http://nimaartman.ir/science/L1.php?text=:HERECOMESTHEQUESTION
If you want to try it (and fill in some real question) you can just put it in your browsers URL and press enter.
As you can see, after "text=:" comes the actual question. But there are no spaces allowed in the question. So our program needs to filter them out.
This works in your browser however the answer looks like this:
data "🤔💠What’s your question? 🚀✨"
It is JSON coded with emoticons and other characters.
I want to filter these out, so our program needs to take care of that too.
So here is the complete program in MicroPython.
''' program to get data from an ai system ''' import network import urequests import ujson # Router credentials ssid = "YOUR-ROUTERS-NAME" pw = "Routers-PAssword" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass # wifi connected print("Connected. IP: ",str(wifi.ifconfig()[0], "\n")) question = "Translate 'twee' from dutch to german" question = question.replace(' ','') url = "http://nimaartman.ir/science/L1.php?text=" url = url + question # Send the GET request response = urequests.get(url) try: # Attempt to parse the JSON response response_str = response.text data = ujson.loads(response_str) # Extract the text field from the JSON data text_with_unicode = data.get("data") ## Clean the unicode text cleaned = ''.join(ch for ch in text_with_unicode if ord(ch) < 128) print(cleaned) response.close except ValueError: # Handle the case where the response is not valid JSON print("Error: The response is not valid JSON or the server returned an error message.") print("Raw response2:", response.text) response.close
Let us have a look at some parts of the code.
The program starts with iporting the necessary libraries (that are included in the MicroPython instalation).
ssid = "YOUR-ROUTERS-NAME" pw = "Routers-PAssword"
Don't forget to replace YOUR-ROUTERS-NAME and Routers-Paswword with the credentials of your router.
The program then connects to the internet and shows the IP Number the microcontroller got from the router.
question = "Translate 'twee' from dutch to german" question = question.replace(' ','')
The question I used as a first test is to translate 2 (two) into German.
The next line replaces all spaces with an empty string and so effectively removes the spaces. This is needed for the API to work as discussed earlier.
url = "http://nimaartman.ir/science/L1.php?text=" url = url + question
This is where the API call is constructed.
# Send the GET request response = urequests.get(url)
The urequests library sends the API call and gets an answer which is stored in the variable response
# Attempt to parse the JSON response response_str = response.text data = ujson.loads(response_str) # Extract the text field from the JSON data text_with_unicode = data.get("data")
The first part decodes the JSON code in the response variable and puts that in the data variable.
The second part gets the answers actual text. It is put in the text_with_unicode variable as it still contains all the unicode characters like emoticons etc.
cleaned = ''.join(ch for ch in text_with_unicode if ord(ch) < 128) print(cleaned)
Now this looks coplicated but in fact really isn't.
We start with and empty string. In that we put all the characters (ch) if their ascii value is less then 128 (ord(ch) < 128)
And then we print the cleaned text.
Copy the code from this page and paste it in Thonny. Save it with an appropriate name and run the code.
Some examples.
Here is what I received when I asked to translate the Dutch word "twee" into German
And here I asked to translate "hello" into Japanese.
And here I asked what the book with the title "This perfect day" is about.
The description is brief but correct while not giving away the plot !!!
Small side-note from me: it is about a society that is run by a supercomputer that makes all decisions for humans.
Written by Ira Levin and highly recommended !!
I guess these are enough examples.
If you have a developed a nice project with this: let me know.
Till next time
Have fun
Luc Volders