adding HTTP requests
This commit is contained in:
parent
dbf72a8753
commit
980fe6f711
@ -1,27 +1,30 @@
|
|||||||
|
|
||||||
// librarys
|
// librarys
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <ArduinoJson.h>
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
// network credentials
|
// network credentials
|
||||||
const char* ssid = "<your SSID>";
|
const char* ssid = "<your SSID>";
|
||||||
const char* password = "<your WiFi password>";
|
const char* password = "<your WiFi password>";
|
||||||
|
|
||||||
|
// URL for the HTTP requests
|
||||||
|
const String baseurl = "http://10.10.10.10:1880";
|
||||||
|
|
||||||
|
// define pins
|
||||||
|
const int pinsout[] = {34,35,32,33,25,26};
|
||||||
|
const int pinsout_length = 6;
|
||||||
|
boolean pinsout_stat[6];
|
||||||
|
|
||||||
|
const int pinsin[] = {27,14,12,13};
|
||||||
|
const int pinsin_length = 4;
|
||||||
|
boolean pinsin_stat[4];
|
||||||
|
|
||||||
// server port
|
// server port
|
||||||
WiFiServer server(80);
|
WiFiServer server(80);
|
||||||
|
|
||||||
// HTTP request
|
// HTTP request
|
||||||
String header;
|
String header;
|
||||||
|
|
||||||
// define pins
|
|
||||||
int pinsout[] = {26, 27};
|
|
||||||
int pinsout_length = 2;
|
|
||||||
boolean pinsout_stat[2];
|
|
||||||
|
|
||||||
int pinsin[] = {32, 33};
|
|
||||||
int pinsin_length = 2;
|
|
||||||
int pinsin_stat[2];
|
|
||||||
|
|
||||||
// define timeout
|
// define timeout
|
||||||
unsigned long currentTime = millis();
|
unsigned long currentTime = millis();
|
||||||
unsigned long previousTime = 0;
|
unsigned long previousTime = 0;
|
||||||
@ -73,29 +76,15 @@ void loop(){
|
|||||||
client.println();
|
client.println();
|
||||||
|
|
||||||
for (int i=0; i<pinsout_length; i++){
|
for (int i=0; i<pinsout_length; i++){
|
||||||
if (header.indexOf("GET /"+String(i)+"/on") >= 0){
|
if (header.indexOf(String(i)+"/on") >= 0){
|
||||||
digitalWrite(pinsout[i], HIGH);
|
digitalWrite(pinsout[i], HIGH);
|
||||||
pinsout_stat[i] = true;
|
pinsout_stat[i] = true;
|
||||||
break;
|
}else if (header.indexOf(String(i)+"/off") >= 0){
|
||||||
}else if (header.indexOf("GET /"+String(i)+"/off") >= 0){
|
|
||||||
digitalWrite(pinsout[i], LOW);
|
digitalWrite(pinsout[i], LOW);
|
||||||
pinsout_stat[i] = false;
|
pinsout_stat[i] = false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
client.print(pinsToJson());
|
||||||
client.print("{\"pinsout\":[");
|
|
||||||
for (int i=0; i<pinsout_length-1; i++){
|
|
||||||
client.print(String(pinsout_stat[i])+",");
|
|
||||||
}
|
|
||||||
client.print(pinsout_stat[pinsout_length]);
|
|
||||||
client.print("],\"pinsin\":[");
|
|
||||||
for (int i=0; i<pinsin_length-1; i++){
|
|
||||||
client.print(String(digitalRead(pinsin[i]))+",");
|
|
||||||
}
|
|
||||||
client.print(String(digitalRead(pinsin[pinsin_length])));
|
|
||||||
client.print("]}");
|
|
||||||
|
|
||||||
client.println();
|
client.println();
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
@ -111,4 +100,31 @@ void loop(){
|
|||||||
Serial.println("Client disconnected.");
|
Serial.println("Client disconnected.");
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i=0; i < pinsin_length; i++){
|
||||||
|
if (pinsin_stat[i] != digitalRead(pinsin[i])){
|
||||||
|
pinsin_stat[i] = digitalRead(pinsin[i]);
|
||||||
|
HTTPClient http;
|
||||||
|
http.begin(baseurl+"/"+i+"/"+pinsin_stat[i]);
|
||||||
|
http.addHeader("Content-Type", "application/json");
|
||||||
|
while(!http.POST(pinsToJson()));
|
||||||
|
http.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String pinsToJson(){
|
||||||
|
String payload = "{\"pinsout\":[";
|
||||||
|
for (int i=0; i<pinsout_length-1; i++){
|
||||||
|
payload+=String(pinsout_stat[i])+",";
|
||||||
|
}
|
||||||
|
payload+=String(pinsout_stat[pinsout_length-1]);
|
||||||
|
payload+="],\"pinsin\":[";
|
||||||
|
for (int i=0; i<pinsin_length-1; i++){
|
||||||
|
payload+=String(pinsin_stat[i])+",";
|
||||||
|
}
|
||||||
|
payload+=String(pinsin_stat[pinsin_length-1]);
|
||||||
|
payload+="]}";
|
||||||
|
return payload;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user