uLibrary for the ESP8366 (ESP32)

netx

netx.py       version 1.1       19 July 2018

Description

This provides an automatic connection to an Access Point (router). It uses a simple text file with the Access Point (AP) details. It can use DHCP or provide a fixed IP address. The default filename for the AP details is called 'mynet.txt' and looks like this:

[('myssid1','mypass1','192.168.11.85'),
('myssid2','mypass2','192.168.11.86'),
('myssid3','mypass3','')]

This uses a choice of AP's that may be available, the one with the strongest signal is chosen. If the IP is left blank then the AP will use DHCP to allocate an IP otherwise a fixed IP will be used.

How to Use

import netx
n = netx.Netx()
n.join()

Methods

join() This uses the text file to find the best AP and connects to that AP,either using DHCP or fixed IP

ip() Returns the current IP details

ip('192.168.11.22') Sets the IP address

mac() Returns the MAC addrress

UDP_COM

Description
Communication between two networked devices using UDP. This is connectionless and so there is no overhead as there is with TCP. The design was for communication between an ESP8266 and a Raspberry Pi but should work between any two, the RPi udp is slightly different.

The idea is for a receive, response function to be written as a function that the udp_com will call back, see the example.

Example

import udp_com
def ccmd(data):
    if data == 'hello':
        u.send('you sent hello')

u = udp_com.Udpcom(callback = ccmd)

In the above a function is written to capture UDP messages from the remote and respond if the message is the correct one, this of course could be commands to tell the ESP to do something or get some value from a sensor.

Before use, either u.search() must be used or u.set_remote((ip,port)). A search from any side, here or the remote will establish enough information for two way communication.

Use

import udp_com
u = usp_com.Udpcom(port=nnn,devid='string',callback=function)

port: any number will do above 1025 and must be known to the remote UDP device, the ip can be searched for using search()

devid: This is returned when a request is made to the remote with the string 'alive'. There is a default function that will send this back. e.g. u.send('alive')

callback: is a function that can be provided to receive and respond to messages, this saves having to alter the udp_com file directly, see the example above.

Properties
u.remoteaddr is the ip and port as a tuple of the remote udp device, this can be set by using the search() function or manually set with set_remote()

Methods
u.search() This will search all ip addresses for a UDP device listening on the given port. It will then fill the u.remoteaddr property so communication can take place
** Takes a long (minutes) time on the ESP but very quick on the rpi. **
NOTE that a search from any direction, here or the remote, will establish enough data for two way communication. Normally search will be needed if one of the devices has had its IP set by DHCP

u.set_remote((ip,port))  Sets u.remoteaddr manually if it is known in advance so search will not be needed

LED Flasher

Description

Simple led flasher class, can vary the flash time (saves re-inventing the wheel)

Use

import led
greenled = led.Led(4)
builtinled = led.Led(2) # this is the led on the ESP board

greenled.speed(5)

This't it. speed(0) turns the led off and speed(100) turns it on, anything between flashes at varying rates

I2C

iic_u.py  20 Jan 2018 This is written on the ESP8266 running micropython

Universal access to i2c functions via one function, this makes it easier to port i2c devices access platforms as all i2c goes through this function it is just a question of re-writing this function for another platform.

i2c = IIC(sclpin=5,sdapin=4,timeout=1000 )
i2c.i2c(<adr>,[byets to send],bytes to return,<option see text>)

Example

i2c.i2c(62,[1,2,3],2) # writes 1,2,3 to device at address 62 and the device returns 2 bytes

General call: Some device will respond to a general call. When a general call is issued (normally address 0) all devices will respond. The general call address follows the return bytes, for example if all of the device on the bus responded to a general call reset the command would be.

u2c.i2c(62,[0x10],0,0)

The 62 is not important in this case but can be used if the general call to the device expects to receive data from the device.