Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
ELClientRest Class Reference

#include <ELClientRest.h>

Public Member Functions

 ELClientRest (ELClient *e)
 Constructor for ELClientRest.
 
int begin (const char *host, uint16_t port=80, boolean security=false)
 Initialize communication to a REST server.
 
void request (const char *path, const char *method, const char *data=NULL)
 Send request to REST server.
 
void request (const char *path, const char *method, const char *data, int len)
 Send request to REST server.
 
void get (const char *path, const char *data=NULL)
 Send GET request to REST server.
 
void post (const char *path, const char *data)
 Send POST request to REST server.
 
void put (const char *path, const char *data)
 Send PUT request to REST server.
 
void del (const char *path)
 Send DELETE request to REST server.
 
uint16_t getResponse (char *data, uint16_t maxLen)
 Retrieve response.
 
uint16_t waitResponse (char *data, uint16_t maxLen, uint32_t timeout=DEFAULT_REST_TIMEOUT)
 Wait for the response.
 
void setUserAgent (const char *value)
 Set user agent of header.
 
void setContentType (const char *value)
 Set content type of header.
 
void setHeader (const char *value)
 Set generic header content.
 

Detailed Description

Definition at line 39 of file ELClientRest.h.

Constructor & Destructor Documentation

◆ ELClientRest()

ELClientRest::ELClientRest ( ELClient e)

Constructor for ELClientRest.

ELClientRest(ELClient *e)

Parameters
ePointer to ELClient structure
Example

Definition at line 26 of file ELClientRest.cpp.

Member Function Documentation

◆ begin()

int ELClientRest::begin ( const char *  host,
uint16_t  port = 80,
boolean  security = false 
)

Initialize communication to a REST server.

begin(const char* host, uint16_t port, boolean security)

Initialize communication to a remote server, this communicates with esp-link but does not open a connection to the remote server.

Parameters
hostHost to be connected. Can be a URL or an IP address in the format of xxx.xxx.xxx.xxx .
portPort to be used to send/receive packets. Port MUST NOT be 80, 23 or 2323, as these ports are already used by EL-CLIENT on the ESP8266
securityFlag if secure connection should be established
Warning
Port MUST NOT be 80, 23 or 2323, as these ports are already used by EL-CLIENT on the ESP8266. Max 4 connections are supported!
Example
int err = rest.begin("www.timeapi.org");
if (err != 0)
{
Serial.print("REST begin failed: ");
Serial.println(err);
while(1) ;
}
ESP esp & Serial
Definition demo.ino:9

Definition at line 78 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ del()

void ELClientRest::del ( const char *  path)

Send DELETE request to REST server.

del(const char* path)

Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
Example
no example code yet

Definition at line 217 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ get()

void ELClientRest::get ( const char *  path,
const char *  data = NULL 
)

Send GET request to REST server.

get(const char* path, const char* data)

Warning
The received data might not be null-terminated.
Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
dataPointer to data buffer
Example
// Request /utc/now from the previously set-up server
rest.get("/utc/now");

Definition at line 159 of file ELClientRest.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getResponse()

uint16_t ELClientRest::getResponse ( char *  data,
uint16_t  maxLen 
)

Retrieve response.

getResponse(char* data, uint16_t maxLen)

Checks if a response from the remote server was received, returns the HTTP status code or 0 if no response (may need to wait longer)

Parameters
dataPointer to buffer for received packet
maxLenSize of buffer for received packet. If the received packet is larger than the buffer, the received packet will be truncated.
Returns
uint16_t Size of received packet or number of sent bytes or 0 if no response
Example
#define BUFLEN 266
void loop()
{
// process any callbacks coming from esp_link
esp.Process();
void loop()
{
// process any callbacks coming from esp_link
esp.Process();
// if we're connected make an HTTP request
{
// Request /utc/now from the previously set-up server
rest.get("/utc/now");
char response[BUFLEN];
memset(response, 0, BUFLEN);
uint16_t code = rest.waitResponse(response, BUFLEN);
if(code == HTTP_STATUS_OK)
{
Serial.println("ARDUINO: GET successful:");
Serial.println(response);
}
else
{
Serial.print("ARDUINO: GET failed: ");
Serial.println(code);
}
delay(1000);
}
}
}
@ HTTP_STATUS_OK
REST rest & esp
Definition demo.ino:11
boolean wifiConnected
Definition demo.ino:13
void loop()
Definition demo.ino:73
#define BUFLEN
Definition rest.ino:71

Definition at line 321 of file ELClientRest.cpp.

Here is the caller graph for this function:

◆ post()

void ELClientRest::post ( const char *  path,
const char *  data 
)

Send POST request to REST server.

post(const char* path, const char* data)

Warning
The received data must be null-terminated.
Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
dataPointer to data buffer
Example
// Generate a fake value starting from 100 going up to 300
if (solarValue == 300)
{
solarValue = 100;
}
String solarValString = String(solarValue);
const char *solarValChar = solarValString.c_str();
// Reserve a buffer for sending the data
char path_data[BUFLEN];
// Copy the path and API key into the buffer
sprintf(path_data, "%s", "/update?api_key=");
sprintf(path_data + strlen(path_data), "%s", api_key);
// Copy the field number and value into the buffer
// If you have more than one field to update,
// repeat and change field1 to field2, field3, ...
sprintf(path_data + strlen(path_data), "%s", "&field1=");
sprintf(path_data + strlen(path_data), "%s", solarValChar);
// Send PUT request to thingspeak.com
rest.post(path_data,"");
float solarValue
char * api_key

Definition at line 192 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ put()

void ELClientRest::put ( const char *  path,
const char *  data 
)

Send PUT request to REST server.

put(const char* path, const char* data)

Warning
The received data must be null-terminated.
Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
dataPointer to data buffer
Example
no example code yet

Definition at line 206 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ request() [1/2]

void ELClientRest::request ( const char *  path,
const char *  method,
const char *  data,
int  len 
)

Send request to REST server.

request(const char* path, const char* method, const char* data, int len)

Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
methodREST method, allowed values are "GET", "POST", "PUT" or "DELETE"
dataPointer to data buffer
lenSize of data buffer
Example
no example code yet

Definition at line 112 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ request() [2/2]

void ELClientRest::request ( const char *  path,
const char *  method,
const char *  data = NULL 
)

Send request to REST server.

request(const char* path, const char* method, const char* data)

The data must be null-terminated.

Parameters
pathPath that extends the URL of the REST request (command or data for the REST server)
methodREST method, allowed values are "GET", "POST", "PUT" or "DELETE"
dataPointer to data buffer
Example
no example code yet

Definition at line 141 of file ELClientRest.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setContentType()

void ELClientRest::setContentType ( const char *  value)

Set content type of header.

setContentType(const char* value)

If no content type is set, it defaults to "x-www-form-urlencoded"

Parameters
valueContent type
Example
no example code yet

Definition at line 248 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ setHeader()

void ELClientRest::setHeader ( const char *  value)

Set generic header content.

setHeader(const char* value)

If no generic header is set, it defaults to an empty string

Parameters
valueHeader content
Example
no example code yet

Definition at line 229 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ setUserAgent()

void ELClientRest::setUserAgent ( const char *  value)

Set user agent of header.

setUserAgent(const char* value)

If no user agent is set, it defaults to "esp-link"

Parameters
valueUser agent
Example
no example code yet

Definition at line 267 of file ELClientRest.cpp.

Here is the call graph for this function:

◆ waitResponse()

uint16_t ELClientRest::waitResponse ( char *  data,
uint16_t  maxLen,
uint32_t  timeout = DEFAULT_REST_TIMEOUT 
)

Wait for the response.

waitResponse(char* data, uint16_t maxLen, uint32_t timeout)

Wait for the response from the remote server for time_out, returns the HTTP status code, 0 if no response (may need to wait longer)

Warning
Blocks the Arduino code for 5 seconds! not recommended to use. Received packet is NOT null-terminated
Parameters
dataPointer to buffer for received packet
maxLenSize of buffer for received packet. If the received packet is larger than the buffer, the received packet will be truncated.
timeout(optional) Timout in milli seconds to wait for a response, defaults to 5000ms
Returns
uint16_t Size of received packet or number of sent bytes or 0 if no response
Example
#define BUFLEN 266
void loop()
{
// process any callbacks coming from esp_link
esp.Process();
// if we're connected make an HTTP request
{
// Request /utc/now from the previously set-up server
rest.get("/utc/now");
char response[BUFLEN];
memset(response, 0, BUFLEN);
uint16_t code = rest.waitResponse(response, BUFLEN);
if(code == HTTP_STATUS_OK)
{
Serial.println("ARDUINO: GET successful:");
Serial.println(response);
}
else
{
Serial.print("ARDUINO: GET failed: ");
Serial.println(code);
}
delay(1000);
}
}

Definition at line 374 of file ELClientRest.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files: