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
ELClient.h
Go to the documentation of this file.
1
5#ifndef _EL_CLIENT_H_
6#define _EL_CLIENT_H_
7
8#include <avr/pgmspace.h>
9#include <HardwareSerial.h>
10#include <Arduino.h>
11#include "ELClientResponse.h"
12#include "FP.h"
13
14#define ESP_TIMEOUT 2000
16// Enumeration of commands supported by esp-link, this needs to match the definition in
17// esp-link!
18typedef enum {
27 //CMD_GET_INFO,
28
43} CmdName;
52};
54typedef struct {
55 uint8_t* buf;
56 uint16_t bufSize;
57 uint16_t dataLen;
58 uint8_t isEsc;
61// The ELClient class implements the basic protocol to communicate with esp-link using SLIP.
62// The SLIP protocol just provides framing, i.e., it delineates the start and end of packets.
63// The format of each packet is dictated by ELClient and consists of a 2-byte command, a 2-byte
64// count of arguments, a 4-byte callback addr, then the arguments, and finally 1 2-byte CRC.
65//
66// ELClient handles communication set-up and reset. It has to take a number of scenarios into
67// account, including simultaneous power-on reset of arduino and esp-link, spontaneous reset of
68// esp-link, and reset of arduino. Returning to a consistent state in all these cases is handled by
69// the Sync function and null commands.
70//
71// When ELClient starts it needs to send a Sync to esp-link. This clears all state and callbacks on
72// the esp-link side and then ELClient can install callbacks, etc. In order to catch the cases where
73// esp-link resets ELClient ensures that it sends periodic commands to esp-link and checks whether
74// esp-link responds with a "not synced" error, which indicates that it reset. If such an error
75// occurs ELClient starts with a fresh Sync. Unfortunately this has to be propagated up the
76// communication layers because the client may have to re-subscribe to MQTT messages or to certain
77// callbacks.
78class ELClient {
79 public:
80 // Create an esp-link client based on a stream and with a specified debug output stream.
81 ELClient(Stream* serial, Stream* debug);
82 // Create an esp-link client based on a stream with no debug output
83 ELClient(Stream* serial);
84
85 Stream* _debug;
87 //== Requests
88 // Start a request. cmd is the command to execute, value is either the address of a function
89 // to call with a response or a first argument to the command if there is no CB.
90 // Argc is the number of additional arguments
91 void Request(uint16_t cmd, uint32_t value, uint16_t argc);
92 // Add a an argument consisting of a data block to a request
93 void Request(const void* data, uint16_t len);
94 // Add a an argument consisting of a data block in flash to a request
95 void Request(const __FlashStringHelper* data, uint16_t len);
96 // Finish a request
97 void Request(void);
98
99 //== Responses
100 // Process the input stream, call this in loop() to dispatch call-back based responses.
101 // Callbacks on FP are invoked with an ElClientResponse pointer as argument.
102 // Returns the ELClientPacket if a non-callback response was received, typically this is
103 // used to create an ELClientResponse. Returns NULL if no response needs to be processed.
104 ELClientPacket *Process(void);
105 // Busy wait for a response with a timeout in milliseconds, returns an ELClientPacket
106 // if a response was recv'd and NULL otherwise. The ELClientPacket is typically used to
107 // create an ELClientResponse.
108 ELClientPacket *WaitReturn(uint32_t timeout=ESP_TIMEOUT);
109
110 //== Commands built-into ELClient
111 // Initialize and synchronize communication with esp-link with a timeout in milliseconds,
112 // and remove all existing callbacks. Registers the wifiCb and returns true on success
113 boolean Sync(uint32_t timeout=ESP_TIMEOUT);
114 // Request the wifi status
115 void GetWifiStatus(void);
116
117 // Callback for wifi status changes. This callback must be attached before calling Sync
119 // Callback to indicate protocol reset, typically due to esp-link resetting. The callback
120 // should call Sync and perform any other callback registration afresh.
121 void (*resetCb)();
123 //private:
124 Stream* _serial;
125 boolean _debugEn;
126 uint16_t crc;
128 uint8_t _protoBuf[128];
130 void init();
131 void DBG(const char* info);
133 void write(uint8_t data);
134 void write(void* data, uint16_t len);
135 uint16_t crc16Add(unsigned char b, uint16_t acc);
136 uint16_t crc16Data(const unsigned char *data, uint16_t len, uint16_t acc);
137};
138#endif // _EL_CLIENT_H_
Definitions for ELClientResponse.
#define ESP_TIMEOUT
Definition ELClient.h:14
CmdName
Definition ELClient.h:18
@ CMD_MQTT_SUBSCRIBE
Definition ELClient.h:31
@ CMD_GET_TIME
Definition ELClient.h:26
@ CMD_WEB_SETUP
Definition ELClient.h:38
@ CMD_CB_ADD
Definition ELClient.h:24
@ CMD_SOCKET_SEND
Definition ELClient.h:42
@ CMD_RESP_V
Definition ELClient.h:21
@ CMD_REST_SETHEADER
Definition ELClient.h:36
@ CMD_REST_SETUP
Definition ELClient.h:34
@ CMD_REST_REQUEST
Definition ELClient.h:35
@ CMD_WIFI_STATUS
Definition ELClient.h:23
@ CMD_WEB_DATA
Definition ELClient.h:39
@ CMD_MQTT_PUBLISH
Definition ELClient.h:30
@ CMD_SYNC
Definition ELClient.h:20
@ CMD_MQTT_SETUP
Definition ELClient.h:29
@ CMD_RESP_CB
Definition ELClient.h:22
@ CMD_SOCKET_SETUP
Definition ELClient.h:41
@ CMD_CB_EVENTS
Definition ELClient.h:25
@ CMD_NULL
Definition ELClient.h:19
@ CMD_MQTT_LWT
Definition ELClient.h:32
WIFI_STATUS
Definition ELClient.h:45
@ STATION_CONNECT_FAIL
Definition ELClient.h:50
@ STATION_IDLE
Definition ELClient.h:46
@ STATION_GOT_IP
Definition ELClient.h:51
@ STATION_NO_AP_FOUND
Definition ELClient.h:49
@ STATION_WRONG_PASSWORD
Definition ELClient.h:48
@ STATION_CONNECTING
Definition ELClient.h:47
Core Utility - Templated Function Pointer Class.
ELClientProtocol _proto
Definition ELClient.h:127
uint16_t crc16Add(unsigned char b, uint16_t acc)
Create CRC for a byte add it to an existing CRC checksum and return the result.
Definition ELClient.cpp:468
ELClientPacket * protoCompletedCb(void)
Process a received SLIP message.
Definition ELClient.cpp:31
uint8_t _protoBuf[128]
Definition ELClient.h:128
boolean Sync(uint32_t timeout=ESP_TIMEOUT)
Synchronize the communication between the MCU and the ESP.
Definition ELClient.cpp:521
void Request(void)
Finish the request.
Definition ELClient.cpp:322
uint16_t crc
Definition ELClient.h:126
ELClientPacket * Process(void)
Handle serial input.
Definition ELClient.cpp:115
void write(uint8_t data)
Send a byte.
Definition ELClient.cpp:154
void DBG(const char *info)
Send debug message over serial debug stream.
Definition ELClient.cpp:419
boolean _debugEn
Definition ELClient.h:125
ELClientPacket * WaitReturn(uint32_t timeout=ESP_TIMEOUT)
Wait for a response from ESP for a given timeout.
Definition ELClient.cpp:444
void(* resetCb)()
Definition ELClient.h:121
Stream * _debug
Definition ELClient.h:85
uint16_t crc16Data(const unsigned char *data, uint16_t len, uint16_t acc)
Create/add CRC for a data buffer.
Definition ELClient.cpp:493
FP< void, void * > wifiCb
Definition ELClient.h:118
void init()
Initialize ELClient protocol.
Definition ELClient.cpp:339
Stream * _serial
Definition ELClient.h:124
void GetWifiStatus(void)
Request WiFi status from the ESP.
Definition ELClient.cpp:564
API abstraction for a Function Pointers.
Definition FP.h:136
uint8_t isEsc
Definition ELClient.h:58
uint8_t * buf
Definition ELClient.h:55
uint16_t dataLen
Definition ELClient.h:57
uint16_t bufSize
Definition ELClient.h:56