2017年9月19日 星期二

用webmos d1 mini 玩物聯網(3)-用電腦控制wemos上的LED燈

一、教學目標:
1開啟 wemos的伺服器模式
2.透過瀏覽器來控制 wemos的led燈

二、接線圖
led燈正極接在D5,負極接GND

三、程式碼



  1. /*
  2. This sketch demonstrates how to set up a simple HTTP-like server.
  3. The server will set a GPIO pin depending on the request
  4. http://server_ip/gpio/0 will set the GPIO2 low,
  5. http://server_ip/gpio/1 will set the GPIO2 high
  6. server_ip is the IP address of the ESP8266 module, will be
  7. printed to Serial when the module is connected.
  8. */
  9.  
  10. #include <ESP8266WiFi.h>
  11.  
  12. #ifndef STASSID
  13. #define STASSID "your-ssid"
  14. #define STAPSK "your-password"
  15. #endif
  16.  
  17. const char* ssid = STASSID;
  18. const char* password = STAPSK;
  19.  
  20. // Create an instance of the server
  21. // specify the port to listen on as an argument
  22. WiFiServer server(80);
  23.  
  24. void setup() {
  25. Serial.begin(115200);
  26.  
  27. // prepare LED
  28. pinMode(LED_BUILTIN, OUTPUT);
  29. digitalWrite(LED_BUILTIN, 0);
  30.  
  31. // Connect to WiFi network
  32. Serial.println();
  33. Serial.println();
  34. Serial.print(F("Connecting to "));
  35. Serial.println(ssid);
  36.  
  37. WiFi.mode(WIFI_STA);
  38. WiFi.begin(ssid, password);
  39.  
  40. while (WiFi.status() != WL_CONNECTED) {
  41. delay(500);
  42. Serial.print(F("."));
  43. }
  44. Serial.println();
  45. Serial.println(F("WiFi connected"));
  46.  
  47. // Start the server
  48. server.begin();
  49. Serial.println(F("Server started"));
  50.  
  51. // Print the IP address
  52. Serial.println(WiFi.localIP());
  53. }
  54.  
  55. void loop() {
  56. // Check if a client has connected
  57. WiFiClient client = server.available();
  58. if (!client) {
  59. return;
  60. }
  61. Serial.println(F("new client"));
  62.  
  63. client.setTimeout(5000); // default is 1000
  64.  
  65. // Read the first line of the request
  66. String req = client.readStringUntil('\r');
  67. Serial.println(F("request: "));
  68. Serial.println(req);
  69.  
  70. // Match the request
  71. int val;
  72. if (req.indexOf(F("/gpio/0")) != -1) {
  73. val = 0;
  74. } else if (req.indexOf(F("/gpio/1")) != -1) {
  75. val = 1;
  76. } else {
  77. Serial.println(F("invalid request"));
  78. val = digitalRead(LED_BUILTIN);
  79. }
  80.  
  81. // Set LED according to the request
  82. digitalWrite(LED_BUILTIN, val);
  83.  
  84. // read/ignore the rest of the request
  85. // do not client.flush(): it is for output only, see below
  86. while (client.available()) {
  87. // byte by byte is not very efficient
  88. client.read();
  89. }
  90.  
  91. // Send the response to the client
  92. // it is OK for multiple small client.print/write,
  93. // because nagle algorithm will group them into one single packet
  94. client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "));
  95. client.print((val) ? F("high") : F("low"));
  96. client.print(F("<br><br>Click <a href='http://"));
  97. client.print(WiFi.localIP());
  98. client.print(F("/gpio/1'>here</a> to switch LED GPIO on, or <a href='http://"));
  99. client.print(WiFi.localIP());
  100. client.print(F("/gpio/0'>here</a> to switch LED GPIO off.</html>"));
  101.  
  102. // The client will actually be *flushed* then disconnected
  103. // when the function returns and 'client' object is destroyed (out-of-scope)
  104. // flush = ensure written data are received by the other side
  105. Serial.println(F("Disconnecting from client"));
  106. }
2.並修改如下

const char* ssid = "xxxx";////WeMos要連上的基地台 ssid
const char* password = "xxxxxx";  //基地台密碼

3.點選序列埠螢幕(serial monitor)可以看到以下畫面 server已經啟動,且ip為192.168.1.103(每一台分到ip是不一樣的。



4.打開瀏覽器,網址部份打入:
http://192.168.1.103/LED=ON,出現如下的畫面,燈變


http://192.168.1.103/gpio/LED=OFF ,出現如下的畫面,燈變






用webmos d1 mini 玩物聯網(2)-簡易的blink程式

一、教學目標:
1.認識WeMos D1 mini 的腳位,並修改內建程式blink程式
2.利用範例程式進行wifi scan

webmos d1 mini



腳位圖

D4:內建LED燈
D1,D2 I2C通訊協定。




腳位圖

二、程式碼

  1. //WeMos內建的led燈的腳位為 gpio2 或是D4
  2. //pinMode 可寫成 pinMode(2,out)
  3. //或是 pinMode(D4,out)
  4.  
  5. void setup() {
  6. // initialize digital pin LED_BUILTIN as an output.
  7. pinMode(D4, OUTPUT);
  8. }
  9.  
  10. // the loop function runs over and over again forever
  11. void loop() {
  12. digitalWrite(D4, HIGH); // turn the LED on (HIGH is the voltage level)
  13. delay(1000); // wait for a second
  14. digitalWrite(D4, LOW); // turn the LED off by making the voltage LOW
  15. delay(1000); // wait for a second
  16. }
  17.  

2017年9月18日 星期一

用webmos d1 mini 玩物聯網(1)-環境設定與簡易測試

一、教學目標:
1.學習在arduino IDE中安裝與設定wemos程式
2.利用範例程式進行wifi scan

webmos簡圖

圖片來源:

webmos d1 mini



腳位圖

D4:內建LED燈
D1,D2 I2C通訊協定。




二、操作步驟:
1.安裝ESP8266 開發套件:
在IDE 檔案→偏好設定
在額外的開發板管理網址輸入
http://arduino.esp8266.com/stable/package_esp8266com_index.json

2.工具→開發板管理員 →找到ESP8266,按More Info 再按安裝esp8266



3請選擇正確的開發板:工具>>開發板>>選 WeMos D1 R2 & mini

4.由檔案->範例->ESP8266Wifi 中的 WiFiScan來測試一下



5.測試結果:可以發現,已經找到10個可以使用wifi。




二、參考資料:


三、練習:

2017年4月30日 星期日

用mblock玩arduino(11)mblock的離線執行

一、教學目標:
將mblock程式寫完之後,轉換成Arduino程式語言,上傳到Arduino控制板,離線執行程式。

注意事項:
(1)與鍵盤、螢幕等電腦有關的指令都不能用,因為已經是離線(離開電腦了)
(2)定義的變數、指令都只能用英文,不可以用中文
(3)積木指令從頭到尾一串

二、相關說明:
 (1)mblock程式中綠旗被點一下改成Arduino主程式


(2) Arduino主程式按右鍵-點選upload to arduino

(3)點選 上傳到Arduino即可



(4)電源部份:可以用手機行動電源接上usb即可


參考資料:

http://blog.ilc.edu.tw/blog/index.php?op=printView&articleId=605798&blogId=868