# 目前進度 設計 ![](https://i.imgur.com/pF40vFg.png) ## Blockly code 主程式 ```javascript import {pythonCode} from "./wifiSourceCode" // Blockly Defination Blockly.Blocks['wi_fi'] = { init: function() { this.appendDummyInput() .appendField("連線到 Wireless Access Point"); this.appendValueInput("wifiName") .setCheck(null) .setAlign(Blockly.ALIGN_RIGHT) .appendField("Wi-Fi 名稱"); this.appendValueInput("wifiPassword") .setCheck(null) .setAlign(Blockly.ALIGN_RIGHT) .appendField("Wi-Fi 密碼"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); this.setColour(270); this.setTooltip(""); this.setHelpUrl(""); } }; //------------------------------------------------------------- //Generater Blockly.Python['wi_fi'] = function(block) { var value_wifiname = Blockly.Python.valueToCode(block, 'wifiName', Blockly.Python.ORDER_ATOMIC); var value_wifipassword = Blockly.Python.valueToCode(block, 'wifiPassword', Blockly.Python.ORDER_ATOMIC); // TODO: Assemble Python into code variable. return pythonCode(value_wifiname,value_wifipassword); }; ``` ## Python Code 這個檔案的函式 會被引到主程式裡 ```javascript // Wifi module Source code function pythonCode(networkName="12345",networkPassword="12345"){ return` import gpb, uos, machine, image, display, ujson from gpb import UART, delay from machine import Pin import time led = Pin(13,Pin.OUT) WIFIname = '${networkName}' # 現場WiFi名稱 WIFIpassword = '${networkPassword}' # 現場 WiFi 密碼 def sendAT(cmd, delay1=100): #用來傳AT指令的function (包含傳送後的等待時間) uartdev.uart_write(str(cmd) + '\r\n') delay(delay1) def GetMessageAT_Byte2String(Switch): readMessage = zeroMessage receivedMessage = zeroMessage receivedMessage_String = str('') while Switch: readMessage = uartdev.uart_read(6) if readMessage == zeroread: #print('receivedMessage(GetMessageAT_Byte2String): ', receivedMessage) receivedMessage_String = convertByte2String(receivedMessage) print('Start receivedMessage_String(GetMessageAT_Byte2String): ', receivedMessage_String) print('End receivedMessage_String(GetMessageAT_Byte2String)') Switch = 0 receivedMessage = receivedMessage + readMessage return receivedMessage_String def convertByte2String(readMessage): # Convert bytearray to bytes byteObj = bytes(readMessage) return byteObj.decode("utf-8") def enableNetwork(display): sendAT('ATE0', 200) # Clear Mirror print("Read Version - AT+GMR ") sendAT('AT+GMR', 200) # Read Version GetMessageAT_Byte2String(1) #sendAT('AT+RESTORE', 1000) # 恢復預設設定 print("設為 station 模式 - AT+CWMODE=1 ") sendAT('AT+CWMODE=1', 200) # 設為 station 模式 GetMessageAT_Byte2String(1) # 連到 WiFi (建議等久一點) print("Connecting to WIFI.... Please wait") sendAT('AT+CWJAP=\"{}\",\"{}\"'.format(WIFIname, WIFIpassword), 15000) replymsg = GetMessageAT_Byte2String(1) if (replymsg.find("WIFI CONNECTED") != -1): print('connection success') return True else: print('connection failed') return False if __name__ == '__main__': #enableUART uartdev = UART(0, 115200) #id = 0 (UART device number) baudrate: 115200 zeroread = uartdev.uart_read(6) #read data whose size is 6 zero = '' zeroMessage = bytearray(zero) #enable Network WifiConnected = enableNetwork(display) delay(100) if ((WifiConnected == True)): led.on() sendAT('AT+CIFSR') # Display the IP Address replyip = GetMessageAT_Byte2String(1) print(replyip) ` } ``` 我有嘗試刪掉一些東西 但很多東西都有關聯 會一直跳ERROR Index HTML ## 測試 wifiSourceCode 是否能正常運作 ```htmlmixed <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./wifiBlockly.js"></script> <!-- <script src="./wifiSourceCode.js"></script> --> </head> <body> <script> function showPythonCodeInTextArea(){ const pre = document.getElementById("printPythonCondeInBrowser"); pre.innerHTML=pythonCode() } </script> <pre id="printPythonCondeInBrowser" ></pre> <button onclick="showPythonCodeInTextArea()">Show python code in Browser</button> </body> </html> ``` ![](https://i.imgur.com/S78Fpl7.png)