Servidor web simple
Fem el muntatge següent amb 4 Leds.
Copia el següent programa però no el carregues encara a la Nano.
/*
WiFi Web Server send data from Nano33IoT
Circuit:
* Nano 33 IoT
* 4 Leds
created for arduino 25 Nov 2012
by Tom Igoe
ported for sparkfun esp32
31.01.2017 by Jan Hendrik Berlin
Modificat per Carles Ferrando per canviar la ESP32 per la «Arduino Nano 33 IoT» , canviar els nombre de leds
i arreglar una mida la presentació de la pagina web.
*/
#include <WiFiNINA.h>
WiFiServer server(80);
#include "arduino_secrets.h"
//please enter your sensitive data in the Secret tab
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wi-Fi radio's status
int ledState = LOW; //ledState used to set the LED
unsigned long previousMillisInfo = 0; //will store last time Wi-Fi information was updated
unsigned long previousMillisLED = 0; // will store the last time LED was updated
const int intervalInfo = 5000; // interval at which to update the board information
//Definim els pins dels 4 leds
int pinLed1 = 9; // Definim el port del LED on escriure el valor lògic HIGH "Engegat" o LOW "Aturat"
int pinLed2 = 10; // Definim el port del LED on escriure el valor lògic HIGH "Engegat" o LOW "Aturat"
int pinLed3 = 11; // Definim el port del LED on escriure el valor lògic HIGH "Engegat" o LOW "Aturat"
int pinLed4 = 12; // Definim el port del LED on escriure el valor lògic HIGH "Engegat" o LOW "Aturat"
void setup() {
// declarem els pins dels Leds com eixides
pinMode(pinLed1, OUTPUT); // Aquest pin és una eixida
pinMode(pinLed2, OUTPUT); // Aquest pin és una eixida
pinMode(pinLed3, OUTPUT); // Aquest pin és una eixida
pinMode(pinLed4, OUTPUT); // Aquest pin és una eixida
delay(10);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial);
// set the LED as output
pinMode(LED_BUILTIN, OUTPUT);
// attempt to connect to Wi-Fi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
Serial.println("---------------------------------------");
server.begin();
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<BODY style='background-color:white'>");
client.println("<font style='color:green'>");
client.println("<font style='font-family:Liberation Serif'>");
client.println("<center>");
client.println("<H1 style='font-size:400%;'>Control remot de 4 LEDs connectats a la Nano 33 IoT</H1>");
client.println("<br />");
client.println("<br />");
// the content of the HTTP response follows the header:
client.print("<br><span style=font-size:4em>Prem <a href=\"/1\">BOTO1</a> per engegar el led 1 i aturar els altres.<br></span>");
client.print("<br><span style=font-size:4em>Prem <a href=\"/2\">BOTO2</a> per engegar el led 2 i aturar els altres.<br></span>");
client.print("<br><span style=font-size:4em>Prem <a href=\"/3\">BOTO3</a> per engegar el led 3 i aturar els altres.<br></span>");
client.print("<br><span style=font-size:4em>Prem <a href=\"/4\">BOTO4</a> per engegar el led 4 i aturar els altres.<br></span>");
client.print("<br><span style=font-size:4em>Prem <a href=\"/5\">BOTO5</a> per aturar tots els Leds.<br></span>");
client.println("</BODY>");
client.println("</HTML>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /1" , "GET /2", "GET /3", "GET /4", "GET /5":
if (currentLine.endsWith("GET /1")) {
digitalWrite(pinLed1, HIGH); // GET /1 turns the LED on
digitalWrite(pinLed2, LOW);
digitalWrite(pinLed3, LOW);
digitalWrite(pinLed4, LOW);
}
if (currentLine.endsWith("GET /2")) {
digitalWrite(pinLed1, LOW);
digitalWrite(pinLed2, HIGH); // GET /2 turns the LED on
digitalWrite(pinLed3, LOW);
digitalWrite(pinLed4, LOW);
}
if (currentLine.endsWith("GET /3")) {
digitalWrite(pinLed1, LOW);
digitalWrite(pinLed2, LOW);
digitalWrite(pinLed3, HIGH); // GET /3 turns the LED on
digitalWrite(pinLed4, LOW);
}
if (currentLine.endsWith("GET /4")) {
digitalWrite(pinLed1, LOW);
digitalWrite(pinLed2, LOW);
digitalWrite(pinLed3, LOW);
digitalWrite(pinLed4, HIGH); // GET /4 turns the LED on
}
if (currentLine.endsWith("GET /5")) {
digitalWrite(pinLed1, LOW); // GET /5 turns the LED off
digitalWrite(pinLed2, LOW); // GET /5 turns the LED off
digitalWrite(pinLed3, LOW); // GET /5 turns the LED off
digitalWrite(pinLed4, LOW); // GET /5 turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}// final programa
|
Guarda-lo amb el nom Nano_Wifi4Leds dins la carpeta Arduino del teu ordinador.
Crea dins la carpeta Nano_Wifi4Leds un fitxer que es diga arduino_secrets.h amb les dues ratlles següents de contingut:
#define SECRET_SSID "El nom de la xarxa wifi o SSID" #define SECRET_PASS "La contrasenya de la wifi" |
Es veurà així:
Carrega el programa ara i mira el monitor serie:
Ara entra en eixa adreça IP mostrada amb l'ordinador:
O amb el telèfon:
Ara el següent pas es controlar un robot/automatisme amb el teu mòbil.