WebServer: Arduino UNO with WiFi ESP01

hi!,

I am in need of help. I can’t put the project to work. I removed this site

WebServer: Arduino UNO com WiFi ESP01 - Fernando K Tecnologia.

When I click the button to turn on the relay, nothing happens.

can you help me?

Thanks in advance.

Below the code:

To load on ESP01:

#include <ESP8266WiFi.h>

// Exchange for your network data
const char * ssid = "SSID";
const char * password = "PASS";
const char * ip = "192.168.0.177";

// Connection timeout
#define TIMEOUT 500
#define MAX_BUFFER 300

// Server on port 80 (http standard)
WiFiServer server (80);
// Buffer where the communication bytes will be written
uint8_t buffer [MAX_BUFFER];

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

// Send network information to connect
WiFi.disconnect ();
WiFi.mode (WIFI_STA);
WiFi.begin (ssid, password);

// Wait for the connection to the access point
while (WiFi.status ()! = WL_CONNECTED) {
delay (500);
}

// Configure the IP
IPAddress ipAddress;
ipAddress.fromString (ip);
WiFi.config (ipAddress, WiFi.gatewayIP (), WiFi.subnetMask ());

// Initialize the server
server.begin ();
}

void loop () {
// Check if someone has connected
WiFiClient client = server.available ();
if (! client) {
// If nobody connected just returns without doing anything
return;
}

// Marks the time the customer logged on and the amount
// bytes read
uint32_t connectedTime = millis ();
int bytesRead = 0;

// While the client is connected
while (client.connected ())
{
//Time now
uint32_t now = millis ();
// How much time has passed since connecting to the client
uint32_t ellapsed = now - connectedTime;

// If the time has passed the maximum time and has not read any bytes
if (ellapsed> TIMEOUT && bytesRead == 0)
{
  // Closes the connection with the client
  client.stop ();
  break;
}

int available = client.available ();

// If the client has bytes to be read
if (available)
{
  int bufferSize = available <MAX_BUFFER? available: MAX_BUFFER;
  int readCount = client.read (buffer, bufferSize);
  // Sends the bytes over the serial and increases the count of bytes read
  Serial.write (buffer, readCount);
  Serial.flush ();
  bytesRead + = readCount;
}

available = Serial.available ();

// If the serial has bytes to be read
if (available)
{
  int bufferSize = available <MAX_BUFFER? available: MAX_BUFFER;
  // Read the bytes
  Serial.readBytes (buffer, bufferSize);

  // If it is the byte that defines the end of the connection
  if (buffer [bufferSize-1] == 127)
  {
    client.write (buffer, bufferSize-1);
    // Send what hasn't been sent yet
    client.flush ();
    // Wait a while for the customer to receive
    delay (100);
    // Close the connection with the client and exit the 'while'
    client.stop ();
    break;
  }

  // Sends the bytes to the client
  client.write (buffer, bufferSize);
}
} // while (client.connected ())
} // loop

To load on Arduino Uno:

#define FIRST_PIN 2 // Pin where the first relay is
#define PINS_COUNT 5 // How many pins will be used

// Maintains the current state of the pins (HIGH or LOW)
int pinsStatus [PINS_COUNT];

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

// Initialize the pins
setupPins ();
}

void setupPins ()
{
// Place the pins that are connected to the relays as output
for (int i = 0; i <PINS_COUNT; i ++)
{
pinsStatus = LOW;
int pinNumber = FIRST_PIN + i;
pinMode (pinNumber, OUTPUT);
digitalWrite (pinNumber, pinsStatus);
}
}

void loop ()
{
// Checks for a new customer
if (Serial.available ())
{
// Read the request
char * request = readRequest ();

// If the request is not for the favicon
if (strstr (request, "favicon") == NULL)
{
 // We execute the action with the value passed in the request
 execute (getAction (request), getValue (request));

 // Sends the response to the customer
 sendResponse ();
}
else
{
 Serial.print (
 "HTTP / 1.1 404 Not Found \ r \ n"
 "Content-Length: 0 \ r \ n"
 "Connection: close \ r \ n"
 "\ r \ n");
}

Serial.write (127);
}
}

// Read the first line of the request
char * readRequest ()
{
bool currentLineIsBlank = true;
char request [50];
int i = 0;
bool firstLine = true;

while (true) {
while (! Serial.available ());
char c = Serial.read ();

// Only the first line of the request interests us
if (firstLine) {
 request = c;
 i ++;
}

if (c == '\ n') {
   // The last line of the request is a \ r \ n alone, after the \ r \ n of the previous line
   if (currentLineIsBlank) {
       // If you got here it is because the request has been read through
       break;
   }

   currentLineIsBlank = true;
   firstLine = false;
}
else if (c! = '\ r') {
   // If you read any characters that are not \ n and \ r it means that the line is not blank
   currentLineIsBlank = false;
}
}

return request;
}

// Send the HTML to the client
void sendResponse ()
{
// Send the HTTP header
Serial.print (
"HTTP / 1.0 200 OK \ r \ n"
"Content-Type: text / html; charset = UTF-8 \ r \ n"
"Connection: close \ r \ n"
"\ r \ n");

Serial.println ("<! DOCTYPE HTML>");
Serial.println ("<html>");
head (); // Send the HTML header
body (); // Send the HTML body
Serial.println ("</html>");
}

// Send the CSS to modify the appearance of the page
void head ()
{
Serial.println (F ("<head>"
"<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0'>"
"<style>"
   "body {"
       "text-align: center;"
       "font-family: sans-serif;"
       "font-size: 14px;"
   "}"
   "P{"
       "color: # 555;"
       "font-size: 12px;"
   "}"
   ".button {"
       "outline: none;"
       "display: block;"
       "border: 1px solid # 555;"
       "border-radius: 18px;"
       "width: 150px;"
       "height: 30px;"
       "margin: 10px;"
       "margin-left: auto;"
       "margin-right: auto;"
       "cursor: pointer;"
   "}"
   ".button_off {"
       "background-color: #FFF;"
       "color: # 555;"
   "}"
   ".button_on {"
       "background-color: # 2C5;"
       "color: #fff;"
   "}"
"</style>"
"</head>"));
}

// Displays sensor data and creates buttons
void body ()
{
Serial.println ("<body>");

String buttons = "";

// Create a button for each pin that has a relay
for (int i = 0; i <PINS_COUNT; i ++)
{
   buttons.concat (button (i));
}

Serial.println (buttons);
Serial.println ("</body>");
}

// Create a button with the appearance and action corresponding to the current state of the relay
String button (int number)
{
String label = String (number + 1);
String className = "button";
className + = pinsStatus [number] == HIGH? "button_on": "button_off";
String action = pinsStatus [number] == HIGH? "off": "on";
return "<button class = \" "+ className +" \ "onclick = \" location.href = '? "+ action +" = "+ label +"' \ ">" + label + "</button>" ;
}

// Returns the action that the client wants to perform (on off)
String getAction (char * request)
{
return getStringBetween (request, '?', '=');
}

// Returns the value (relay number) that the action will be performed
String getValue (char * request)
{
return getStringBetween (request, '=', '');
}

// Returns the string between the first 'start' character and the first 'end' character
String getStringBetween (char * input, char start, char end)
{
String str = "";
// returns the memory address of the 'start' character
char * c = strchr (input, start);

// If you didn't find the character
if (c == NULL)
{
 return "";
}

// Go to the next character
c ++;

// Until you reach the 'end' character or the end of the string
while (* c! = end && * c! = '\ 0')
{
 str + = * c;
 c ++;
}

return str;
}

// Executed the action next to the value (relay number)
void execute (String action, String value)
{
// If it is one of the two actions we expect
if (action == "on" || action == "off")
{
// The relays are numbered from 1, max the array starts from 0
// then we take out 1
int index = value.toInt () - 1;
// The pin number will be the index plus the pin number where the relays
// start. Relays must be in sequence from the starting pin (FIRST_PIN)
int pinNumber = FIR

To put your code in a code box, use the </> icon in the far left of the post tool bar and paste your code between the two bracket sets that appear.

To go back and put your code in a code box, in the bottom right of your post, select “more” and click modify. When the modify post opens, high light your code and click the </> in the far left of the post tool bar. This will put you code in code brackets. Then save the changes.