1

I am working on a home automation project in which I am trying to use Star Topology, where one esp8266 acts as a server and can be accessed from web browser to receive commands from the user, while two or more esp8266 acts as clients which are connected to relays. Upon the request of user, server esp8266 sends command to the respective client to trigger the relay and the client esp8266 replies with the current status of the relay.

Connections isn't a problem so I am keeping those things aside. So far

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "AndroidAP";
const char *password = "qvdo2440";

ESP8266WebServer server(80);
String webPage = "";

void handleRoot() {
  server.send(200, "text/html", webPage);
}

void setup() {
  webPage += "<h1>ESP8266 Web Server</h1>";
  webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button>
</a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";

  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println();
  while (WiFi.status() != WL_CONNECTED) {
   delay(500);
    Serial.print(".");
  }
  Serial.print("Configuring access point...");
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

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

This is the code for server side. I cannot think of how to program for client side. Any help would be appreciable.

And yes. I have been through many articles, tried out many things which i found on the internet. But nothing could help me. So i am here for few more suggestions with better elaboration.

  • 2
    Make each ESP8266 totally autonomous. They received commands as page requests (REST) and return results. Make a Web Server on a PC, with a page that links all the individual ESP8266 for command and results display. – user31481 Sep 15 '17 at 11:14
  • I got your point. N it seems absolutely fine for my type of project. Can i do the same thing on my web host instead of making a server on my pc? And it would be very helpful if you could share some references for the same. – Rohit Mathur Sep 15 '17 at 11:51
  • 2
    ESP8266 APs can only handle 4 incoming clients, so they aren't good stand-alone servers. – dandavis Sep 15 '17 at 19:30
  • dandavis --> what should be my approach to achieve the completion of my project. Hope the vision of my project is clear. There are going to be n no. of nodes – Rohit Mathur Sep 16 '17 at 5:00
  • 2
    I'm voting to close this question as off-topic because to OP really just wants someone to write the code for him – Code Gorilla Sep 18 '17 at 7:20
1

I agree with most of 'Look Alterno's comment. So here it is explained in more detail.

Each ESP runs a web server. Each ESP will handle

  • GET /root/status
  • POST /root/change?device=x,state=y

When a client requests /root/status information on each relay managed by the device will be displayed. For satellite ESPs this will mean displaying live data and for the central on it will mean displaying the last known state (it could also request an update at this time). If you use an array then presenting this information will be quite simple.

When a client posts a change to a device then the satellite ESPs will change the relay state and the central ESP will forward the message onto the satellite. It should probably also request a status update to check that the change has occurred.

If you steer clear of hardcoding things and use arrays rather than variable1, variable2, variable3 you should be able to make this expandable. The biggest problem you will have is registering the satellites with the central device, but you could get around that for now by adding a configure page which had a HTML form that allowed you to enter the IP address of the central device and enter the name of the satellite and the name of the things connected to the relay.

Personally I wouldn't use a PC web server, yes it gives you more processing power, but I don't think you really need it for this, I was surprised how good they were. If you have issues look at using an ESP32 instead of a ESP8266 for the central node.

I hope that helps

  • can u please share some references so that i could learn how to achieve this? Im just 14. I am not aware of much of programming – Rohit Mathur Sep 15 '17 at 12:00
  • www.w3schools.com/html would be the best place to start. You already know how to convert a html file into string, you've done that already. Look at forms for posting examples and GET is the default that you know how to do. – Code Gorilla Sep 15 '17 at 12:10
  • 1
    I used a NodeMCU as a WebServer, but it fails when it comes to big files (.js, images ...). Later I discovered MQTT and ditched HTTP/HTML for good/ever. No more CSS, HTML, HTTP, JavaScript. No more server configuration. Just pure, simple and plain MQTT. – user31481 Sep 15 '17 at 12:53
  • Code Gorilla --> i have learned the html. I am aware of GET & POST method of html form. But i cant conclude this to implement this thing to my project. – Rohit Mathur Sep 16 '17 at 5:02
  • 1
    Also i have found a great visual tool "Node-Red". I believe Node-Red, MQTT, Esp8266 will accomplish my needs. Correct me if i am wrong – Rohit Mathur Sep 16 '17 at 5:07
-1
#include<ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
String i;
long newvar;
String httpurl;
String TheHiddenAnswerOfClient;
HTTPClient http;
String SendWithAnswer(String IPcache, String monmessagecache)
{
  httpurl = "http://";
  httpurl += IPcache;
  httpurl += monmessagecache;
  http.begin(httpurl);
  http.GET();
  TheHiddenAnswerOfClient = (http.getString());
  http.end();
  return TheHiddenAnswerOfClient;
}


void setup() {
  // put your setup code here, to run once:
  i = "";
  newvar = 0;
  Serial.begin(9600);
  WiFi.disconnect();
  delay(3000);
  Serial.println("START");
  WiFi.begin("electromodule", "electromodule@456");
  while ((!(WiFi.status() == WL_CONNECTED)))
  {
    delay(300);
    Serial.print("..");
  }
  Serial.println("Connected");
}

void loop() {
  // put your main code here, to run repeatedly:
  i = (SendWithAnswer("192.168.0.2", "YES"));
  Serial.println("Before Clear");
  Serial.println(i);
  i.remove(0, 25);
  i.remove(i.length() - 11, 11);
  Serial.println("After clear");
  Serial.println(i);
  newvar = i.toInt();
  Serial.println((2000 + newvar));
}
  • This is the codefor clent side.the ip of the server will be provided in the code so that the server and clent are connected .Also include client header file in the program – shubhangi agarwal Jul 25 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.