Adsense HTML/JavaScript

Tuesday, March 16, 2021

ESP32/MicroPython: get wifi network info, scan networks

 Get WiFi MAC address, network info such as ip, netmask...

from os import uname
from sys import implementation
import network
import ubinascii

ssid = "ssid"
password = "password"

print(implementation.name)
print(uname()[3])
print(uname()[4])
print()

mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC: " + mac)
print()

def do_connect():
    print('connect to network...')
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    
    print()
    print('network config:')
    print("interface's IP/netmask/gw/DNS addresses")
    print(wlan.ifconfig())
    
do_connect()

print('- bye -')


Scan WiFi networks:


from os import uname
from sys import implementation
import network
import ubinascii
import utime

ssid = "ssid"
password = "password"

print(implementation.name)
print(uname()[3])
print(uname()[4])
print()

mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC: " + mac)
print()

#init ESP32 as STA
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
utime.sleep(1)

def do_connect():
    global wlan
    print('connect to network...')
    
    wlan.active(True)
    if not wlan.isconnected():
        print('...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    
    print()
    print('network config:')
    print("interface's IP/netmask/gw/DNS addresses")
    print(wlan.ifconfig())
    
def do_scan():
    global wlan
    print('scan network...')
    wlan.active(True)
    for network in wlan.scan():
        print(network)
    
do_scan()

print('\n- bye -')



ref:
~ Docs > Quick reference for the ESP32 > Networking


Friday, March 12, 2021

Simple test DX-BT24 (BLE 5.0 Bluetooth module), with Seeeduino XIAO as serial bridge.

DX-BT24 is a BLE 5.0 Bluetooth module with UART interface. Seeeduino XIAO is programmed (in Arduino platform) as a serial bridge to link Arduino Serial Monitor and DX-BT24. The another side, Serial Bluetooth Terminal app (on Android) is used to test with.

/*
Xiao act as a bridge between USB and BT-24

The device (BT-24) is powered on 3V3
Connection
Xiao Rx   - device Tx
Xiao Tx   - device Rx
XIAO GND  - device GND
XIAO 3V3  - device VCC
XIAO D1   - STATE

*/

const int PIN_STATE = 1;


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PIN_STATE, INPUT);
  
  delay(1000);
  SerialUSB.begin(9600);
  Serial1.begin(9600);

  //wait serial ports to device
  //while(SerialUSB);
  while(!Serial1);
  
  //Blink the LED 3 times to indicate program started
  for(int i=0; i<3; i++){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
  }
}

void loop()
{
  if(SerialUSB.available() > 0){
    Serial1.write(SerialUSB.read());
  }

  if(Serial1.available() > 0){
    SerialUSB.write(Serial1.read());
  }


  if (digitalRead(PIN_STATE) == HIGH){
    digitalWrite(LED_BUILTIN, LOW);
  }else{
    digitalWrite(LED_BUILTIN, HIGH);
  }
  
}




link:
DX-BT24 product page


Wednesday, March 10, 2021

ESP_AT_Lib tested on XIAO with ESP32-S


ESP_AT_Lib is an ESP8266/ESP32 wrapper library for Arduino providing an easy-to-use way to manipulate ESP8266/ESP32-AT shields. Test on Seeeduino XIAO (programmed in Arduino framework) with ESP32-S. ESP32-S is act as a WiFi co-processor, controlled using AT-command. 

ESP_AT_Lib can be installed in Arduino IDE's Library Manager.


Try ESP_AT_Lib example ConnectWiFi



Uncomment the code "#define USE_ESP32_AT true" to use ESP32-AT commands.


Edit ssid/password for your WiFi network.


Here is the running output:



About ESP32-S/and ESP-AT:

ESP32-S is a wireless module based on ESP32, supports WiFi and Bluetooth 4.2, with built-in 32Mbit Flash, in the SMD38 package. There're also onboard PCB antenna and metal shield. In short, it's a small form factor and fairly high cost effective wireless module.

ESP32-S used is flashed  with ESP-AT Firmware, of version:
AT version:2.1.0.0(883f7f2 - Jul 24 2020 11:50:07)
SDK version:v4.0.1-193-ge7ac221
compile time(0ad6331):Jul 28 2020 02:47:21
Bin version:2.1.0(WROOM-32)

What is ESP-AT:

ESP-AT is a solution developed by Espressif to integrate connectivity into customers’ products, which can be quickly moved to mass production. It aims to reduce software development costs and quickly form products. With ESP-AT commands, you can quickly join the wireless network, connect to the cloud platform, realize data transmission and remote control functions, and realize the interconnection of everything through wireless communication easily.

ESP-AT is a project based on ESP-IDF or ESP8266_RTOS_SDK. It makes an ESP board work as a slave, and an MCU as a host. The host MCU sends AT commands to the ESP chip and receives AT responses back. ESP-AT provides a wide range of AT commands with different functions, such as Wi-Fi commands, TCP/IP commands, Bluetooth LE commands, Bluetooth commands, MQTT commands, HTTP commands, and Ethernet commands.



Please note that ESP32 AT uses two UART ports: UART0 is used to download firmware and log output; UART1 is used to send AT commands and receive AT responses.

All ESP32 modules use GPIO1 and GPIO3 as UART0, but they use different GPIOs as UART1. The following sections illustrate which GPIOs you should connect for each ESP32 series of modules.



In my case:
XIAO TX connect to GPIO16 (RX)
XIAO RX connect to GPIO17 (TX)
Wthout hardware flow control., just ignore CTS/RTS.

Suggested to provide separated power supply to ESP32:

At beginning, I power the ESP32-S module from XIAO 3V3. But it re-boot repeatedly, caused by:
Brownout detector was triggered

It's caused by power low. With separated power from breadoard power supply to ESP32-S, this problem fixed.

Monday, March 8, 2021

Update PyBoard MicroPython firmware, run on Ubuntu 20.10.

Install dfu-util: 

Run in Terminal -

$ sudo apt install dfu-util

Download firmware:

To update MicroPython firmware, visit http://micropython.org/download/, select your board and download firmware for your board. ex. pybv11-20210308-unstable-v1.14-83-g680ce4532.dfu, latest firmware for PYBv1.1 boards.

Flash .dfu to pyboard:

Switch to the downloaded file folder.

Run the command:

$ sudo dfu-util --alt 0 -D <downloaded firmware .dfu>