friendev EtherDune TCP/IP library
HTTPClientDemo_REST.ino
Go to the documentation of this file.
1 // EtherDune HTTPClient class demo
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Demonstrates the HTTPClient class by connecting to a weather REST service
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
19 
22 
23 #include <ACross.h>
24 #include <FlowScanner.h>
25 #include <HTTPClient.h>
26 
27 
28 #define AC_LOGLEVEL 6
29 #include <ACLog.h>
30 ACROSS_MODULE("HTTPClient demo");
31 
32 #define DHCP_ENABLE true
33 
34 static const uint8_t CS_PIN = 10; //Put here what pin you are using for your ENC28J60's chip select
35 static MACAddress_P mymac = { 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64 };
36 
37 //the following parameters will be ignored and not linked if DHCP_ENABLE is set to true.
38 static IPAddress_P gatewayIP = { 192, 168, 1, 1 };
39 static IPAddress_P myIP = { 192, 168, 1, 33 };
40 static IPAddress_P netmask = { 255, 255, 255, 0 };
41 static IPAddress_P dns = { 8, 8, 8, 8 };
42 
43 
44 #if DHCP_ENABLE
45 
46 #include <DHCP.h>
47 DHCP dhcp;
48 
49 #else
50 
51 #endif
52 
53 
54 DEFINE_FLOWPATTERN(temperaturePattern, "\"temp\":%7[^,]"); // look for a string like this "temp":12.321, and extract the values
55 
56 class MyHTTPClient : public HTTPClient
57 {
58 
59 public:
60 
61  char temp[8]; //temporary buffer to hold the captured temperature string
62 
63  void start()
64  {
65  request(F("api.openweathermap.org"), F("/data/2.5/weather?q=Madrid,es&units=metric"));
66  //see http://openweathermap.org/current
67  }
68 
69  void onHeaderReceived(uint16_t len, const byte* data)
70  {
71  //Serial.write(data, len);
72  }
73 
74  void onResponseReceived()
75  {
76  ACTRACE("HTTP status=%d", statusCode);
77  if (statusCode != HTTP_RESPONSE_OK)
78  {
79  Serial.print(F("Server error, status code="));
80  Serial.println(statusCode);
81  terminate();
82  }
83 
84  }
85 
86  void onBodyBegin()
87  {
88  scanner.setPattern(temperaturePattern);
89  }
90 
91  void onBodyReceived(uint16_t len, const byte* data)
92  {
93  ACTRACE("HTTP bytes received=%d", len);
94 
95  byte* buf = (byte*)data;
96 
97  if (temperaturePattern.signaled)
98  return;
99 
100  if (scanner.scan(&buf, &len, temp))
101  {
102  float t = atof(temp);
103 
104  Serial.print(F("The temperature is "));
105  Serial.print(t);
106  Serial.println('C');
107  close();
108  return;
109  }
110 
111  }
112  void onResponseEnd()
113  {
114  ACTRACE("HTTP session end");
115  }
116 
117 }http;
118 
119 
120 unsigned long waitTimer = 0;
121 
122 void setup()
123 {
124  Serial.begin(115200);
125  ACross::init();
126  Serial.println(F("EtherDune HTTPClient sample"));
127  Serial.print(F("Free RAM: ")); Serial.println(ACross::getFreeRam());
128 
129  Serial.println(F("Press any key to start..."));
130 
131  while (!Serial.available());
132 
134 
135  if (!net::begin(CS_PIN))
136  {
137  ACERROR("failed to start EtherDune");
138  Serial.println(F("failed to start EtherDune"));
139  }
140 
141  ACINFO("waiting for link...");
142 
143  while (!net::isLinkUp());
144 
145  ACINFO("link is up");
146 
147 
148 #if DHCP_ENABLE
149  Serial.println("Obtaining DHCP configuration...");
150 
151  if (!dhcp.dhcpSetup())
152  {
153  Serial.println(F("DHCP setup failed"));
154  ACross::halt(1);
155  }
156 
157  Serial.println(F("DHCP setup OK"));
158 
159  Serial.println(F("DHCP config:"));
160  Serial.print(F("Local IP: "));
161  Serial.println(net::localIP.toString());
162  Serial.print(F("Network mask: "));
163  Serial.println(net::netmask.toString());
164  Serial.print(F("Gateway IP: "));
165  Serial.println(net::gatewayIP.toString());
166  Serial.print(F("DNS IP: "));
167  Serial.println(net::dnsIP.toString());
168 #else
169  net::localIP = myIP;
172  net::dnsIP = dns;
173 
174 #endif
175 
176  http.start();
177 
178  Serial.println("Connecting...");
179 
180 }
181 
182 
183 
184 void loop()
185 {
186  net::loop();
187 
188 }
189 
190 /// \endcond
#define MACAddress_P
Definition: inet.h:242
void request(const String &hostName, const String &resource, uint16_t port=80)
Starts a new HTTP request.
Definition: HTTPClient.cpp:68
virtual void onResponseReceived()
Called immediately after the first line is received and there is a status code, e.g.
Definition: HTTPClient.cpp:28
#define IPAddress_P
helper macro to store an IP address in PROGMEM
Definition: inet.h:170
virtual void onBodyBegin()
Called when all HTTP headers have been received and the body of the response is about to arrive...
Definition: HTTPClient.cpp:50
static IPAddress_P gatewayIP
EtherDune DHCP Service.
Definition: DHCP.h:122
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
static IPAddress netmask
Subnet mask.
static IPAddress gatewayIP
IP address of the gateway in this network.
ACROSS_MODULE("ARP")
static const uint8_t CS_PIN
bool dhcpSetup()
Attempts to configure the IP settings: local IP, subnet mask, gateway and DNS via DHCP...
Definition: DHCP.cpp:50
static MACAddress_P mymac
static bool isLinkUp()
Determines whether the network link is ready
Definition: ENC28J60.cpp:306
static const uint16_t HTTP_RESPONSE_OK
The request has succeeded.
Definition: HTTPConstants.h:77
void setup()
Provides an easy way to query a web server.
Definition: HTTPClient.h:41
static IPAddress dnsIP
IP address of the DNS server to use.
DHCP dhcp
static MACAddress localMAC
Ethernet MAC address.
static IPAddress_P myIP
virtual void onBodyReceived(uint16_t len, const byte *data)
Called once for each fragment of the body that is received.
Definition: HTTPClient.cpp:44
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
void terminate()
Immediately shuts down the socket and makes it available for a new task.
Definition: TCPSocket.cpp:183
static IPAddress localIP
IP address of this application.
virtual void onHeaderReceived(uint16_t len, const byte *data)
Called once for each fragment of the header portion of the response
Definition: HTTPClient.cpp:38
virtual void onResponseEnd()
Called after all the body has been received
Definition: HTTPClient.cpp:32
void close()
Attempts to gracefully close a connection.
Definition: TCPSocket.cpp:194
static IPAddress_P netmask
void loop()