HTTP - 设置伺服器以控制 LED

材料准备

  • AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1

  • 面包板 x1

  • LED x 1

  • 1KΩ电阻 x 1

流程

这个范例里,我们使用Ameba连上WiFi,作为服务器端,用户可通过网页来控制LED的开关。

First, connect Ameba with the LED. In a LED, the longer pin is the positive pole, and the shorter pin is the negative pole. So, we connect the shorter pin to GND and connect the longer pin to D13. Additionally, to avoid the electric current exceeds the tolerance of the LED and causes damage, we connect a resistance on the positive pole.

AMB21 / AMB22 接线图:

../../../../_images/image110.png

AMB23 接线图:

../../../../_images/image1-12.png

BW16 接线图:

../../../../_images/image1-23.png

备注

对于 BW16 ,您可以考虑将“LED_PIN”重新定义为 10 用于内置绿色 LED,或 11 用于蓝色内置 LED,或 12 用于红色内置 LED,以避免使用额外的组件。

BW16-TypeC Wiring Diagram:

../../../../_images/image1-33.png
Then open “File” “Examples” “WiFi” “SimpleWebServerWiFi”
../../../../_images/image29.png
将WiFi连线的相关讯息,如下图中黄色标注的地方进行对应的修改。
../../../../_images/image35.png

上传程式码之后,按下Ameba的Reset按钮, 在WiFi连线成功后,会提示”To see this page in action, open a browser to http://xxx.xxx.xxx.xxx”。如下图黄色标注:

“To see this page in action, open a browser to http://xxx.xxx.xxx.xxx”

in the Arduino IDE as shown in the figure:

../../../../_images/image44.png

在连接在同一WiFi环境下的电脑或手机上,打开浏览器,输入提示的地址

../../../../_images/image55.png

点击网页中的“here”即可完成LED灯泡的点亮与关闭操作。在连线以及点击时,Ameba会有相关的信息显示。

程式码说明

Use WiFi.begin() to establish WiFi connection. https://www.arduino.cc/en/Reference/WiFiBegin

To get the information of a WiFi connection: Use WiFi.SSID() to get SSID of the current connected network. https://www.arduino.cc/en/Reference/WiFiSSID

Use WiFi.RSSI() to get the signal strength of the connection. https://www.arduino.cc/en/Reference/WiFiRSSI

Use WiFi.localIP() to get the IP address of Ameba. https://www.arduino.cc/en/Reference/WiFiLocalIP

Use WiFiServer server() to create a server that listens on the specified port. https://www.arduino.cc/en/Reference/WiFiServer

Use server.begin() to tell the server to begin listening for incoming connections. https://www.arduino.cc/en/Reference/WiFiServerBegin

Use server.available() to get a client that is connected to the server and has data available for reading. https://www.arduino.cc/en/Reference/WiFiServerAvailable

Use client.connected() to get whether or not the client is connected. https://www.arduino.cc/en/Reference/WiFiClientConnected

Use client.println() to print data followed by a carriage return and newline. https://www.arduino.cc/en/Reference/WiFiClientPrintln

Use client.print() to print data to the server that a client is connected to. https://www.arduino.cc/en/Reference/WiFiClientPrint

Use client.available() to return the number of bytes available for reading. https://www.arduino.cc/en/Reference/WiFiClientAvailable

Use client.read() to read the next byte received from the server the client is connected to. https://www.arduino.cc/en/Reference/WiFiClientRead

Use client.stop() to disconnect from the server the client is connected to. https://www.arduino.cc/en/Reference/WiFIClientStop