arduino服务器_如何使用Arduino检查Web服务器的响应状态
arduino服務器
by Harshita Arora
通過Harshita Arora
如何使用Arduino檢查Web服務器的響應狀態 (How to use Arduino to check your web server’s response status)
Last year, I created Crypto Price Tracker (an app which was acquired by Redwood City Ventures this year). A back end member of my team had been using an Arduino setup to check web server response statuses continuously to get updates all the time. I found that setup to be pretty useful and interesting.
去年,我創建了Crypto Price Tracker (一個應用程序,今年被Redwood City Ventures收購)。 我團隊的一個后端成員一直在使用Arduino設置來連續檢查Web服務器響應狀態以始終獲取更新。 我發現該設置非常有用且有趣。
I researched about it and recreated the setup for myself. In this article, I’ll show you how you can build it yourself, too.
我對此進行了研究,并為自己重新創建了設置。 在本文中,我將向您展示如何自己構建它。
您需要的東西: (Things that you need:)
Arduino Uno
Arduino Uno
Ethernet Shield for Arduino (to connect the Arduino to the Internet)
Arduino的以太網屏蔽 (將Arduino連接到Internet)
設置 (Setting It Up)
Insert one end of the resistor into the breadboard slot 1b and the other into slot 5b.
將電阻的一端插入面包板插槽1b,另一端插入插槽5b。
Insert one end of the first jumper cable into the breadboard slot 1e. Insert the other end into the GND slot of the Ethernet shield.
將第一根跳線的一端插入面包板插槽1e中。 將另一端插入以太網屏蔽的GND插槽。
Insert one end of the second jumper cable into the breadboard slot 6e. Insert the other end into pin slot 2 of the Ethernet shield.
將第二根跨接電纜的一端插入面包板插槽6e中。 將另一端插入以太網屏蔽的插針插槽2中。
Connect the Ethernet cable from your router to your Ethernet shield.
將以太網電纜從路由器連接到以太網屏蔽。
This is what my setup looks like:
這是我的設置:
7. Open a command line interface on your machine and check and note your default gateway. This can be done using ipconfig command on Windows or the netstat -nr | grep default command on Linux/Mac.
7.在計算機上打開命令行界面,然后檢查并記下默認網關。 可以使用ipconfig完成 Windows或netstat -nr | grep default使用netstat -nr | grep default netstat -nr | grep default Linux / Mac上的命令。
8. Download and install the Arduino IDE if you haven’t already.
8.如果尚未下載并安裝Arduino IDE,請下載并安裝。
9. Open the IDE and go to Files -> Examples -> Ethernet -> WebClientRepeating. You should see the following code:
9.打開IDE,然后轉到“文件” -& > 為例es - >的Eth er網- > WebClientRepeating。 您應該看到以下代碼:
10. Edit the line 37 to be an IP address in the range (1–254) of your default gateway IP. So for example, if my default gateway is 10.0.0.1, then I can use an IP address from 10.0.0.2 to 10.0.0.254. It is, however, important to make sure that the IP that you’re using doesn’t conflict with any other IP addresses on your network.
10.將第37行編輯為默認網關IP范圍(1-254)中的IP地址。 因此,例如,如果我的默認網關是10.0.0.1,則可以使用從10.0.0.2到10.0.0.254的IP地址。 但是,重要的是要確保您使用的IP地址與網絡上的任何其他IP地址都沒有沖突。
For this example, I changed the line of code to be:
對于此示例,我將代碼行更改為:
IPAddress ip(10, 0, 0, 2);
IPAddress ip(10, 0, 0, 2);
11. Change the DNS in line 40 to be 8.8.8.8 (this is the Google Public DNS and is just something I prefer, you may use a DNS that you prefer).
11.將第40行的DNS更改為8.8.8.8 (這是Google Public DNS,這只是我喜歡的東西,您可以使用自己喜歡的DNS)。
For this example, I changed the line of code to be:
對于此示例,我將代碼行更改為:
IPAddress myDns(8, 8, 8, 8);
IPAddress myDns(8, 8, 8, 8);
12. Change the URL in line 45 to a URL matching your web server. If you would like to use an IP address instead, then comment line 45 and uncomment line 46. Since I am using a web server that I’m hosting locally, for this example, I will use an IP address.
12.將第45行中的URL更改為與您的Web服務器匹配的URL。 如果您想改用IP地址,請在注釋行45和注釋行46中取消注釋。 由于我使用的是本地托管的Web服務器,因此在此示例中,我將使用IP地址。
For this example, I changed the line of code to be:
對于此示例,我將代碼行更改為:
//char server[] = “www.arduino.cc";IPAddress server(127,0,0,1);
//char server[] = “ www.arduino.cc "; IPAddress server(127,0,0,1);
Note that the port or the path here is not important yet. Just the IP Addressis needed. If you would like to change the port that is used for the GETrequest, you may change it on line 94.
請注意,此處的端口或路徑并不重要。 僅需要IP地址。 如果您想更改用于GETrequest的端口,則可以在第94行進行更改。
For this example, I have hosted my local webserver on port 3000. Thus, I will change the code in line 94 to something like this:
對于此示例,我將本地Web服務器托管在端口3000上。因此,我將在第94行中的代碼更改為如下所示:
if (client.connect(server, 3000)) {
if (client.connect(server, 3000)) {
13. Edit the GET request that is pre-written in lines 97 - 100 to follow this pattern:
13.編輯在第97-100 行中預先編寫的GET請求,以遵循以下模式:
client.println(“GET /path_to_url HTTP/1.1”);client.println(“Host: 127.0.0.1”);client.println(“Connection: close”);client.println();
client.println(“GET /path_to_url HTTP/1.1”); client.println(“Host: 127.0.0.1”); client.println(“Connection: close”); client.println();
14. We can now start programming the behaviour of the LED depending on the web server status and response. To do this, we must first declare the pin we’re using for the LED on our Ethernet shield.
14.現在,我們可以開始根據Web服務器的狀態和響應對LED的行為進行編程。 為此,我們必須首先在以太網屏蔽上聲明用于LED的引腳。
Add the following line of code after the first two include statements of the program:
在前兩個包括之后添加以下代碼行 該計劃的陳述:
int LED = 2;
int LED = 2;
15. Add the following lines of code at the beginning of the setup() function.
15.在setup()函數的開頭添加以下代碼行。
pinMode(LED, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW); //program starts with the LED turned off
digitalWrite(LED, LOW); //program starts with the LED turned off
16. Add the following line of code after the GET request line that we previously edited:
16.在我們先前編輯的GET請求行之后添加以下代碼行:
digitalWrite(LED, LOW);
digitalWrite(LED, LOW);
17. Finally, add this line of code at the beginning of the else statement of the same conditional:
17.最后,在else的開頭添加這一行代碼 相同條件的語句:
digitalWrite(LED, HIGH);
digitalWrite(LED, HIGH);
And voilà, you’re done!
瞧,您完成了!
Upload the program to your Arduino. Open the serial monitor from the top right part of the IDE and watch the response. If your server doesn’t respond, the LED glows, if it does, the LED stays off :)
將程序上傳到您的Arduino。 從IDE的右上方打開串行監視器,然后觀察響應。 如果您的服務器沒有響應,則LED發光,如果發光,則LED保持熄滅:)
You can check out my final code here.
您可以在此處查看我的最終代碼。
檢查響應 (Checking Response)
If you would also like to validate the response that you receive from yourweb server, then you may add them inside the following conditional of theprogram.
如果您還想驗證從Web服務器收到的響應,則可以將它們添加到程序的以下條件中。
if (client.available()) {char c = client.read();Serial.write(c);}
if (client.available()) { char c = client.read(); Serial.write(c); }
The variable c is where the response is stored.You could check it like this:
變量c是響應的存儲位置,您可以像這樣檢查它:
if (client.available()) {
if (client.available()) {
char c = client.read();if(c == “arduino is great”){ digitalWrite(LED, LOW); //correct response}else{digitalWrite(LED, HIGH); //wrong response}Serial.write(c);}
char c = client.read(); if(c == “arduino is great”){ digitalWrite(LED, LOW); //correct response digitalWrite(LED, LOW); //correct response } else{ digitalWrite(LED, HIGH); //wrong response digitalWrite(LED, HIGH); //wrong response } Serial.write(c); }
Do note that, if you’re trying to do this, then it’s best to get rid of thedigitalWrite statement after the GET request. Depending on your response,you may also have to parse JSON values. There are several ways to do thisand plenty of tutorials/articles around for it too! Make sure to check themout!
請注意,如果您嘗試執行此操作,則最好在GET請求之后刪除digitalWrite語句。 根據您的響應,您可能還必須解析JSON值。 有幾種方法可以做到這一點,并且還有很多教程/文章! 請務必檢查一下!
Have fun! Feel free to email me at harshita (at) harshitaapps.com for any questions, feedback, or ideas!
玩得開心! 如有任何問題,反饋或想法,請隨時通過harshita (at) harshitaapps.com給我發送電子郵件!
Make sure to check out Crypto Price Tracker app if you’re interested/invested in cryptocurrencies! :)
如果您對加密貨幣感興趣/投資了,請確保簽出Crypto Price Tracker應用程序! :)
翻譯自: https://www.freecodecamp.org/news/how-to-use-arduino-to-check-your-web-servers-response-status-9e47e02a61cc/
arduino服務器
總結
以上是生活随笔為你收集整理的arduino服务器_如何使用Arduino检查Web服务器的响应状态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: msbuild构建步骤_如何按照以下步骤
- 下一篇: 如何使用Node.js,Express和