You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.2 KiB
C++
131 lines
3.2 KiB
C++
|
|
// librarys
|
|
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
|
|
// network credentials
|
|
const char* ssid = "<your SSID>";
|
|
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[] = {23,22,19,18,5,4};
|
|
const int pinsout_length = 6;
|
|
boolean pinsout_stat[6];
|
|
|
|
const int pinsin[] = {21,17,16,0,2};
|
|
const int pinsin_length = 5;
|
|
boolean pinsin_stat[5];
|
|
|
|
// server port
|
|
WiFiServer server(80);
|
|
|
|
// HTTP request
|
|
String header;
|
|
|
|
// define timeout
|
|
unsigned long currentTime = millis();
|
|
unsigned long previousTime = 0;
|
|
const long timeoutTime = 2000;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
for (int i=0; i < pinsout_length; i++){
|
|
pinMode(pinsout[i], OUTPUT);
|
|
digitalWrite(pinsout[i], LOW);
|
|
}
|
|
|
|
// connect to Wi-Fi
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected.");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
server.begin();
|
|
}
|
|
|
|
void loop(){
|
|
WiFiClient client = server.available();
|
|
|
|
if (client) {
|
|
currentTime = millis();
|
|
previousTime = currentTime;
|
|
Serial.println("New Client.");
|
|
String currentLine = "";
|
|
while (client.connected() && currentTime - previousTime <= timeoutTime) {
|
|
currentTime = millis();
|
|
if (client.available()) {
|
|
char c = client.read();
|
|
Serial.write(c);
|
|
header += c;
|
|
if (c == '\n') {
|
|
if (currentLine.length() == 0) {
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-type:application/json");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
for (int i=0; i<pinsout_length; i++){
|
|
if (header.indexOf(String(i)+"/on") >= 0){
|
|
digitalWrite(pinsout[i], HIGH);
|
|
pinsout_stat[i] = true;
|
|
}else if (header.indexOf(String(i)+"/off") >= 0){
|
|
digitalWrite(pinsout[i], LOW);
|
|
pinsout_stat[i] = false;
|
|
}
|
|
}
|
|
client.print(pinsToJson());
|
|
client.println();
|
|
break;
|
|
} else {
|
|
currentLine = "";
|
|
}
|
|
} else if (c != '\r') {
|
|
currentLine += c;
|
|
}
|
|
}
|
|
}
|
|
header = "";
|
|
client.stop();
|
|
Serial.println("Client disconnected.");
|
|
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;
|
|
}
|