1+ /*
2+ WiFi Web Server LED Blink
13
2- #include " Arduino.h"
3- #include < esp_wifi_types.h>
4- // #include "freertos/FreeRTOS.h"
5- #include " esp_wifi.h"
6- #include " esp_system.h"
7- #include " esp_event.h"
8- #include " esp_event_loop.h"
9- #include " nvs_flash.h"
10- #include " driver/gpio.h"
11- #include < string>
4+ A simple web server that lets you blink an LED via the web.
5+ This sketch will print the IP address of your WiFi Shield (once connected)
6+ to the Serial monitor. From there, you can open that address in a web browser
7+ to turn on and off the LED on pin 5.
128
9+ If the IP address of your shield is yourAddress:
10+ http://yourAddress/H turns the LED on
11+ http://yourAddress/L turns it off
1312
14- #define LED_PIN 5
13+ This example is written for a network using WPA encryption. For
14+ WEP or WPA, change the Wifi.begin() call accordingly.
1515
16- esp_err_t event_handler (void *ctx, system_event_t *event)
17- {
18- return ESP_OK;
19- }
16+ Circuit:
17+ * WiFi shield attached
18+ * LED attached to pin 5
2019
20+ created for arduino 25 Nov 2012
21+ by Tom Igoe
2122
22- void setup () {
23-
24-
25- pinMode (LED_PIN, OUTPUT);
26- digitalWrite (LED_PIN, LOW);
27-
28- nvs_flash_init ();
29- tcpip_adapter_init ();
30- ESP_ERROR_CHECK ( esp_event_loop_init (event_handler, NULL ) );
31- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
32- ESP_ERROR_CHECK ( esp_wifi_init (&cfg) );
33- ESP_ERROR_CHECK ( esp_wifi_set_storage (WIFI_STORAGE_RAM) );
34- ESP_ERROR_CHECK ( esp_wifi_set_mode (WIFI_MODE_STA) );
35- wifi_config_t sta_config;
36- sta_config.ap .authmode = WIFI_AUTH_WPA2_PSK;
37- sta_config.ap .channel = 0 ;
38-
39- sta_config.ap .ssid [0 ] = ' E' ;
40- sta_config.ap .ssid [1 ] = ' S' ;
41- sta_config.ap .ssid [2 ] = ' P' ;
42- sta_config.ap .ssid [3 ] = ' 3' ;
43- sta_config.ap .ssid [4 ] = ' 2' ;
44- sta_config.ap .ssid [5 ] = ' A' ;
45- sta_config.ap .ssid [6 ] = ' P' ;
46- sta_config.ap .ssid [7 ] = ' 0' ;
47- sta_config.ap .ssid_len = 7 ;
48-
49- sta_config.ap .password [0 ] = ' f' ;
50- sta_config.ap .password [1 ] = ' o' ;
51- sta_config.ap .password [2 ] = ' r' ;
52- sta_config.ap .password [3 ] = ' t' ;
53- sta_config.ap .password [4 ] = ' i' ;
54- sta_config.ap .password [5 ] = ' s' ;
55- sta_config.ap .password [6 ] = ' s' ;
56- sta_config.ap .password [7 ] = ' 1' ;
57- sta_config.ap .password [8 ] = ' 2' ;
58- sta_config.ap .password [9 ] = ' 3' ;
59- sta_config.ap .password [10 ] = 0 ;
60-
61-
62- sta_config.ap .ssid_hidden = 0 ;
63- sta_config.ap .max_connection = 1 ;
64- sta_config.ap .beacon_interval = 100 ;
65- ESP_ERROR_CHECK ( esp_wifi_set_config (WIFI_IF_STA, &sta_config) );
66- ESP_ERROR_CHECK ( esp_wifi_start () );
67- ESP_ERROR_CHECK ( esp_wifi_connect () );
68-
69- gpio_set_direction (GPIO_NUM_13, GPIO_MODE_OUTPUT);
70- }
23+ ported for sparkfun esp32
24+ 31.01.2017 by Jan Hendrik Berlin
25+
26+ */
27+
28+ #include " WiFi.h"
7129
72- void loop () {
30+ const char * ssid = " your_ssid" ;
31+ const char * password = " your_pwd" ;
7332
74- for (int i = 0 ; i < 2 ; i++) {
75- digitalWrite (LED_PIN, HIGH);
33+ WiFiServer server (80 );
34+
35+ void setup ()
36+ {
37+ Serial.begin (115200 );
38+ pinMode (5 , OUTPUT); // set the LED pin mode
39+ for (int i = 0 ; i < 5 ; i++) {
40+ digitalWrite (5 , HIGH);
7641 delay (1000 );
77- digitalWrite (LED_PIN , LOW);
42+ digitalWrite (5 , LOW);
7843 delay (1000 );
7944 }
8045
81- // vTaskDelay(300 / portTICK_PERIOD_MS);
46+ // We start by connecting to a WiFi network
47+
48+ Serial.println ();
49+ Serial.println ();
50+ Serial.print (" Connecting to " );
51+ Serial.println (ssid);
52+
53+ WiFi.begin (ssid, password);
8254
55+ while (WiFi.status () != WL_CONNECTED) {
56+ delay (500 );
57+ Serial.print (" ." );
58+ }
59+
60+ Serial.println (" " );
61+ Serial.println (" WiFi connected." );
62+ Serial.println (" IP address: " );
63+ Serial.println (WiFi.localIP ());
64+
65+ Serial.flush ();
66+
67+ server.begin ();
68+
69+ }
70+
71+ int value = 0 ;
72+
73+ void loop (){
74+
75+
76+ WiFiClient client = server.available (); // listen for incoming clients
77+
78+
79+
80+ if (client) { // if you get a client,
81+ Serial.println (" New Client." ); // print a message out the serial port
82+ String currentLine = " " ; // make a String to hold incoming data from the client
83+ while (client.connected ()) { // loop while the client's connected
84+ if (client.available ()) { // if there's bytes to read from the client,
85+ char c = client.read (); // read a byte, then
86+ Serial.write (c); // print it out the serial monitor
87+ if (c == ' \n ' ) { // if the byte is a newline character
88+
89+ // if the current line is blank, you got two newline characters in a row.
90+ // that's the end of the client HTTP request, so send a response:
91+ if (currentLine.length () == 0 ) {
92+ // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
93+ // and a content-type so the client knows what's coming, then a blank line:
94+ client.println (" HTTP/1.1 200 OK" );
95+ client.println (" Content-type:text/html" );
96+ client.println ();
97+
98+ // the content of the HTTP response follows the header:
99+ client.print (" Click <a href=\" /H\" >here</a> to turn the LED on pin 5 on.<br>" );
100+ client.print (" Click <a href=\" /L\" >here</a> to turn the LED on pin 5 off.<br>" );
101+
102+ // The HTTP response ends with another blank line:
103+ client.println ();
104+ // break out of the while loop:
105+ break ;
106+ } else { // if you got a newline, then clear currentLine:
107+ currentLine = " " ;
108+ }
109+ } else if (c != ' \r ' ) { // if you got anything else but a carriage return character,
110+ currentLine += c; // add it to the end of the currentLine
111+ }
112+
113+ // Check to see if the client request was "GET /H" or "GET /L":
114+ if (currentLine.endsWith (" GET /H" )) {
115+ digitalWrite (5 , HIGH); // GET /H turns the LED on
116+ }
117+ if (currentLine.endsWith (" GET /L" )) {
118+ digitalWrite (5 , LOW); // GET /L turns the LED off
119+ }
120+ }
121+ }
122+ // close the connection:
123+ client.stop ();
124+ Serial.println (" Client Disconnected." );
125+ }
83126}
0 commit comments