adding arduino project
This commit is contained in:
parent
6744338bad
commit
dbf72a8753
114
ESP32-json-control/ESP32-json-control.ino
Normal file
114
ESP32-json-control/ESP32-json-control.ino
Normal file
@ -0,0 +1,114 @@
|
||||
|
||||
// librarys
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// network credentials
|
||||
const char* ssid = "<your SSID>";
|
||||
const char* password = "<your WiFi password>";
|
||||
|
||||
// server port
|
||||
WiFiServer server(80);
|
||||
|
||||
// HTTP request
|
||||
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
|
||||
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("GET /"+String(i)+"/on") >= 0){
|
||||
digitalWrite(pinsout[i], HIGH);
|
||||
pinsout_stat[i] = true;
|
||||
break;
|
||||
}else if (header.indexOf("GET /"+String(i)+"/off") >= 0){
|
||||
digitalWrite(pinsout[i], LOW);
|
||||
pinsout_stat[i] = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
break;
|
||||
} else {
|
||||
currentLine = "";
|
||||
}
|
||||
} else if (c != '\r') {
|
||||
currentLine += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
header = "";
|
||||
client.stop();
|
||||
Serial.println("Client disconnected.");
|
||||
Serial.println("");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user