Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 9553519

Browse files
author
nicolas leger
committed
HTTP Request push datas
1 parent dbc9cb9 commit 9553519

File tree

8 files changed

+140
-3
lines changed

8 files changed

+140
-3
lines changed

examples/Wifinfo/Wifinfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ extern unsigned long seconds;
124124
extern _sysinfo sysinfo;
125125
extern Ticker Tick_emoncms;
126126
extern Ticker Tick_jeedom;
127+
extern Ticker Tick_httpRequest;
127128

128129

129130
// Exported function located in main sketch
130131
// ===================================================
131132
void ResetConfig(void);
132133
void Task_emoncms();
133134
void Task_jeedom();
135+
void Task_httpRequest();
134136

135137
#endif
136138

examples/Wifinfo/Wifinfo.ino

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ Ticker red_ticker;
6363
Ticker Every_1_Sec;
6464
Ticker Tick_emoncms;
6565
Ticker Tick_jeedom;
66+
Ticker Tick_httpRequest;
6667

6768
volatile boolean task_1_sec = false;
6869
volatile boolean task_emoncms = false;
6970
volatile boolean task_jeedom = false;
71+
volatile boolean task_httpRequest = false;
7072
unsigned long seconds = 0;
7173

7274
// sysinfo data
@@ -129,6 +131,18 @@ void Task_jeedom()
129131
task_jeedom = true;
130132
}
131133

134+
/* ======================================================================
135+
Function: Task_httpRequest
136+
Purpose : callback of http request ticker
137+
Input :
138+
Output : -
139+
Comments: Like an Interrupt, need to be short, we set flag for main loop
140+
====================================================================== */
141+
void Task_httpRequest()
142+
{
143+
task_httpRequest = true;
144+
}
145+
132146
/* ======================================================================
133147
Function: LedOff
134148
Purpose : callback called after led blink delay
@@ -370,6 +384,11 @@ void ResetConfig(void)
370384
strcpy_P(config.jeedom.url, CFG_JDOM_DEFAULT_URL);
371385
//strcpy_P(config.jeedom.adco, CFG_JDOM_DEFAULT_ADCO);
372386

387+
// HTTP Request
388+
strcpy_P(config.httpReq.host, CFG_HTTPREQ_DEFAULT_HOST);
389+
config.httpReq.port = CFG_HTTPREQ_DEFAULT_PORT;
390+
strcpy_P(config.httpReq.path, CFG_HTTPREQ_DEFAULT_PATH);
391+
373392
config.config |= CFG_RGB_LED;
374393

375394
// save back
@@ -441,7 +460,7 @@ int WifiHandleConn(boolean setup = false)
441460
WiFi.begin(config.ssid);
442461
}
443462

444-
timeout = 25; // 25 * 200 ms = 5 sec time out
463+
timeout = 50; // 50 * 200 ms = 5 sec time out
445464
// 200 ms loop
446465
while ( ((ret = WiFi.status()) != WL_CONNECTED) && timeout )
447466
{
@@ -570,6 +589,7 @@ void setup()
570589
DebugF("Config size="); Debug(sizeof(_Config));
571590
DebugF(" (emoncms="); Debug(sizeof(_emoncms));
572591
DebugF(" jeedom="); Debug(sizeof(_jeedom));
592+
DebugF(" http request="); Debug(sizeof(_httpRequest));
573593
Debugln(')');
574594
Debugflush();
575595

@@ -773,6 +793,10 @@ void setup()
773793
// Jeedom Update if needed
774794
if (config.jeedom.freq)
775795
Tick_jeedom.attach(config.jeedom.freq, Task_jeedom);
796+
797+
// HTTP Request Update if needed
798+
if (config.httpReq.freq)
799+
Tick_httpRequest.attach(config.httpReq.freq, Task_httpRequest);
776800
}
777801

778802
/* ======================================================================
@@ -802,6 +826,9 @@ void loop()
802826
} else if (task_jeedom) {
803827
jeedomPost();
804828
task_jeedom=false;
829+
} else if (task_httpRequest) {
830+
httpRequest();
831+
task_httpRequest=false;
805832
}
806833

807834
// Handle teleinfo serial

examples/Wifinfo/config.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,11 @@ void showConfig()
201201
DebugF("key :"); Debugln(config.jeedom.apikey);
202202
DebugF("compteur :"); Debugln(config.jeedom.adco);
203203
DebugF("freq :"); Debugln(config.jeedom.freq);
204+
205+
DebuglnF("\r\n===== HTTP request");
206+
DebugF("host :"); Debugln(config.httpReq.host);
207+
DebugF("port :"); Debugln(config.httpReq.port);
208+
DebugF("path :"); Debugln(config.httpReq.path);
209+
DebugF("freq :"); Debugln(config.httpReq.freq);
204210
}
205211

examples/Wifinfo/config.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@
4242
#define CFG_JDOM_ADCO_SIZE 12
4343
#define CFG_JDOM_DEFAULT_PORT 80
4444
#define CFG_JDOM_DEFAULT_HOST "jeedom.local"
45-
#define CFG_JDOM_DEFAULT_URL "/jeedom/plugins/teleinfo/core/php/jeeTeleinfo.php"
45+
#define CFG_JDOM_DEFAULT_URL "/plugins/teleinfo/core/php/jeeTeleinfo.php"
4646
#define CFG_JDOM_DEFAULT_ADCO "0000111122223333"
4747

48+
#define CFG_HTTPREQ_HOST_SIZE 32
49+
#define CFG_HTTPREQ_PATH_SIZE 150
50+
#define CFG_HTTPREQ_DEFAULT_PORT 80
51+
#define CFG_HTTPREQ_DEFAULT_HOST "127.0.0.1"
52+
#define CFG_HTTPREQ_DEFAULT_PATH "/json.htm?type=command&param=udevice&idx=1&nvalue=0&svalue=%HCHP%;%HCHC%;0;0;%PAPP%;0"
4853

4954
// Port pour l'OTA
5055
#define DEFAULT_OTA_PORT 8266
@@ -79,6 +84,11 @@
7984
#define CFG_FORM_JDOM_ADCO FPSTR("jdom_adco")
8085
#define CFG_FORM_JDOM_FREQ FPSTR("jdom_freq")
8186

87+
#define CFG_FORM_HTTPREQ_HOST FPSTR("httpreq_host")
88+
#define CFG_FORM_HTTPREQ_PORT FPSTR("httpreq_port")
89+
#define CFG_FORM_HTTPREQ_PATH FPSTR("httpreq_path")
90+
#define CFG_FORM_HTTPREQ_FREQ FPSTR("httpreq_freq")
91+
8292
#define CFG_FORM_IP FPSTR("wifi_ip");
8393
#define CFG_FORM_GW FPSTR("wifi_gw");
8494
#define CFG_FORM_MSK FPSTR("wifi_msk");
@@ -112,6 +122,17 @@ typedef struct
112122
uint8_t filler[90]; // in case adding data in config avoiding loosing current conf by bad crc*/
113123
} _jeedom;
114124

125+
// Config for http request
126+
// 256 Bytes
127+
typedef struct
128+
{
129+
char host[CFG_HTTPREQ_HOST_SIZE+1]; // FQDN
130+
char path[CFG_HTTPREQ_PATH_SIZE+1]; // Path
131+
uint16_t port; // Protocol port (HTTP/HTTPS)
132+
uint32_t freq; // refresh rate
133+
uint8_t filler[24]; // in case adding data in config avoiding loosing current conf by bad crc*/
134+
} _httpRequest;
135+
115136
// Config saved into eeprom
116137
// 1024 bytes total including CRC
117138
typedef struct
@@ -126,7 +147,7 @@ typedef struct
126147
uint8_t filler[131]; // in case adding data in config avoiding loosing current conf by bad crc
127148
_emoncms emoncms; // Emoncms configuration
128149
_jeedom jeedom; // jeedom configuration
129-
uint8_t filler1[256]; // Another filler in case we need more
150+
_httpRequest httpReq; // HTTP request
130151
uint16_t crc;
131152
} _Config;
132153

examples/Wifinfo/data/index.htm.gz

22 Bytes
Binary file not shown.

examples/Wifinfo/webclient.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,62 @@ boolean jeedomPost(void)
239239
return ret;
240240
}
241241

242+
/* ======================================================================
243+
Function: HTTP Request
244+
Purpose : Do a http request
245+
Input :
246+
Output : true if post returned 200 OK
247+
Comments: -
248+
====================================================================== */
249+
boolean httpRequest(void)
250+
{
251+
boolean ret = false;
252+
253+
// Some basic checking
254+
if (*config.httpReq.host)
255+
{
256+
ValueList * me = tinfo.getList();
257+
// Got at least one ?
258+
if (me && me->next)
259+
{
260+
String url ;
261+
boolean skip_item;
262+
263+
url = *config.httpReq.path ? config.httpReq.path : "/";
264+
url += "?";
265+
266+
// Loop thru the node
267+
while (me->next) {
268+
// go to next node
269+
me = me->next;
270+
skip_item = false;
271+
272+
// Si Item virtuel, on le met pas
273+
if (*me->name =='_')
274+
skip_item = true;
275+
276+
// On doit ajouter l'item ?
277+
if (!skip_item)
278+
{
279+
String valName = String(me->name);
280+
if (valName == "HCHP")
281+
{
282+
url.replace("%HCHP%", me->value);
283+
}
284+
if (valName == "HCHC")
285+
{
286+
url.replace("%HCHC%", me->value);
287+
}
288+
if (valName == "PAPP")
289+
{
290+
url.replace("%PAPP%", me->value);
291+
}
292+
}
293+
} // While me
294+
295+
ret = httpPost( config.httpReq.host, config.httpReq.port, (char *) url.c_str()) ;
296+
} // if me
297+
} // if host
298+
return ret;
299+
}
300+

examples/Wifinfo/webclient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
boolean httpPost(char * host, uint16_t port, char * url);
3535
boolean emoncmsPost(void);
3636
boolean jeedomPost(void);
37+
boolean httpRequest(void);
3738

3839
#endif

examples/Wifinfo/webserver.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ void handleFormConfig(void)
182182
}
183183
config.jeedom.freq = itemp;
184184

185+
// HTTP Request
186+
strncpy(config.httpReq.host, server.arg("httpreq_host").c_str(), CFG_HTTPREQ_HOST_SIZE );
187+
strncpy(config.httpReq.path, server.arg("httpreq_path").c_str(), CFG_HTTPREQ_PATH_SIZE );
188+
itemp = server.arg("httpreq_port").toInt();
189+
config.httpReq.port = (itemp>=0 && itemp<=65535) ? itemp : CFG_HTTPREQ_DEFAULT_PORT ;
190+
itemp = server.arg("httpreq_freq").toInt();
191+
if (itemp>0 && itemp<=86400)
192+
{
193+
Tick_httpRequest.detach();
194+
Tick_httpRequest.attach(itemp, Task_httpRequest);
195+
} else {
196+
itemp = 0 ;
197+
}
198+
config.httpReq.freq = itemp;
199+
185200
if ( saveConfig() ) {
186201
ret = 200;
187202
response = "OK";
@@ -501,6 +516,12 @@ void getConfJSONData(String & r)
501516
r+=CFG_FORM_JDOM_ADCO; r+=FPSTR(FP_QCQ); r+=config.jeedom.adco; r+= FPSTR(FP_QCNL);
502517
r+=CFG_FORM_JDOM_FREQ; r+=FPSTR(FP_QCQ); r+=config.jeedom.freq;
503518

519+
r+=CFG_FORM_HTTPREQ_HOST; r+=FPSTR(FP_QCQ); r+=config.httpReq.host; r+= FPSTR(FP_QCNL);
520+
r+=CFG_FORM_HTTPREQ_PORT; r+=FPSTR(FP_QCQ); r+=config.httpReq.port; r+= FPSTR(FP_QCNL);
521+
r+=CFG_FORM_HTTPREQ_PATH; r+=FPSTR(FP_QCQ); r+=config.httpReq.path; r+= FPSTR(FP_QCNL);
522+
r+=CFG_FORM_HTTPREQ_FREQ; r+=FPSTR(FP_QCQ); r+=config.httpReq.freq;
523+
524+
504525
r+= F("\"");
505526
// Json end
506527
r += FPSTR(FP_JSON_END);

0 commit comments

Comments
 (0)