0% found this document useful (0 votes)
333 views

ESP8266

This document contains code to control an LED connected to an Arduino using a web server hosted on an ESP8266 WiFi module. The ESP8266 is configured as a web server on port 80. When a request is received, the code checks for a command parameter and turns the LED on or off accordingly by updating its status array. It generates an HTML response page with the updated status and sends it back to the client before closing the connection.

Uploaded by

Turnitin Report
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
333 views

ESP8266

This document contains code to control an LED connected to an Arduino using a web server hosted on an ESP8266 WiFi module. The ESP8266 is configured as a web server on port 80. When a request is received, the code checks for a command parameter and turns the LED on or off accordingly by updating its status array. It generates an HTML response page with the updated status and sends it back to the client before closing the connection.

Uploaded by

Turnitin Report
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <SoftwareSerial.

h>

#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 5, make TX Arduino line is pin 6.

int LED=12; //LED PIN

int itsONled[] = {0,0}; // LED STATUS ARRAY eg- ON or OFF at startup.

void setup()

pinMode(LED, OUTPUT);

Serial.begin(9600);

esp8266.begin(115200); // your esp's baud rate might be different

sendData("AT+RST\r\n",2000,DEBUG); // reset module

//sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as Access point mode

sendData("AT+CWMODE=1\r\n",1000,DEBUG); // configure as Wireless Station mode

sendData("AT+CWJAP=\"yourssid\",\"pass\"\r\n", 6000, DEBUG); //Put Your SSID and password if activate as Station mode else
comment down the line
sendData("AT+CIFSR\r\n",2000,DEBUG); // get ip address

sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections

sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80

void loop()

if(esp8266.available()) // check if the esp is sending a message

if(esp8266.find("+IPD,"))

// subtract 48 because the read() function returns

// the ASCII decimal value and 0 (the first decimal number) starts at 48

int connectionId = esp8266.read()-48;

//To read the url sent by the client

String msg;

esp8266.find("?");

delay(100);

msg = esp8266.readStringUntil(' ');

String command1 = msg.substring(0);

// HTML START

String webpage = "<html><head><title>ESP8266 WEB SWITCH</title>";

webpage += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>.button {background-color:


orange;border: none;color: white;padding: 15px 32px;text-align: center;display: inline-block;font-size: 16px;} .centre {text-align:
center;}</style>";

webpage += "</head><body class=\"centre\"><h1 class=\"centre\">ESP8266 WEB SWITCH</h1>";

//COMMANDS TO TURN ON or OFF LED RECEIVE BY WEB

if (command1 == "T"){

if (itsONled[1] == 1)

digitalWrite(LED, LOW);
itsONled[1] = 0;

webpage += "<p>LED STATUS OFF</p>";

else

digitalWrite(LED, HIGH);

itsONled[1] = 1;

webpage += "<p>LED STATUS ON</p>";

webpage += "<a class=\"button\" href=\"?T\">TAP</a></body></html>";

String cipSend = "AT+CIPSEND=";

cipSend += connectionId;

cipSend += ",";

cipSend +=webpage.length();

cipSend +="\r\n";

sendData(cipSend,500,DEBUG);

sendData(webpage,500,DEBUG);

//BELOW THIS LINE CLOSE THE CONNECTION

String closeCommand = "AT+CIPCLOSE=";

closeCommand+=connectionId; // append connection id

closeCommand+="\r\n";

sendData(closeCommand,500,DEBUG);

//PROGRAM TO SEND COMMAND TO ESP8266

void sendData(String command, const int timeout, boolean debug)

{
esp8266.print(command); // send the read character to the esp8266

long int time = millis();

while( (time+timeout) > millis())

while(esp8266.available())

// The esp has data so display its output to the serial window

Serial.write(esp8266.read());

}
//Make sure to subscribe Technomekanics:)

String ssid = "Simulator Wifi"; // SSID to connect to

String password = ""; // Our virtual wifi has no password

String host = "api.thingspeak.com"; // Open Weather Map API

const int httpPort = 80;

String uri = "/update?api_key=AATWMZDY4ZQM0ZJU&field1=";

int setupESP8266(void) {

// Start our ESP8266 Serial Communication

Serial.begin(115200); // Serial connection over USB to computer

Serial.println("AT"); // Serial connection on Tx / Rx port to ESP8266

delay(10); // Wait a little for the ESP to respond

if (!Serial.find("OK")) return 1;
// Connect to 123D Circuits Simulator Wifi

Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");

delay(10); // Wait a little for the ESP to respond

if (!Serial.find("OK")) return 2;

// Open TCP connection to the host:

Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);

delay(50); // Wait a little for the ESP to respond

if (!Serial.find("OK")) return 3;

return 0;

void anydata(void) {

int temp = map(analogRead(A0),20,358,-40,125);

// Construct our HTTP call

String httpPacket = "GET " + uri + String(temp) + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";

int length = httpPacket.length();

// Send our message length

Serial.print("AT+CIPSEND=");

Serial.println(length);

delay(10); // Wait a little for the ESP to respond if (!Serial.find(">")) return -1;

// Send our http request

Serial.print(httpPacket);

delay(10); // Wait a little for the ESP to respond


if (!Serial.find("SEND OK\r\n")) return;

void setup() {

setupESP8266();

void loop() {

anydata();

delay(10000);

You might also like