Raport: Ministerul Educaţiei, Culturii Și Cercetării Al Republicii Moldova Universitatea Tehnică A Moldovei

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Ministerul Educaţiei, Culturii și Cercetării al Republicii

Moldova
Universitatea Tehnică a Moldovei

RAPORT
Lucrarea de laborator nr.4
la”B.T.D.”

A efectuat: V. Juc
st. gr. CR-182

A verificat: V. Lascu

Chişinău -2019
Tema: Configurarea placii NodeMCU

Programul care prmite scanarea retelelor WIFI


#include "ESP8266WiFi.h"

void setup() {
Serial.begin(115200);

WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);

Serial.println("Setup done");
}
void loop() {
Serial.println("scan start");

// WiFi.scanNetworks will return the number of networks found


int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
}
}
Serial.println("");

// Wait a bit before scanning again


delay(5000);
}
Programul de configuare a placii in regim de web-server

#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";


const char* password = "your-password";

// Create an instance of the server


// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);

// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);

// Connect to WiFi network


Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server


server.begin();
Serial.println("Server started");

// Print the IP address


Serial.println(WiFi.localIP());
}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data


Serial.println("new client");
while (!client.available()) {
delay(1);
}

// Read the first line of the request


String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
val = 0;
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
} else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);

client.flush();

// Prepare the response


String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE
HTML>\r\n<html>\r\nGPIO is now ";
s += (val) ? "high" : "low";
s += "</html>\n";

// Send the response to the client


client.print(s);
delay(1);
Serial.println("Client disonnected");

Concluzie:
In aceasta lucrare de la borator am invatat cum se configureaza placa Node MCU in special
modalitatile ei de conectare la retelele wifi.

You might also like