Robo's esp'ecial ESP8266 fork

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Robo's esp'ecial ESP8266 fork

Post by mianos » Sun Nov 01, 2015 9:07 am

Just got the I2C running. It is 100% dynamic in terms of pin usage but uses I2C.sda_pin=2, I2C.scl_pin=14l. (That is what is marked on the NoceMCU boards). If you have another GPIO pin used I don't see why it would not work by simply defining them them in the esp.I2C constructor:
esp.I2C(sda_pin, scl_pin)
I found a Ds3231 clock board in my box so I have tested it using that. I also ordered an I2C light meter to give a try.

Code: Select all

class Ds3231:
    DS3231_DEVICE_ADDR = 0x68
    DS3231_ADDR_TIME = 0x00

    def __init__(self, i2c):
        self.i2c = i2c

    @staticmethod
    def http_time_to_ds_array(aa):
        md = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
        wdays = {'Sun': 1, 'Mon': 2, 'Tue': 3, 'Wed': 4, 'Thu': 5, 'Fri': 6, 'Sat': 7}
        bb = re.match('(.*), ([0-9]+) (...) ([0-9]+) ([0-2][0-9]):([0-5][0-9]):([0-5][0-9])', aa)
        data = [int(bb.group(7)),   # Second
                int(bb.group(6)),  # Minute
                int(bb.group(5)),  # Hour
                wdays[bb.group(1)],  # Weekday number
                int(bb.group(2)),  # Day
                md[bb.group(3)],   # Month
                int(bb.group(4)) - 1900]  # YY
        return data

    @staticmethod
    def decToBcd(dec):
        return ((dec // 10) * 16) + (dec % 10)

    @staticmethod
    def bcdToDec(bcd):
        return ((bcd // 16) * 10) + (bcd % 16)

    @staticmethod
    def format_da(aa):
        return "%02d/%02d/%02d %02d:%02d:%02d" % (aa[6] + 1900, aa[5], aa[4], aa[2], aa[1], aa[0])

    def set_date(self, date):
        print("date '%s'" % date)
        st = [self.DS3231_ADDR_TIME] + list(map(self.decToBcd, self.http_time_to_ds_array(date)))
        self.i2c.write(st, self.DS3231_DEVICE_ADDR)

    def date(self):
        i2c.write([self.DS3231_ADDR_TIME], self.DS3231_DEVICE_ADDR)
        return self.format_da(list(map(self.bcdToDec, i2c.read(7, self.DS3231_DEVICE_ADDR))))

i2c = esp.I2C()
ds = Ds3231(i2c)
To make a serial clock:

Code: Select all

def pp(timer):
    print("\033[2J")
    print(ds.date())
and call it in an esp task timer.

Code: Select all

esp.os_timer(pp, period=500)
The set_date method expects a date format from a web request. I am setting the clock using a simple connect to http://www.google.com
and getting the date from the header. No fancy NTP for me. The whole example is here:
https://raw.githubusercontent.com/miano ... /ds3221.py

I am pulling master into my branch once every few days.
What I want to do next is load modules from a remote web site. I am just deciding if I load them into 'irom0' like my own esp_frozen system or try and parse them as the callback brings in blocks of data.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork 1 wire

Post by mianos » Sun Nov 22, 2015 7:47 am

I just wrote a 1 wire driver. As usual I have a HAL in esp_1wire.c and esp_1wire.h and a micropython interface in
mod_esp_1wire.c mod_esp_1wire.h
It is only supporting one device per pin so far.
The API is written to support multiple devices but I have not tried a discovery process yet.

Code: Select all

aa = esp.one_wire(5)  # This is the GPIO pin (the pin, I used my table)
aa.reset()
aa.write([0x33], suppress_skip=True) # 33 means send id, suppress_skip means don't send a suppress skip rom command 
cc = aa.read(8)
calc_crc(cc[:-1]) == cc[-1]
(40, 90, 2, 81, 7, 0)

aa.reset()
aa.write([0x44])

aa.reset()
aa.write([0xbe])
dd = aa.read(9)
calc_crc(dd[:-1]) == dd[-1]

convert(*dd[:2])

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Robo's esp'ecial ESP8266 fork

Post by Turbinenreiter » Sun Nov 22, 2015 10:27 am

Why don't you open a pull request to get it merged?

/edit: Nevermind, just saw it.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork

Post by mianos » Sun Nov 22, 2015 6:21 pm

I have a fork.
https://github.com/mianos/micropython
I pull master into it once a week.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork - updated

Post by mianos » Mon Dec 21, 2015 9:41 pm

I have just got smartconfig running. This allows you do configure the access point using a simple app available from the android and apple store. This way you don't need to hard code any access point names or passwords. I have it running in C so now I have to save the values in irom and make a python wrapper over the interface.

I have now added a queue that uses pre-allocated lists in the constructor. This way I can add items to the list with safety in pure interrupt handlers and get the values back out in python. It works really well for my interrupt driven DHT22 driver. It also makes a handy queue to use if you don't want to worry about memory as the queue is statically allocated (in python).

This box, has a switch, light, and a relay on board. The the serial port is run to the edge ready for headers. The switch is is on GPIO0 so all you have to do is solder some headers and hold the button down to reflash it. It runs my micropython perfectly.
http://www.banggood.com/DIY-Wi-Fi-Wirel ... 19971.html
The board design is quite acceptable for something out of China. The creepage protection is very wide and milled out where needed. It is way way better than your average power pack!

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Robo's esp'ecial ESP8266 fork

Post by Turbinenreiter » Tue Dec 22, 2015 11:38 am

I hope I can find some time over the holidays to study your port.
I'd like to change the API to follow micropythons. I don't see a good reason to have a micropython fork for a certain platform with an incompatible API. Especially when it's just renaming and moving code around.
Maybe I can also start on getting it merged part by part.

Time, man. I need more of that.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork

Post by mianos » Tue Dec 22, 2015 8:37 pm

I am totally for a merge of the port. Most of the drivers are written for scratch but they could be re-arranged to look like the existing code. All my drivers are all C with a HAL and the micropython interface is in a separate module so you could also add a 'compatibility' module that limits the functionality to that of the stock micropython driver where they already exist but are limited. Most of the newer stuff I have is not in micropython but some would be sensible to add. I am afraid the easyconfig is just using the library that comes with the ESP SDK :(

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork debounced queued buttons

Post by mianos » Fri Dec 25, 2015 10:01 am

So I can press that button to put the ESP into smartconfig mode I needed interrupt driven buttons.
How this works is; I attach one of my native interrupt safe circular queues and an os_task on that queue to a GPIO pin.
When someone presses the button, a 1 is send down the queue, when they release it, a 0 is sent.
The os_task receives the queue insertion os_task event and, when it runs, it can receive the button events.
(At this time I can time the buttons to decide if it is a long press and put the card into smartconfig).
Obviously the buttons need to be debounced so I have a simple debounce state machine whose time can
be configured when it is a attached.

https://github.com/mianos/micropython/b ... pts/tm3.py

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork - smartconfig

Post by mianos » Mon Dec 28, 2015 12:22 am

Smartconfig is now integrated.

Now all I need to do is hook up my new single button clickety click UI to this smartconfig class
https://github.com/mianos/micropython/c ... 41dd0e9ead

and I'll be able to double click, then confirm with a second double click, to use the android/smartphone app to remotely configure the access point credentials.

Then I will be able to use these to control power points out of the box:
http://www.banggood.com/DIY-Wi-Fi-Wirel ... 19971.html

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: Robo's esp'ecial ESP8266 fork - wifi events

Post by mianos » Thu Dec 31, 2015 1:42 am

To achieve 100% reliability I have integrated the ESP wifi events callbacks.
Now, I can connect to to an access point and be informed when the board receives a DHCP address.
Once I receive this I can proceed to get temperatures etc., and send them to the server.

https://github.com/mianos/micropython/b ... _events.py

The only issue, is, the ESP does not inform me if I do a network.connect and it is already connected. I am experimenting with this, but at the moment I think the best thing to do might be to de-associate when put the board to sleep.

Post Reply