顯示具有 ESP8266 標籤的文章。 顯示所有文章
顯示具有 ESP8266 標籤的文章。 顯示所有文章

2020年4月5日 星期日

使用ESP32/8266晶片的MicroPython範例

一、uPyCraft工具安裝
(1) Install uPyCraft IDE – Windows PC Instructions
(2) Install uPyCraft IDE – Mac OS X Instructions
(3) Install uPyCraft IDE – Linux Ubuntu Instructions

二、工具
(1) Flash/Upload MicroPython Firmware to ESP32 and ESP8266
三、基本篇


四、感知篇

五、通訊篇

六、應用篇





2019年12月3日 星期二

[ MicroPython ] ESP32/ESP8266 MicroPython Web Server – Control Outputs 網頁中文化

MicroPython是以Python為基礎,可以內嵌到系統晶片上,以ESP32/ESP8266為例,可以參考:ESP32/ESP8266 MicroPython Web Server – Control Outputs的文章,本文將介紹如何將畫面中文化。

以上程式請存檔成為boot.py,並下載到ESP32/ESP8266中。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Complete project details at https://RandomNerdTutorials.com

try:
  import usocket as socket
except:
  import socket

from machine import Pin
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = '您的熱點'
password = '熱點的密碼'

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

led = Pin(2, Pin.OUT)

以下請儲存成main.py,文字中黃色底為修改部份,特別是meta標籤中,屬性chartset設定utf-8,即可以中文化。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Complete project details at https://RandomNerdTutorials.com

def web_page():
  if led.value() == 1:
    gpio_state=""
  else:
    gpio_state=""
  
  html = """<html><head> <title>ESP Web Server</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; 
  border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  .button2{background-color: #4286f4;}</style></head><body> <h1> 敏哥的家 </h1> 
  <p>客廳的電燈: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
  <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  if led_on == 6:
    print('LED ON')
    led.value(1)
  if led_off == 6:
    print('LED OFF')
    led.value(0)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()

請記得用手機打開瀏覽器,輸入網址,即可以看到控制資訊,以下是在uPyCraft執行的資訊:

Connection successful
('192.168.43.127', '255.255.255.0', '192.168.43.120', '192.168.43.120')


Got a connection from ('192.168.43.120', 37436)
Content = b'GET /?led=off HTTP/1.1\r\nHost: 192.168.43.127\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nSave-Data: on\r\nUser-Agent: Mozilla/5.0 (Linux; Android 9; ASUS_I01WD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\nReferer: http://192.168.43.127/?led=off\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: zh-TW,zh;q=0.9\r\n\r\n'
LED OFF
Got a connection from ('192.168.43.120', 37438)


Content = b'GET /?led=on HTTP/1.1\r\nHost: 192.168.43.127\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nSave-Data: on\r\nUser-Agent: Mozilla/5.0 (Linux; Android 9; ASUS_I01WD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\nReferer: http://192.168.43.127/?led=off\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: zh-TW,zh;q=0.9\r\n\r\n'
LED ON
Got a connection from ('192.168.43.120', 37450)


Content = b'GET /?led=off HTTP/1.1\r\nHost: 192.168.43.127\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nSave-Data: on\r\nUser-Agent: Mozilla/5.0 (Linux; Android 9; ASUS_I01WD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\r\nReferer: http://192.168.43.127/?led=on\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: zh-TW,zh;q=0.9\r\n\r\n'

執行結果:



2017年1月23日 星期一

[ESP8266] 利用ArduinoIDE開發ESP8266專案--開發環境設定

目前市售的ESP8266模組有很多種版本,如下圖
本篇是選用ESP01作為介紹
接下來分幾個部分
1.ESP8266 Core For Arduino安裝
2.連接ESP8266燒錄電路
3.燒錄Arduino程式到ESP8266


1.ESP8266 Core For Arduino安裝
這邊先到Arduino官網下載頁面




然後找舊版的ArduinoIDE[整合開發環境(Integrated Development Environment)]安裝

這裡選擇1.6.9版本

進入下載頁面後選Just Download
接下來安裝就不贅述,就一直下一步就好,若你有安裝其他版本的ArduinoIDE,安裝程式會先解除你目前的ArduinoIDE,才會繼續安裝1.6.9版本的ArduinoIDE

安裝好ArduinoIDE後,可能不會像畫面是中文的,不過沒關係,先選擇圖片中相對位置的選項進行更改,檔案->偏好設定

選擇後會出現這個視窗,若沒中文的同學請在Editor language選擇Chinese(Taiwan),然後再選擇下方的按鈕

跑出另一個視窗後輸入http://arduino.esp8266.com/stable/package_esp8266com_index.json,然後選擇OK,另一個視窗也選擇OK,再重開ArduinoIDE之後,就是中文的啦!

接下來到"工具->板子->板子管理員"中安裝ESP8266 Core

 選擇後後會出現下面這個視窗,選擇2.3.0版本安裝,然後他就會開始跑下載,下在完成後關閉按鈕才會跑出來,這時候才能關掉視窗

若有成功安裝,則板子內會出現許多新的選項

2.連接ESP8266燒錄電路
接下來會需要幾樣工具,麵包版或洞洞板USB轉TTL模組(建議用CH340系列或CP2102系列,較不會燒錄程式失敗)、5V轉3.3V降壓模組或降壓IC,以下是我用的USB轉TTL模組和ESP01版本的腳位圖(黑色板子跟藍色板子的ESP01腳位一樣)



幾個比較要注意的地方
1.ESP8266能接受的電壓3.3V
2.要接3.3V的有ESP8266的VCCCH_PD兩個部分
3.GPIO0要在接上電前接到GND,否則會沒辦法進入燒錄模式
電路如下圖
USB TTL <–> ESP-01
GND               GND
TX                   RX
RX                  TX
3.3V               VCC
 3.3V             CH_PD
GPIO0           GND

3.燒錄Arduino程式到ESP8266內
這裡要先確定你ESP8266的Flash Size,若你的ESP01是藍色的,就選第一個512K(64K SPIFFS),若你的ESP01是黑色的,就選第四個1M(512K SPIFFS)
再來就是Upload Speed,若CH340系列或CP2102系列的USB轉TTL模組,可以選擇921600,若不是CH340系列或CP2102系列的USB轉TTL模組或是上傳一直失敗的同學,請選擇115200,然後甚麼都先不用寫,直接選擇"上傳"按鈕(箭頭那個)
若程式有上傳成功,會出現像下圖的訊息(要在"偏好設定"中,勾選"顯示詳細輸出"的"編譯"和"上傳",才會有橘色字的訊息)

那本篇的教學先到這邊告個段落,下次會繼續寫關於ESP8266程式部分與電路部分,大概內容會以現在目前常見的Arduino模組,將電路與程式碼移植到ESP8266上

參考:
1.Arduino core for ESP8266 WiFi chip
https://github.com/esp8266/Arduino
2.燒錄程式電路圖
http://www.circuitsgallery.com/2016/01/getting-started-with-esp8266-esp-01.html
3.ESP8266模組
http://web-engineering.info/node/65
http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family

2016年2月12日 星期五

[物聯網] URSA IoT模組的初體驗

耕雲推出URSA IoT模組,採用ESP8266晶片設計而成,我們就來測試一下。
打開Aduino先在檔案->偏好設定選單的額外板子管理員網址中輸入http://arduino.esp8266.com/stable/package_esp8266com_index.json,再到工具->板子選單中,開啟板子管理員,找到esp8266進行板子的安裝。
安裝完後就可以在板子選擇Adafruit HUZZAH ESP8266。

首先您要先準備2個硬體設備,URSA IoT模組USB/TTL轉換器
USB/TTL轉換器腳位如下:
1.DTR
2.RXD
3.TXD
4.VCC
5.CTS
6.GND

URSA IoT模組腳位如下(P1 & P2):
P1
1.Tx
2.Rx
3.Gnd
4.1  (GPIO5)
5.2  (GPIO4)
3.3  (GPIO0)
4.4  (GPIO2)
P2
1.5 (5V)
2.3.3 (3.3V)
3.Gnd
4.A (A0)
5.5  (GPIO14)
6.6  (GPIO12)
7.7  (GPIO13)

電路接線如表一

表一、URSA IoT模組和USB/TTL轉換器和腳位對應表
URSA IoT模組USB/TTL轉換器
3.3VCC
GndGnd
RxTxd
TxRxd

在表二的7號上接上LED。

表二、腳位對應表
電路板上的編號Arduino上的宣告
15 (GPIO5)
24 (GPIO4)
30 (GPIO0)
42 (GPIO2) 
514 (GPIO14)
612 (GPIO12)
713 (GPIO13)

我們改寫範例ESP8266Blink上的範例,紅色為修改的內容。
void setup() {
  pinMode(13, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(13, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

在進行燒錄時,S2的JUMP必須要朝下。
若使用到GPIO0時,在燒錄完成後,必須要把S2的JUMP朝上。
您不妨把LED換其他腳位測試一下表二上的對應。
注意!JUMP如果在下方的話,重新啟動後會進入燒錄模式,不會執行程式。