Thanks for the ideas. I have been through the PubSubClient Library examples and couldn't figure out how to add a web server to any of those, which is the problem. I just do not know how to do it.
The effect I need is really simple; click on any of the 4 links in the code and immediately publish the topic and topic payload as shown, while the webserver continues to run.
This is the code I have. I am now going in going in circles.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <PubSubClient.h>
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; // subnet mask
byte mac[] = { 0xAE, 0xAE, 0xBE, 0xAE, 0xFE, 0xAE }; // physical mac address
byte ip_webserver[] = { 192, 168, 1, 51 }; // ip of webserver
byte ip_mqtt_server[] = { 192, 168, 1, 120 }; // MQTT webserver
EthernetServer server(80);
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
IPAddress server2(ip_mqtt_server);
EthernetClient ethClient;
PubSubClient client(server2, 1883, callback, ethClient);
String readString;
void setup() {
Ethernet.begin(mac, ip_webserver, subnet, gateway);
server.begin();
client.setServer(server2, 1883); // MQTT
client.setCallback(callback); // MQTT
}
void reconnect() {
while (!client.connected()) {
if (client.connect("arduinoClient"))
{
if (readString.indexOf("?lrm-main-on") >0) { client.publish("groungfloor/livingroom/mainlight_on_off", "on "); }
if (readString.indexOf("?lrm-main-off") >0) { client.publish("groungfloor/livingroom/mainlight_on_off", "off"); }
if (readString.indexOf("?lrm-side-on") >0) { client.publish("groungfloor/livingroom/sidelights_on_off","on" ); }
if (readString.indexOf("?lrm-side-off") >0) { client.publish("groungfloor/livingroom/sidelights_on_off","off"); }
//client.subscribe("headlands/groundfloor/bedroom4/temperature_DS18B20");
}
else
{
Serial.print("failed, rc=");
Serial.println(" try again in 1 seconds");
// Wait 1 seconds before retrying
delay(1000);
}
}
}
void loop()
//1st open bracket
{
EthernetClient client = server.available();
// 2nd open bracket
if (client) {
// 3rd open bracket
while (client.connected()) {
//4th open bracket
if (client.available()) {
char c = client.read();
//read char by char HTTP request
// open bracket with closed bracket
if (readString.length() < 100) {
//store characters to string
readString += c;
Serial.print(c); }
//5th open bracket
if (c == '\n') { //if HTTP request has ended
////////////////////////////////////
// HTML STARTED //
////////////////////////////////////
////////////////// HTML header statements /////////////////////////
client.println("<html><head><meta name='viewport' content='width=device-width'><title></title><style>a{color:blue}.content1{width: 20px; float:right;}.content2{width: 20px; float:left;}</style></head><body><center><br/>");
//////////////////////////////////////////////////////
// LINKS to : Computer & Mobile, LAN & WAN //
//////////////////////////////////////////////////////
client.println("<b><font size=+2 color='#CC33FF'>House</font></b><br/><br/>");
//////// Room Door Switches ////////////////////////////////////
client.println("<b><font size=+1 color='#CC33FF'>Room Door Switches</font></b><br/>");
client.println("<a href='/?lrm-main-on' target='x2'>LRm, Main Light On</a> <a href='/?lrm-main-off' target='x2'>LRm, Main Light Off</a><br/>");
client.println("<a href='/?lrm-side-on' target='x2'>LRm, Side Light On</a> <a href='/?lrm-side-off' target='x2'>LRm, Side Light Off</a><br/>");
///////////////////////////////////////////////////////////////////////////////////
// CRITICAL .. below iframe code stops page refresh on the button click //
///////////////////////////////////////////////////////////////////////////////////
client.println("<iframe name=x2 style='display:none'></iframe>");
////////////////////////////////////
client.println("<p></p></center></body></html>");
////////////////////////////////////
// HTML FINISHED //
////////////////////////////////////
/////////////////////////////////////////////
// SWITCHING LOGIC BEGINS //
/////////////////////////////////////////////
// LRm Main Door Switch //
// main on
if (readString.indexOf("?lrm-main-on") >0){reconnect(); }
// main off
if (readString.indexOf("?lrm-main-off") >0){reconnect(); }
// LRm Side Door Switch //
// on button90, off button91
// side on
if (readString.indexOf("?lrm-side-on") >0){reconnect();}
// side off
if (readString.indexOf("?lrm-side-off") >0){reconnect();}
//clearing string for next read
readString="";
delay(1);
//stopping client
client.stop();
//5th bracket closed
}
//4th bracket closed
}
//3rd bracket closed
}
//2nd bracket closed
}
//1st bracket closed
}