Need to migrate project--Please suggest

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Need to migrate project--Please suggest

Post by bukaida » Sat Jul 11, 2020 9:43 am

Kindly help in this regards --
My current project is running with the following architecture--
1. 3 axis accelerometer+3 axis gyroscope data coming from smartphone via WiFi using APK (Client).
2. A python3 server is running on laptop command prompt which is receiving the data via UDP and storing it in in a file as well as writing it in the command prompt.

I am still in the "hello world" phase in micro-python (Although have some basic knowledge of python, but the treatment and modules are entirely different here).

I am running WebRepl and connected with my ESP8266 (LOLIN) via my home router. I also have a MPU6050 module that is attached with my ESP and ran successfully in Arduino sketch.

I like to do the following --
1. Substitute the smartphone with ESP8266+MPU6050 to receive 3 axis accelerometer and gyroscope data to python server on laptop via WiFi.
2. Use al python 3 server code to run it as independent server on command prompt.

This is the current server code in python

Code: Select all

import sys
from flask import Flask
from flask_sockets import Sockets


app = Flask(__name__)
sockets = Sockets(app)

@sockets.route('/accelerometer')
def echo_socket(ws):
	f=open("acc.json","w")
	while True:
		message = ws.receive()
		print(message)
		ws.send(message)
		print>>f,message
	f.close()


@sockets.route('/gyroscope')
def echo_socket(ws):
	f=open("gyroscope.txt","a")
	while True:
		message = ws.receive()
		print(message)
		ws.send(message)
		print>>f,message
	f.close()



@app.route('/')
def hello():
	return 'Hello World!'

if __name__ == "__main__":
	from gevent import pywsgi
	from geventwebsocket.handler import WebSocketHandler
	server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, handler_class=WebSocketHandler)
	server.serve_forever()
I have found this project in Github https://github.com/larsks/py-mpu6050
However when I uploaded entire thing into ESP8266 module via WebRepl and restart, the module crashed and I ended up reloading the Firmware again [ Guess the effect of driving a car without learning how to drive :( ]

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Need to migrate project--Please suggest

Post by kevinkk525 » Sat Jul 11, 2020 3:41 pm

Can't say anything about the accelerometer but for sending messages between micropython and a python server I can recommend this project: https://github.com/peterhinch/micropython-iot

It provides a reliable client-server connection, so you don't have to worry about that. However, it uses uasyncio so you would definitely have to learn about that: https://github.com/peterhinch/micropython-async
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need to migrate project--Please suggest

Post by bukaida » Sat Jul 11, 2020 5:17 pm

I was trying to learn (I am always ready to learn) from your link but it seems that the codes are written for pyboard not ESP8266. At present I do not have enough knowledge to make it run on ESP by making necessary change.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Need to migrate project--Please suggest

Post by kevinkk525 » Sat Jul 11, 2020 6:03 pm

both links are for pyboard, esp8266 and esp32 and partly even the unix port.
You dont need to do any changes.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need to migrate project--Please suggest

Post by pythoncoder » Mon Jul 13, 2020 5:04 am

The library Kevin suggested has been extensively tested on ESP8266. If you look at the client demo you will see it is cross-platform. The graphic in the readme shows ESP8266 clients and there are details of how to install on that platform.

As for MPU6050 I suggest you go here. This library is widely used and I might be able to help with any problems.
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need to migrate project--Please suggest

Post by bukaida » Tue Jul 14, 2020 11:46 am

Yes, I have read the documentation, flushed the firmware with 12/07/2020 daily build and installed the files via rshell ( although client.mpy needed to be copied by web-repl) and some more folders individually, but managed to transfer the same ultimately. Then I tried to run the command in webrepl( Which was connected with my ESP via wifi router, thanks to the step by step nice documentation) by issuing:

Code: Select all

import iot.examples.c_app
Output client
http://imgur.com/a/xLfSA35
This detected and said WiFi OK and was waiting for the server response.
Then I moved to my laptop and went to the C:\Micropython\micropython-iot where I cloned the project. I thought this part will be easy as I have nothing to change in the code. So on the command prompt I issued :

Code: Select all

python -m iot.examples.s_app_cp 
However to my surprise the server program threw an error and terminated.
The error was

Code: Select all

 File "C:\Micropython\micropython-iot\iot\server.py", line 79, in run
    poller = select.poll()
AttributeError: module 'select' has no attribute 'poll'
With my limited knowledge, i could not detect what went wrong.
My laptop IP is 192.168.1.15 which I put in both local.py
ESP ip is 192.168.1.14 which I did not put anywhere as client ip should not be important for client-server program.
Last edited by bukaida on Tue Jul 14, 2020 5:38 pm, edited 1 time in total.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need to migrate project--Please suggest

Post by pythoncoder » Tue Jul 14, 2020 3:38 pm

What version of CPython are you running? As stated in the docs you need CPython V3.8+. On many distros issuing python brings up V2.x. You can also run the server side programs under the Unix build of MicroPython, but you do need to take steps to compile in uasyncio V3 (also in the docs).
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need to migrate project--Please suggest

Post by bukaida » Tue Jul 14, 2020 4:17 pm

I was trying to run it on python 3.7 , I am on windows 10.

Code: Select all

pip install cpython 
showed something like

Code: Select all

C:\Micropython\micropython-iot>pip install cpython
Requirement already satisfied: cpython in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (0.0.5)
Requirement already satisfied: pymongo in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from cpython) (3.10.1)
Requirement already satisfied: requests in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from cpython) (2.22.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from requests->cpython) (2019.3.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from requests->cpython) (1.25.3)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from requests->cpython) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\bukai\appdata\local\programs\python\python37\lib\site-packages (from requests->cpython) (3.0.4)
but still I am getting these errors.

Code: Select all

C:\Micropython\micropython-iot>python -m iot.examples.s_app_cp
Awaiting connection. 8123
Client 2 Awaiting connection.
Client 1 Awaiting connection.
Client 3 Awaiting connection.
Client 4 Awaiting connection.
Closing sockets
Traceback (most recent call last):
  File "C:\Users\bukai\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\bukai\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Micropython\micropython-iot\iot\examples\s_app_cp.py", line 79, in <module>
    run()
  File "C:\Micropython\micropython-iot\iot\examples\s_app_cp.py", line 69, in run
    asyncio.run(main())
  File "C:\Users\bukai\AppData\Local\Programs\Python\Python37\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\Users\bukai\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 568, in run_until_complete
    return future.result()
  File "C:\Micropython\micropython-iot\iot\examples\s_app_cp.py", line 65, in main
    await server.run(clients, True, port=PORT, timeout=TIMEOUT)
  File "C:\Micropython\micropython-iot\iot\server.py", line 79, in run
    poller = select.poll()
AttributeError: module 'select' has no attribute 'poll'
The client output http://imgur.com/a/xLfSA35

As I am really noob in this domain, please help.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Need to migrate project--Please suggest

Post by pythoncoder » Wed Jul 15, 2020 5:08 am

I don't know how to explain this any simpler. You need Python 3.8. A Windows installer can be downloaded here.

You then need to be sure it is running that version. Unfortunately I have no Windows machines or recent experience, but under Linux issuing python at the command line brings up Python 2.7, even though I have Python 3 installed. I have to issue python3 to run the correct version. Maybe under Windows you have to do something else: look at the docs. Type whatever they suggest at the command line and check the version number that's displayed. Then use the same invocation to run the code.

Or, go to the correct directory, run Python 3.8. Check the Python 3.8 command prompt, and issue

Code: Select all

>>> import iot.examples.s_app_cp
Peter Hinch
Index to my micropython libraries.

bukaida
Posts: 19
Joined: Tue Jul 07, 2020 6:30 pm

Re: Need to migrate project--Please suggest

Post by bukaida » Wed Jul 15, 2020 7:45 am

I already have python 3.8

Code: Select all

C:\Micropython\micropython-iot>python
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import iot.examples.s_app_cp
>>>
Nothing happens.

Post Reply