Pages

2017/06/03

ESP-01 - how to build a simple web server using Arduino - part 2 of 2

This is the second part of a 2 parts series on how to build a simple web server using ESP-01 and Arduino. The first part of this post is at http://wei48221.blogspot.tw/2017/06/esp-01-how-to-build-simple-web-server.html.

The Schematics

It's basically the same as that for the previous post except there is a 220 ohm resister and LED attached in series to GPIO2.


Note,

After testing, it's possible to power ESP-01 with the +3V power output from CP-2102. Both the uploading of Arduino sketch to the on-board flash memory of ESP-01 and wifi transmission work correctly.

Arduino Sketch

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server;
uint8_t pin_led = 2;  // External LED attached to GPIO2
char* ssid = "YOUR_SSID";
char* password = "YOUR_PASSWORD";

void setup()
{
  pinMode(pin_led, OUTPUT);
  WiFi.mode(WIFI_STA);      //We don't want the ESP to act as an AP
  WiFi.begin(ssid,password);
  Serial.begin(115200);
  while(WiFi.status()!=WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.on("/",[](){server.send(200,"text/plain","Hello World!");});
  server.on("/toggle",toggleLED);
  server.begin();
}

void loop()
{
  server.handleClient();
}

void toggleLED()
{
  digitalWrite(pin_led,!digitalRead(pin_led));
  server.send(204,"");
}

Source: https://github.com/acrobotic/Ai_Tips_ESP8266/tree/master/simple_webserver

Note,

- Be sure to replace "YOUR_SSID" and  "YOUR_PASSWORD" with the SSID and the Password for the wireless router used.

- It's important to use "WiFi.mode(WIFI_STA)" to set ESP-01 to act as WiFi client only. If it's not used, ESP-01 will act as both an AP and a WiFi-client when connecting to a WiFi Router and that can mess up other WiFi-devices on your network. 

Procedure for uploading the Arduino Sketch

Be sure to do the following before uploading the sketch to ESP-01.

- Provide external +3.3V to ESP-01 (make sure the +3.3V is connected to VCC, and the GND is connected to GND). Or, power ESP-01 with the +3V power output from CP-2102.

- Plug the CP-2102 module to the USB port of the computer used for firmware upload;

- First close the flash switch then the reset switch so that both GPIO0 and RST of ESP-01 are connected to GND;

- First open the reset switch then the flash switch so that bboth GPIO0 and RST are floating;

- Click on the upload icon to compile and upload the sketch.


Result

After the sketch is uploaded successfully, open Serial Monitor (Tools -> Serial Monitor). If nothing happens, close then open the reset switch to reset ESP-01.

The IP address assigned by the wireless router to ESP-01 will be display in the serial monitor.


Launch a web browser and enter the IP address obtained from the serial monitor of the Arduino IDE.


Enter "192.168.2.19/toggle" to toggle the external attached to GPIO2.

References:

ESP8266 Webserver Step-By-Step Using Arduino IDE (Mac and Windows)
https://www.youtube.com/watch?v=m2fEXhl70OY

Want to blink GPIO2 LED (built in) on ESP8266-01
https://github.com/esp8266/Arduino/issues/2627

How to Use the ESP8266-01 Pins
http://www.instructables.com/id/How-to-use-the-ESP8266-01-pins/

No comments:

Post a Comment