Project Description

I created that sends HTTP request to find Korea using a Naver small dictionary web service using an Ethernet shield and output the result to a serial monitor. This example is modified from arduino WebClient example.

 

 

1. Materials and circuits

You will need an Ethernet shield board, an Arduino board or a mega board. You must have an Internet router (router or DHCP server) to be assigned an IP address.

 

materials

=====

  • Arduino board
  • Ethernet shield board
  • Internet router
  • Internet line

 

Circuit configuration
=======
The circuit configuration is not required, but the Ethernet shield is attached to the Arduino board and connected to the Internet.

 

 

2. Sketch Description

 As described above, I modified the WebClient sketch provided by Arduino IDE to create an HTTP request to search for “Korea” in Naver’s small dictionary and send the result to a serial monitor.

 

sketch

=====

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  Web client 
This sketch uses the ArduinoWiznets Ethernet shield,
 and  Access the Naver Dictionary (http://endic.naver.com).
 Circuit:
 * The Ethernet shield is connected to digital 10, 11, 12, and 13 pins. Also, SD
   Digital 4th pin is used for card chip selection.
 Created December 18, 2009 by David A. Mellis,

April 09, 2012 It is changed by Adrian McEwen based on work by Tom Igoe
                  
 October 11, 2014 Modified by WayFarer 
 
 
 */
#include <SPI.h>
#include <Ethernet.h>
// Please put the MAC address for the Ethernet controller
// New Ethernet shield has MAC address is printed on a sticker
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// If you do not want to use DNS (and also reduce the size of the sketch)
// Use the numeric IP address instead of the server’s name:
//IPAddress server(202.131.24.231);  // Naver dictionary IP address (without DNS)
char server[] = “endic.naver.com”;    // Naver dictionary  address (with DNS)
// Set the static IP address to use in case of not being assigned by DHCP
//
IPAddress ip(192, 168, 0, 100);
// Declare the Ethernet client instance (object). 
//  An instance to connect the web server ,
// An instance to connect to, and connect to server 80 port
// 
EthernetClient client;
void setup() {
  // Open the serial port and wait for the connection:
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for the serial port to connect. Requires only Leonardo board
  }
  // Start an Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println(“Failed to configure Ethernet using DHCP”);
    // Attempt to connect to a static IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // Wait 1 second for ethernet shield initialization:
  delay(1000);
  Serial.println(“connecting…”);
  //If connected, inform the serial monitor:
  if (client.connect(server, 80)) {
    Serial.println(“connected”);
    // create HTTP request:
    client.println(“GET /popManager.nhn?sLn=kr&m=search&searchOption=&query=korea HTTP/1.1″);
    client.println(“Host: endic.naver.com”);
    client.println(“Connection: close”);
    client.println();
  }
  else {
    // If you are not connected to the server:
    Serial.println(“Not connected”);
  }
}
void loop()
{
  // If there is data from the server, read it and send it to the serial monitor
  // 
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  //  If the server has disconnected, the client will also hang:
  if (!client.connected()) {
    Serial.println();
    Serial.println(“disconnect.”);
    client.stop();
    // No more processing :
    while (true);
  }
}
Colored by WayFarer™

 

 

3. Execution result

ing the sketch and opening the serial monitor and waiting for a while, the messages “Connecting …” and “Connected” are displayed and the response data is received from the Naver small dictionary service web server and output to the serial monitor. Then, at the end, it will successfully answer the “Korea” query with the “disconnect” message.

 

 
Copy the response message “<DOCTYPE …” to “</ html>” in the response message on the serial monitor window and save the file in UTF-8 code. You can see the search results.

 

 

 

4. Finish

Such as Ubiquitous Sensor Network, When you want to collect data from a large number of sensors, It would be good idea that periodically or when situation is changed send data to central web server after make each sensor node a web client. Of course, there may be a method of collecting data by requesting data transmission to each sensor node in the center, but it seems to be a matter of considering how effective it is.

 

Author : 과객

Source : http://blog.naver.com/msyang59/220147949576