Created
April 5, 2023 23:48
-
-
Save oliverswitzer/3f6720753c40d93fa110992abc12e221 to your computer and use it in GitHub Desktop.
A small script that starts a simple HTTP server on the Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFiNINA.h> | |
char ssid[] = ""; // your network SSID (name) | |
char pass[] = ""; // your network password | |
WiFiServer server(80); | |
void setup() { | |
Serial.begin(9600); | |
// attempt to connect to Wifi network: | |
while (WiFi.begin(ssid, pass) != WL_CONNECTED) { | |
Serial.println("Attempting to connect to SSID: " + String(ssid)); | |
delay(500); | |
} | |
Serial.println("Connected to wifi"); | |
// print Wi-Fi connection details | |
Serial.println("Connected to Wi-Fi network"); | |
Serial.print("Put this IP address in your browser: "); | |
Serial.println(WiFi.localIP()); | |
server.begin(); | |
} | |
void loop() { | |
WiFiClient client = server.available(); // listen for incoming clients | |
if (client) { // if you get a client, | |
Serial.println("New client connected"); | |
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 | |
Serial.write(c); // print it out the serial monitor | |
} | |
if (client.available() > 0) { | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println(); | |
client.println("<html>"); | |
client.println("<head>"); | |
client.println("<title>Arduino Nano IoT 33</title>"); | |
client.println("</head>"); | |
client.println("<body>"); | |
client.println("<h1>Hello, world!</h1>"); | |
client.println("</body>"); | |
client.println("</html>"); | |
delay(100); | |
client.stop(); | |
} | |
} | |
Serial.println("Client disconnected"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment