hi im using at tiny 85 to monitor dc switches and to serial print with this
#include <SendOnlySoftwareSerial.h>
SendOnlySoftwareSerial myserial (4); // Tx pin
int sw1 = 0;
int sw2 = 1;
int sw3 = 2;
int sw4 = 3;
int sta1 = 0;
int sta2 = 0;
int sta3 = 0;
int sta4 = 0;
bool st1 = false;
bool st2 = false;
bool st3 = false;
bool st4 = false;
void setup () {
myserial.begin(9600);
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
}
void loop ()
{
sta1 = digitalRead(sw1);
sta2 = digitalRead(sw2);
sta3 = digitalRead(sw3);
sta4 = digitalRead(sw4);
if (sta1 == HIGH && st1 == false){
myserial.println("sw1 on");
st1 = true;
delay(1);
}
else if (sta1 == LOW && st1 == true){
myserial.println("sw1 off");
st1 = false;
delay(1);
}
if (sta2 == HIGH && st2 == false){
myserial.println("sw2 on");
st2 = true;
delay(1);
}
else if (sta2 == LOW && st2 == true){
myserial.println("sw2 off");
st2 = false;
delay(1);
}
if (sta3 == HIGH && st3 == false){
myserial.println("sw3 on");
st3 = true;
delay(1);
}
else if (sta3 == LOW && st3 == true){
myserial.println("sw3 off");
st3 = false;
delay(1);
}
if (sta4 == HIGH && st4 == false){
myserial.println("sw4 on");
st4 = true;
delay(1);
}
else if (sta4 == LOW&& st4 == true){
myserial.println("sw4 off");
st4 = false;
delay(1);
}
}
to esp8266 with local broker to turn leds on and off but its only working through mqtt but not with physical switches which are connected to at tiny 85. please help this is my mqt code
//ItKindaWorks - Creative Commons 2016
//github.com/ItKindaWorks
//
//Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
//
//ESP8266 Simple MQTT light controller
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
int LED3 = D2;
int LED4 = D4;
int LED2 = D3;
int LED1 = D5;
String data ;
// the number of the pushbutton pin
//EDIT THESE LINES TO MATCH YOUR SETUP
#define MQTT_SERVER "192.168.43.223"
const char* ssid = "kuro";
const char* password = "qwertyui";
//LED on ESP8266 GPIO2
char* lightTopic = "/test/light1";
char* lightTopic1 = "/test/light2";
char* lightTopic2 = "/test/light3";
char* lightTopic3 = "/test/light4";
WiFiClient wifiClient;
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
void setup() {
//initialize the light as an output and set to LOW (off)
Serial.begin(9600);
pinMode(LED3 , OUTPUT);
pinMode(LED4 , OUTPUT);
pinMode(LED1 , OUTPUT);
pinMode(LED2 , OUTPUT);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (Serial.available()>0) /* If data available at serial port, enter if loop */
{
data = Serial.readStringUntil('\n'); /* Read data present at serial port */
/* Print string with \r\n */
Serial.println(data); /* Print data received */
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
//reconnect if connection is lost
if (!client.connected() && WiFi.status() == 3) {reconnect();}
//maintain MQTT connection
client.loop();
//MUST delay to allow ESP8266 WIFI functions to run
delay(10);
}
void callback(char* topic, byte* payload, unsigned int length)
{
//convert topic to string to make it easier to work with
String topicStr = topic;
//Print out some debugging info
Serial.println("Callback update.");
Serial.print("Topic: ");
Serial.println(topicStr);
if(topicStr.equals(lightTopic)||Serial.available()){
if(payload[0] == '1' || data.equals ("sw1 on")){
digitalWrite(LED1 , HIGH);
}
else if (payload[0] == '0'|| data.equals ("sw1 off")){
digitalWrite(LED1 , LOW);
}
}
if(topicStr.equals(lightTopic1)||Serial.available()){
if(payload[0] == '1'|| data.equals ("sw2 on")){
digitalWrite(LED2 , HIGH);
}
else if (payload[0] == '0'|| data.equals ("sw2 off")){
digitalWrite(LED2 , LOW);
}
}
if(topicStr.equals(lightTopic2)||Serial.available()){
if(payload[0] == '1'|| data.equals ("sw3 on")){
digitalWrite(LED3 , HIGH);
}
else if (payload[0] == '0'|| data.equals ("sw3 off")){
digitalWrite(LED3 , LOW);
}
}
if(topicStr.equals(lightTopic3)||Serial.available()){
if(payload[0] == '1'|| data.equals ("sw4 on")){
digitalWrite(LED4 , HIGH);
}
else if (payload[0] == '0'|| data.equals ("sw4 off")){
digitalWrite(LED4 , LOW);
}
}
}
void reconnect() {
//attempt to connect to the wifi if connection is lost
if(WiFi.status() != WL_CONNECTED){
//debug printing
Serial.print("Connecting to ");
Serial.println(ssid);
//loop while we wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print out some more debug once connected
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//make sure we are connected to WIFI before attemping to reconnect to MQTT
if(WiFi.status() == WL_CONNECTED){
// Loop until we're reconnected to the MQTT server
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
//if connected, subscribe to the topic(s) we want to be notified about
if (client.connect((char*) clientName.c_str())) {
Serial.print("\tMTQQ Connected");
client.subscribe(lightTopic);
client.subscribe(lightTopic1);
client.subscribe(lightTopic2);
client.subscribe(lightTopic3);
}
//otherwise print failed for debugging
else{Serial.println("\tFailed."); abort();}
}
}
}
//generate unique name from MAC addr
String macToStr(const uint8_t* mac){
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5){
result += ':';
}
}
return result;
}