// code for esp32 (spiffs html file is below this code)
/*
JK electronics
*/
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
// Replace with your network credentials
const char* ssid = "*****"; //Replace with your own SSID
const char* password = "*****"; //Replace with your own password
const int ledPin = 17;
String ledState;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Replaces placeholder with LED state value
String processor(const String& var){
Serial.println(var);
if(var == "LED_STATE"){
if(digitalRead(ledPin)){
ledState = "ON";
}
else{
ledState = "OFF";
}
Serial.print(ledState);
return ledState;
}
return String();
}
void setup(){
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Initialize SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/JKelectronics.html", String(), false, processor);
});
// Route for image heading
server.on("/logoJK.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/logoJK.jpg", "image/jpg");
});
// Route to set GPIO to HIGH
server.on("/led17On", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(ledPin, HIGH);
request->send(SPIFFS, "/JKelectronics.html", String(), false, processor);
});
// Route to set GPIO to LOW
server.on("/led17Off", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(ledPin, LOW);
request->send(SPIFFS, "/JKelectronics.html", String(), false, processor);
});
// Start server
server.begin();
}
void loop(){
}
// html code for making the webpage
<style>
.navigator {
color: rgb(35, 31, 67);
text-decoration:underline;
}
</style>
</h2>
</nav>
<br>
<br>
<div id="buttons" name="buttons">
<p>LightState: <strong>%LED_STATE%</strong></p>
<button><a href="/led17On" class="lights">Light on</a></button>
<button><a href="/led17Off" class="lights">Light off</a></button>
<style>
.lights {
color:rgb(230, 190, 138)
}
button{
background-color: rgb(35, 31, 67);
border-width: 100%;
border-radius: 20px;
border-style: double;
padding: 5%;
}
</style>
</div>
</body>
Xcode code: I've used the NSurl method to direct action to browser adres. ex: 192.168.*./led17on.
it works but it's also very slow..
Topic:
Programming Languages
SubTopic:
Swift
Tags: