Skip to content

Instantly share code, notes, and snippets.

@vlazar-
Last active December 20, 2021 19:34
Show Gist options
  • Save vlazar-/4f195d9d67104b404c3cbff977b3c9dc to your computer and use it in GitHub Desktop.
Save vlazar-/4f195d9d67104b404c3cbff977b3c9dc to your computer and use it in GitHub Desktop.
MKR1000 - server led
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "";
char pass[] = "";
int stanje_veze = WL_IDLE_STATUS;
WiFiServer server(80);
int led = 6;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Pocetak programa!");
pinMode(led, OUTPUT);
while (stanje_veze != WL_CONNECTED) {
Serial.print("Pokusaj spajanja na: ");
Serial.println(ssid);
stanje_veze = WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress ip = WiFi.localIP();
Serial.print("Moja IP adresa je: ");
Serial.println(ip);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Spojio se novi klijent..");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("<p><strong>LED1: </strong>");
client.print("<a href=\"/H1\">UPALI</a> | ");
client.print("<a href=\"/L1\">UGASI</a></p><br>");
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H1")) {
digitalWrite(led, HIGH);
}
if (currentLine.endsWith("GET /L1")) {
digitalWrite(led, LOW);
}
}
}
client.stop();
Serial.println("Veza zavrsena");
}
}
<!-- https://davidjwatts.com/youtube/esp8266/esp-convertHTM.html# -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upravljanje led diodom</title>
</head>
<body style="background-color: darkgray;">
<h1 style="text-align: center;">UPRAVLJANJE SVJETLOM</h1>
<div style="text-align: center;">
<h2>LED1: </h2>
<a style="padding: 10px; background-color: seashell;" href="/H1">UPALI</a> |
<a style="padding: 10px; background-color: seashell;" href="/H1" href="/L2">UGASI</a>
</div>
</body>
</html>
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "";
char pass[] = "";
int stanje_veze = WL_IDLE_STATUS;
WiFiServer server(80);
int led = 6;
String html ="<!DOCTYPE html> <html lang=\"en\"> <head> <meta charset=\"UTF-8\"> <title>Upravljanje led diodom</title> </head> <body style=\"background-color: darkgray;\"> <h1 style=\"text-align: center;\">UPRAVLJANJE SVJETLOM</h1> <div style=\"text-align: center;\"> <h2>LED1: </h2> <a style=\"padding: 10px; background-color: seashell;\" href=\"/H1\">UPALI</a> | <a style=\"padding: 10px; background-color: seashell;\" href=\"/H1\" href=\"/L2\">UGASI</a> </div> </body> </html>";
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Pocetak programa!");
pinMode(led, OUTPUT);
while (stanje_veze != WL_CONNECTED) {
Serial.print("Pokusaj spajanja na: ");
Serial.println(ssid);
stanje_veze = WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress ip = WiFi.localIP();
Serial.print("Moja IP adresa je: ");
Serial.println(ip);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Spojio se novi klijent..");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print(html);
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
if (currentLine.endsWith("GET /H1")) {
digitalWrite(led, HIGH);
}
if (currentLine.endsWith("GET /L1")) {
digitalWrite(led, LOW);
}
}
}
client.stop();
Serial.println("Veza zavrsena");
}
}
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "";
char pass[] = "";
int stanje_veze = WL_IDLE_STATUS;
WiFiServer server(80);
int ledice[] = {6, 7};
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Pocetak programa!");
for (int i = 0; i < sizeof(ledice); i++) {
pinMode(ledice[i], OUTPUT);
}
while (stanje_veze != WL_CONNECTED) {
Serial.print("Pokusaj spajanja na: ");
Serial.println(ssid);
stanje_veze = WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress ip = WiFi.localIP();
Serial.print("Moja IP adresa je: ");
Serial.println(ip);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Spojio se novi klijent..");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print("<h1>Upravljanje LED diodama</h1>");
for (int i = 0; i < sizeof(ledice); i++) {
client.print("<p><strong>LED" + String(ledice[i]) + ": </strong>");
client.print("<a href=\"/H" + String(ledice[i]) + "\">UPALI</a> | ");
client.print("<a href=\"/L" + String(ledice[i]) + "\">UGASI</a></p><br>");
}
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
for (int i = 0; i < sizeof(ledice); i++) {
if (currentLine.endsWith("GET /H" + String(ledice[i]))) {
digitalWrite(ledice[i], HIGH);
}
if (currentLine.endsWith("GET /L" + String(ledice[i]))) {
digitalWrite(ledice[i], LOW);
}
}
}
}
client.stop();
Serial.println("Veza zavrsena");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment