publicsensors / IntroSensors

Source files for the textbook, Foundations of Environmental Sensing
Other
0 stars 2 forks source link

thonny IDE on ubuntu #13

Open seastate opened 3 years ago

seastate commented 3 years ago

I've now installed thonny on my machine and done a few basic tasks.

In short, connecting to an ESP8266 Feather, interacting via REPL, file transfer and editing files work very nicely.

It's very useful to be able to see and edit files directly on the microcontroller on the ESP8266, as you can on the pyboards and such!

Flashing does not at this point work.

As far as I can tell the issue is that, from the command line, esptool.py --port /dev/ttyUSB0 erase_flash esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ~/PublicSensors/Micropython/stockESP8266firmware/esp8266-20210902-v1.17.bin

(as suggested in the docs) works, but thonny is using different flags for esptool.py:

/usr/bin/python3 -u -m esptool --chip esp8266 --port /dev/ttyUSB0 write_flash 0x0 /home/dg/PublicSensors/Micropython/stockESP8266firmware/esp8266-20210902-v1.17.bin

I haven't found where I can change thonny's argument set yet.

It doesn't work even if I erase flash from the command line, so I think the problem is with thonny's flags.

Have you run into this kind of thing before?

thonny looks like a very useful platform to me. It will be even more useful if we can fix this bug.

jwlauer commented 3 years ago

No, I haven’t tried flashing an ESP8266 yet, but I can try tonight when I head home.

I’ve noticed that basically any MicroPython tutorial written after the beginning of 2021 refers the reader to Thonny rather than the older IDEs. I think it’s the way to go, especially if you can figure out the problem with the ESP8266.

By the way, as I’ve mentioned, Thonny is supposed to allow communication over a WebREPL connection. I have still not tried that. If I have time, I’ll give it a go tonight with an ESP8266.

jwlauer commented 3 years ago

There is one other big advantage to Thonny. In Thonny, the REPL allows you to paste in multiple lines of code without changing any settings. That can be VERY helpful for debugging.

seastate commented 3 years ago

Yes, it seems great.

I cloned the thonny repo from git.

It looks like a lot of this is happening in thonny/plugins/esp/init.py

There is a comment, though:

def get_instructions(self) -> Optional[str]:
    return (
        "This dialog allows installing or updating firmware on %s using the most common settings.\n"
        % self._chip.upper()
        + "If you need to set other options, then please use 'esptool' on the command line.\n\n"
        + "Note that there are many variants of MicroPython for ESP devices. If the firmware provided\n"
        + "at micropython.org/download doesn't work for your device, then there may exist better\n"
        + "alternatives -- look around in your device's documentation or at MicroPython forum."
    )
seastate commented 3 years ago

Also,

def work_in_thread(self, port, firmware_path, flash_mode, erase_flash, port_was_used_in_thonny): if port_was_used_in_thonny: time.sleep(1.5)

    erase_command = self._esptool_command + [
        "--port",
        port,
        "erase_flash",
    ]

    write_command = self._esptool_command + [
        "--port",
        port,
        "write_flash",
        "--flash_mode",
        flash_mode,
        "--flash_size",  # default changed in esptool 3.0
        "detect",
        self._start_address,
        firmware_path,
    ]
seastate commented 3 years ago

It'll be interesting to see how thonny's wifi interface works.

mpfshell opens repl via wifi directly, not through webrepl.

seastate commented 3 years ago

Another very useful feature is that thonny synchronizes the RTC. rshell does that, and it's been very useful for our kind of work.

seastate commented 3 years ago

In .config/Thonny/configuration.ini:

[ESP8266] port = /dev/ttyUSB0

[run] backend_name = ESP8266 program_arguments = dock_user_windows = False pgzero_mode = False auto_cd = True birdseye_port = 7777 run_in_terminal_python_repl = False run_in_terminal_keep_open = True

It's possible adding arguments to program_arguments will modify the esptool command. But seems more likely to mess things up.

jwlauer commented 3 years ago

This brings us back to the question about which board is best for the textbook. I’m inclined to think the best choice as of October, 2021 is an ESP32 in the Feather form factor (either the AdaFruit or Sparkfun versions). I bet either of these can be flashed using Thonny’s default settings. I know you can flash the Lolin D32 Pro using Thonny, so it may very well be that the Lolin D32 is a close second as far as board choice. However, I think the Feather form factor has sufficient buy-in that we should not move away from it.

As soon as MicroPython releases a bin file for the ESP32-S2, I think the Feather ESP32-S2 will be the best board, given its power management features and native USB (same as the PyBoard). I just got one this weekend, but I was not able to install MicroPython without building it. So, right now, the ESP32-S2 is only useful for a novice if they program in CircuitPython. That’s not a good choice for the textbook, so I still think the ESP32 is the best board for us to write about. If we use one with a Feather form factor, it would be easy to update the text for a new board (or maybe that wouldn’t even be required).

There is one other board-related point. The ESP8266 only has one ADC, so it can’t be used for the EC device, which requires three voltage measurements.

jwlauer commented 3 years ago

Agreed!

seastate commented 3 years ago

Is this ESP32-S2 firmware usable? https://www.wemos.cc/en/latest/s2/s2_mini.html?spm=a2g0o.detail.1000023.1.36da1596ZoDwKF https://www.wemos.cc/en/latest/tutorials/s2/get_started_with_micropython_s2.html

seastate commented 3 years ago

Also, a question about ADS1115 sample speed: You mentioned it's sampling rate was too slow, but the posts I saw suggested sample rates of e.g. 860 sps.

Is that not consistent with your experience, or do you need even faster rates? That's much more than needed for anything I've been doing.

The relevance here is that I still see the ADS1115 as a useful sensor for the situations like you describe, where more or more accurate ADCs are needed than available on a given microcontroller.

jwlauer commented 3 years ago

Is this ESP32-S2 firmware usable? https://www.wemos.cc/en/latest/s2/s2_mini.html?spm=a2g0o.detail.1000023.1.36da1596ZoDwKF https://www.wemos.cc/en/latest/tutorials/s2/get_started_with_micropython_s2.html

Not sure--I'll try it tonight. By the way, I ordered both of the Lolin ESP32-S2 boards last week. I don't expect them two arrive until early November.

jwlauer commented 3 years ago

Also, a question about ADS1115 sample speed: You mentioned it's sampling rate was too slow, but the posts I saw suggested sample rates of e.g. 860 sps.

Is that not consistent with your experience, or do you need even faster rates? That's much more than needed for anything I've been doing.

The relevance here is that I still see the ADS1115 as a useful sensor for the situations like you describe, where more or more accurate ADCs are needed than available on a given microcontroller.

It's not necessarily just a question of sampling rate. Four-pole EC circuits are supposed to run on AC in order to prevent polarization. The EC circuit "fakes" an AC signal by simply quickly switching the state of the two GPIO Pins on either electrode, then taking ADC readings, then switching polarity again. This is repeated many times. I thought about trying it with the ADS1115 but haven't done it yet. I think the read time would be so large that I wouldn't be even close to switching polarity at the recommended rate. This might not cause too much trouble, I just don't know.

I do agree that the ADS1115 would be nice to document. In fact, it's kind of necessary if the ESP32 is used as the base microcontroller, given the noise and non-linearity in the ESP32's ADC. That's one reason I went to the PyBoard for some of my class exercises--its ADC is much better than that on the ESP32.

I'm hoping the ESP32-S2 ADC is better than that on the ESP32, but I'm not super optimistic.

seastate commented 3 years ago

Inconvenient!

But sounds like a great discussion section about microcontroller and sensor choices in a textbook!