おはようございます。

期日前投票の機会がなかったので、先ほど近所の投票所で第49回衆議院選挙投票と2021年最高裁判所裁判官国民審査を済ませてきました。

今回は、投票権と審査権を持つ国民の意思が反映される審判が下される気がしますね。

 

最近は、なぜかYouTubeの古代史のコンテンツに注目しています。

その筆頭は、梅前佐紀子さんかな。飛鳥探訪良いですね。グッ

それから、古墳探訪では、”世界の墳丘から”のgotas1975さんグッ

武田晴樹氏の 燃え盛るように熱い日本古代史グッ

 

さて前回の宣言どおりに今日は、ARM-FirstのMicroPythonでWiFi接続のスクリプトを書きましたので紹介します。

BGMは、いつものRadio GardenでWKNL/100.9 K-Hits FMです。

 

からの続きになります。

上差し手前が、MicroPythonの載ったARM-Firstです。奥は、Weather Stationの竹 バージョンです。

 

①メインのスクリプトで、後から示す②、③を参照しています。②と③は、ARM-Firstに装着したSD CARDに常駐させてあります。

from wifi import * # wifi.py in SD Card@ARM-First
def main():

    lcd_init()
    print("=== START CONNECTING ===")
    # XBEE_RESET
    uart_init(uart,'B1')
    # To connect wifi, Replace with your network credentials
    res = connect("SSID","PASSWORD")
    # LCD1602 opening message
    lcd_string("WiFi & LCD Test!",LCD_LINE_1)
    lcd_string("IP"+res,LCD_LINE_2)
############################################################
main()

 

②今回のメインであるWiFi接続のスクリプトのライブラリ(wifi.py)

# ARM-First w/ESP8266 AT Command wifi library
# Last update 2021/10/21 by jk1brk
from pyb import UART, Timer
from lcd1602 import * # lcd1602.py in SD Card@ARM-First

#############################################################
# esp8266 AT command control library
#############################################################

# init with given baudrate, timeout & buffer length
uart = pyb.UART(2, 115200, timeout=3000, read_buf_len=4096)
# init with given parameters
uart.init(115200, bits=8, parity=None, stop=1)

def uart_init(uart,reset):
    uart = uart
    dbuf = b''
    reset = Pin(reset,Pin.OUT)
    reset.value(0)
    pyb.delay(1)
    reset.value(1)
    print('Wait->',end='')
    for count in range(5):
       print('->', end='')
       lcd_string("Wait...         ",LCD_LINE_1)
       pyb.delay(1000)
    print()
    while True:
        try:
            res = atout('AT+CWMODE_CUR=1')
            if b'OK' in res:
                print("Open Network UART")
                lcd_string("Open N/W UART   ",LCD_LINE_1)
            else:
                print("Not Open UART")
                lcd_string("Not Open UART   ",LCD_LINE_2)
            return
        except:
            pass
        
def atout(cmd,timeout=10000):
    t_count = 0
    resp = b''
    uart.write(cmd+'
')
    while True:
        if uart.any() != 0:
            res = uart.readline()
            resp += res
            if b'OK' in res:
              #print(resp)
              return resp
            if b'ERROR' in res:
              return b''
        if t_count==timeout:return "Command_rp Timeout"
        t_count += 1
        pyb.delay(1)
    
def connect(ssid,password,timeout=5000):
    res = atout('AT+CWJAP_CUR="%s","%s"'%(ssid,password),timeout)
    #print(res)
    if "CONNECTED" in res:print("WIFI CONNECTED")
    else:
        print("Not connected try again!")
    res = atout('AT+CIFSR',timeout)
    if "STAIP" in res:
        work = res.decode().split()[1]
        print(work.split(',')[1])
    else:
        print("No IP address detected!")
    return work.split(',')[1]

③前回紹介したLCD1602を制御するスクリプトのライブラリ(LCD1602.py)
#LCD1602 test program for ARM-First
#Last update 2021/10/9
from pyb import Pin
import time, pyb

LCD_CHR=1
LCD_CMD=0
LCD_LINE_1=0x80
LCD_LINE_2=0xC0

pin_bl = Pin('A15',Pin.OUT)
pin_e  = Pin('B11',Pin.OUT)
pin_rs = Pin('A4',Pin.OUT)
pin_d4 = Pin('C13',Pin.OUT)
pin_d5 = Pin('B8',Pin.OUT)
pin_d6 = Pin('B12',Pin.OUT)
pin_d7 = Pin('C7',Pin.OUT)
pin_bl.value(1)

pin_c0 = Pin('C0',Pin.IN)
adc = pyb.ADC(pin_c0)              # create an analog object from a pin
val = adc.read()                   # read an analog value

def lcd_init():
  time.sleep_ms(15)
  
  #lcd_nibble(0x03)
  #time.sleep_us(4100)
  #lcd_nibble(0x03)
  #time.sleep_us(100)
  lcd_byte(0x33,LCD_CMD)
  
  #lcd_nibble(0x03)
  #time.sleep_us(40)
  #lcd_nibble(0x02)
  #time.sleep_us(40)
  lcd_byte(0x32,LCD_CMD)
  
  # 4bit mode/2 lines/char. size 5*7
  lcd_byte(0x28,LCD_CMD)
  # display on/block cusor off/blink off
  lcd_byte(0x0C,LCD_CMD)
  # increment/shiht off
  lcd_byte(0x06,LCD_CMD)
  # display clear
  lcd_byte(0x01,LCD_CMD)

  time.sleep_us(1640)
  
def lcd_nibble(bits):
  pin_e.value(1)
  pin_d4.value(bits & 0x10)
  pin_d5.value(bits & 0x20)
  pin_d6.value(bits & 0x40)
  pin_d7.value(bits & 0x80)
  pin_e.value(0)
  
def lcd_byte(bits,mode):
  pin_rs.value(mode)
  lcd_nibble(bits)
  time.sleep_us(40)
  bits<<=4
  lcd_nibble(bits)
  time.sleep_us(40)
    
def lcd_string(message,line):
  # Send string to display
  lcd_byte(line, LCD_CMD)
  for i in range(len(message)):
    lcd_byte(ord(message[i]),LCD_CHR)

 

チョキ

 

次回は、ARM-Firstに載ったMicroPythonでWeather Stationのスクリプトを書いたので紹介します。

CU(See you) ツートツート トトツー

 

【参照記事】