Go Down

Topic: Arduino UNO--esp8266-01s--webserver (Read 38 times) previous topic - next topic

mostafarduino

hi there,
I have a request. I want to send temperature value(ds18b20) from Arduino uno to web page by esp8266. I use this library -->>    <ESP8266WiFi.h>. at the moment, I can send data for Arduino UNO by esp8266 and I'm controlling digital output's arduino successfully. I have two codes, one code for the esp and another for arduino. could you tell me how can I send data from arduino to web page?

my codes:

1. esp8266:

Code: [Select]

/*This code was created by H.I Electronic Tech channel
 * I hope this code helps you in your projects
 * if you want to support me, you could put a Like Share and subscribe to my channel
 * here the youtube video if you have any question put it in the comments
 *https://youtu.be/na-jyyerjHM
*/

#include <ESP8266WiFi.h>//be sure this line changes to the red when you select the ESP board

const char* SSID = "*****";       //your WiFi name
const char* password = "123456789";

// Setup GPIO2
int pinGPIO2 = 2; //To control Relay
int pinGPIO0 = 0; //To control LED
int LStatus = 0; //0=off,1=on,
int RStatus = 0; //0=off,1=on,

// Create the ESP Web Server on port 80
WiFiServer WebServer(80);

// Web Client
WiFiClient client;




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

  // Setup the GPIO2 LED Pin
  pinMode(pinGPIO2, OUTPUT);
  digitalWrite(pinGPIO2, LOW);
  // Setup the GPIO0 Relay Pin
  pinMode(pinGPIO0, OUTPUT);
  digitalWrite(pinGPIO2, LOW);
 
  // Connect to WiFi network
  Serial.println();
  WiFi.disconnect();//disconnect from any wifi network
  WiFi.mode(WIFI_STA);
  Serial.print("Connecting to ");
  Serial.println(SSID);
  WiFi.begin(SSID, password);//connect to the wifi

  while (WiFi.status() != WL_CONNECTED) {//while the esp8266 trying to connect with WiFi print dot(..)
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("Connected to WiFi");

  // Start the Web Server
  WebServer.begin();
  Serial.println("Web Server started");

  // Print the IP address
  Serial.print("You can connect to the ESP8266 at this URL: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}




void loop() {
  //if the WiFi restart or abruptly turned off the esp will reconnect with it.
  if(WiFi.status() != WL_CONNECTED){
    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    Serial.print("Reconnecting to ");
    Serial.println(SSID);
    WiFi.begin(SSID, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
    Serial.println("");
    Serial.println("Connected");
  }
 
  // Check if a user has connected
  client = WebServer.available();
  if (!client) {
    return;     //restart loop
  }

  // Wait until the user sends some data
  Serial.println("New User");
  while (!client.available()) {
    delay(1);
  }
// Read the first line of the request
  String request = client.readStringUntil('\r\n');
  Serial.println(request);
  client.flush();
 
  // Process the request:
  if (request.indexOf("/ONR") != -1) {
    digitalWrite(pinGPIO2, HIGH);
    Serial.print("0");
    RStatus = 1;
  }
  if (request.indexOf("/OFFR") != -1) {
    digitalWrite(pinGPIO2, LOW);
    Serial.print("1");
    RStatus = 0;
  }

// Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html; charset=UTF-8");
  client.println("");
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
client.println("<title>ESP8266 controller</title><style type=\"text/css\">body{background-color: gray;}.button {background: rgba(225,225,222,1);");
  client.println("font-size:23px;border-radius:15px;}.button:hover{box-shadow:0px 0px 8px 2px #333;cursor:pointer}; a {text-decoration: none;} ");
  client.println("</style>");
  client.println("</head>");
  client.println("<body>");
  client.println("</br></br><center><h1 style=\"text-align: center;\">Esp8266 WiFi controller</h1>");
  client.println("<a href=\"/\">Refresh Status</a>");
  //check the Relay status
  if (RStatus == 0) {
    client.print("<h1>Relay is Off</h1></br>");
    client.println("<h2>Turn the Relay</h2><a href=\"/ONR\"><button class=\"button\">ON</button></a></br>");
  }
  else if (RStatus == 1) {
    client.print("<h1>Relay is On</h1></br>");
    client.println("<h2>Turn the Relay</h2><a href=\"/OFFR\"><button class=\"button\">OFF</button></a></br>");
  }
client.println("</center></br>");
  client.println("</body>");
  client.println("</html>");

  delay(1);
  Serial.println("User disconnected");
  Serial.println("");

}





2. arduino's code:

Code: [Select]


void setup() {
  Serial.begin(9600); 
  pinMode(7, OUTPUT);
 
}

void loop() {
  if (Serial.available()) {
    char ser = Serial.read();
    switch (ser) {
      case '0':
      digitalWrite(7, HIGH);
      break;
      case '1':
      digitalWrite(7, LOW);
      break;
    }
  }

}



best regards











Go Up