element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum 4-pin Switch not working with MQTT publish in Arduino
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 9 replies
  • Subscribers 392 subscribers
  • Views 763 views
  • Users 0 members are here
  • mqtt
  • arduino
Related

4-pin Switch not working with MQTT publish in Arduino

Former Member
Former Member over 10 years ago

I have done a setup where if I press 4-pin switch,the LED connected to digital pin 7 with Arduino,will glow.

This is working fine when I connect it without MQTT publish.

Its working fine for the below code,

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, LOW);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, HIGH); 
  }

But When I use MQTT publish,its sending more than 300 publish messages to MQTT clients!!!! .I want it as,when I press the switch,the led has to glow and an MQTT message has to be published.But here it is like,eventhough I press once or twice,or evenif I dont press also,the message starts getting published the moment I upload the sketch into Arduino!

Code which is not working,

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
#include <util.h>
#include <ctype.h>

#define CLIENTID "ArduinoSensor"
#define TOPICNAME "switch/signal"
#define POLLINTERVAL 120000

#define PORT 80
//Connect to MQTT server
byte server1 [] = {X,X,X,X};
byte mac[] = {X,X,X,X};

IPAddress arduinoIP(X,X,X,X);
IPAddress dnsIP(X,X,X,X);
IPAddress gatewayIP(X,X,X,X);
IPAddress subnetIP(X,X,X,X);
EthernetServer server(PORT);

EthernetClient ethClient;
PubSubClient arduinoClient(server1, 8081, callback, ethClient);

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin =  7;      // the number of the LED pin

// variables will change:
int buttonState=0; 

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}

void setup() {
  Serial.begin(9600);
Ethernet.begin(mac, arduinoIP, dnsIP, gatewayIP, subnetIP);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); 
digitalWrite(ledPin, LOW); 

//Connect to the MQTT server
beginConnection() ;
}

//Initialise MQTT connection
void beginConnection() {
Serial.begin(9600);
//Ethernet.begin(mac) ;
int connRC = arduinoClient.connect(CLIENTID) ;
if (!connRC) {
Serial.println(connRC) ;
Serial.println("Could not connect to MQTT Server");
Serial.println("Please reset the arduino to try again");
delay(100);
exit(-1);
}
else {
Serial.println("Connected to MQTT Server...");

}}
void loop(){
  // read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == HIGH) {   
    digitalWrite(ledPin, LOW); 
  }
  else {
  digitalWrite(ledPin, HIGH);
  arduinoClient.publish(TOPICNAME, "Button A is pressed") ;
  }
  }

In Serial Monitor,I can see the output as below,

 

1

1

1

0

0

0

0

1

1

1

0

0

0

0

0

0

 

When switch is not pressed,the output is with 1s and 0s.But when there is switch pressed,0s are coming continuously until switch is pressed...

 

Not sure how to make this work...I need this little urgently..Please help me on this...

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago +1
    I dont think the issue is with the MQTTLibraries the issue with your code is your not testing to see if the button was already pressed and already sent a publish, currently based on the above sample it…
Parents
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    Hi,

     

    first of all please post the code selecting Syntax Highlighting as shown in the following image else it is impossible to copy and past the code in an ide and eventually see what's wrong to help you.

     

    Thank you. Enrico

     

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to balearicdynamics

    I have done that now...Please help me on my query...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to Former Member

    Thank you. Sure, happy if I can be helpful. Let me see.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to Former Member

    What is the PubSubClient library ? It seems not a standard library but there is an include on top of your non-working code. The same for the util.h include.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to Former Member

    Well, take a look and eventually test the code below. I have removed all the elements referring to the ethernet and the code seems working. So, the first thing I will check is if the button and led pins are used also by the ethernet hardware. In this case there is an overlap of the logic states that can create the issue. Let me know.

     

    #include <SPI.h>
    // #include <PubSubClient.h>
    #include <Ethernet.h>
    // #include <util.h>
    #include <ctype.h>
    
    
    #define CLIENTID "ArduinoSensor"
    #define TOPICNAME "switch/signal"
    #define POLLINTERVAL 120000
    
    
    #define PORT 80
    //Connect to MQTT server
    
    
    /*
    byte server1 [] = {X, X, X, X};
    byte mac[] = {X, X, X, X};
    
    
    IPAddress arduinoIP(X, X, X, X);
    IPAddress dnsIP(X, X, X, X);
    IPAddress gatewayIP(X, X, X, X);
    IPAddress subnetIP(X, X, X, X);
    */
    
    
    EthernetServer server(PORT);
    
    
    EthernetClient ethClient;
    
    
    // PubSubClient arduinoClient(server1, 8081, callback, ethClient);
    
    
    const int buttonPin = 2;    // the number of the pushbutton pin
    const int ledPin =  7;      // the number of the LED pin
    
    
    // variables will change:
    int buttonState = 0;
    
    
    void callback(char* topic, byte* payload, unsigned int length) {
      // handle message arrived
    }
    
    
    void setup() {
      Serial.begin(9600);
    //  Ethernet.begin(mac, arduinoIP, dnsIP, gatewayIP, subnetIP);
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin, INPUT);
      digitalWrite(ledPin, LOW);
    
    
      //Connect to the MQTT server
      beginConnection() ;
    }
    
    //Initialise MQTT connection
    void beginConnection() {
      Serial.begin(9600);
      //Ethernet.begin(mac) ;
    
    
    //  int connRC = arduinoClient.connect(CLIENTID) ;
    
    
    // For code testing only e.m.
    int connRC = 1;
    
    
      if (!connRC) {
        Serial.println(connRC) ;
        Serial.println("Could not connect to MQTT Server");
        Serial.println("Please reset the arduino to try again");
        delay(100);
        exit(-1);
      }
      else {
        Serial.println("Connected to MQTT Server...");
    
    
      }
    }
    
    
    void loop() {
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
      Serial.println(buttonState);
      if (buttonState == HIGH) {
        digitalWrite(ledPin, LOW);
      }
      else {
        digitalWrite(ledPin, HIGH);
    //    arduinoClient.publish(TOPICNAME, "Button A is pressed") ;
      }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to balearicdynamics

    PubSubclient library is standard one for MQTT library in Arduino.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to balearicdynamics

    Yes...I have already tried and posted as first code which does not have ethernet part.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 10 years ago in reply to balearicdynamics

    Yes...I have already tried and posted as first code which does not have ethernet part.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to Former Member

    Please give me the links where I can download the libraries so I can download them and take a look to the sources. Thanks.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube