hello,i have this sketch on my arduino uno:
//zoomkat 3-17-12
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://xxx.xxx.x.xx:xxxx when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Configuração do acesso ao Broker MQTT
#define MQTT_AUTH false
#define MQTT_USERNAME “nxxxxxxxxxx”
#define MQTT_PASSWORD “2xxxxxxxxxxxxxa”
#define RELAY D2
//#include <Servo.h>
//Servo myservo; // create servo object to control a servo
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
192, 168, 1, 151 }; // ip in lan
byte MQTT_SERVER[] = { 192, 168, 1, 235 };
byte gateway[] = {
192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(8761); //server port
String readString;
EthernetClient ethClient;
PubSubClient client(ethClient);
//////////////////////
void setup(){
pinMode(4, OUTPUT); //pin selected to control
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println(“server LED test 1.0”); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>NEOS HOME</H1>");
// DIY buttons
// mousedown buttons
client.println("<br><input type=\"button1\" value=\"ON\" onmousedown=\"location.href = '/?off0';\"/>");
client.println("<input type=\"button1\" value=\"OFF\" onmousedown=\"location.href = '/?on0';\"/>");
client.println("<br><input type=\"button2\" value=\"ON\" onmousedown=\"location.href = '/?off1';\"/>");
client.println("<input type=\"button2\" value=\"OFF\" onmousedown=\"location.href = '/?on1';\"/>");
client.println("<br><input type=\"button3\" value=\"ON\" onmousedown=\"location.href = '/?off2';\"/>");
client.println("<input type=\"button3\" value=\"OFF\" onmousedown=\"location.href = '/?on2';\"/>");
client.println("<br><input type=\"button4\" value=\"ON\" onmousedown=\"location.href = '/?off3';\"/>");
client.println("<input type=\"button4\" value=\"OFF\" onmousedown=\"location.href = '/?on3';\"/>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("on0") >0) {
digitalWrite(4, HIGH);
Serial.println("Led on0");
}
if(readString.indexOf("off0") >0) {
digitalWrite(4, LOW);
Serial.println("Led off0");
}
if(readString.indexOf("on1") >0) {
digitalWrite(5, HIGH);
Serial.println("Led on1");
}
if(readString.indexOf("off1") >0) {
digitalWrite(5, LOW);
Serial.println("Led off1");
}
if(readString.indexOf("on2") >0) {
digitalWrite(6, HIGH);
Serial.println("Led on2");
}
if(readString.indexOf("off2") >0) {
digitalWrite(6, LOW);
Serial.println("Led off2");
}
if(readString.indexOf("on3") >0) {
digitalWrite(7, HIGH);
Serial.println("Led on3");
}
if(readString.indexOf("off3") >0) {
digitalWrite(7, LOW);
Serial.println("Led off3");
}
//clearing string for next read
readString="";
}
}
}
}
}
i have this arduino on my home lan,with a ethernet shield, and i can connect to him throug the internet any where i want…but i want to add this arduino on my home assistant!!!
i think the mqtt is the way!? i have read similar projects but they use esp…devices!!!no ethernet shields!? is there code to add mqtt to my sketch???i try to same things on the code but i don´t really understand nothing of this!!