INPUT_PULLUP instead of OUTPUT?



  • I'm working with Mosquitto MQTT and trying to figure out what's going on.
    I really like my Wemos and don't want to burn one up. What would happen if I changed pinMode (ledPin, OUTPUT) to INPUT_PULLUP, then plugged in an LED with resistor from Positive rail to D5, then wrote digitalWrite(D5, HIGH)?
    Basically, I want the LED to turn on or off and stay that way until another digitalWrite.

    #include <EEPROM.h>
    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    #include <Wire.h>
    
    
    // Connect to the WiFi
    const char* ssid = "MYSSID";
    const char* password = "MYPASSWORD";
    const char* mqtt_server = "Mosquitto_broker";
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    
    const int chartsPin = D1; // LED on D1 of Wemos D1 mini
    const int buttonPin = D2; // Adafruit Capacitive Touch Toggle on D2
    const int officeaPin = D5; // LED on D5 for PSA
    const int officebPin = D6; // LED on D5 for PSB
    const int officecPin = D7; // LED on D5 for PSC
    const int officedPin = D8; // LED on D5 for PSD
    
    
    int buttonState = 0;
    int lastButtonState = 0;
    int buttonPushCounter = 0;   // counter for the number of button presses
    
    
    // ******************* RECEPTION ******************************
    void callback(char* topic, byte* payload, unsigned int length) {
    
    
      //  I FELT THIS WAS REDUNDANT SINCE WE TURN ON/OFF THE CHARTS LED
      //  IN THE LOOP
      //  **********************************************************
      //  if (strcmp(topic, "Charts/PS") == 0) {
      //    // Charts up!
      //    for (int i = 0; i < length; i++) {
      //      char receivedChar = (char)payload[i];
      //      if (receivedChar == '0')
      //        digitalWrite(chartsPin, HIGH);
      //      if (receivedChar == '1')
      //        digitalWrite(chartsPin, LOW);
      //    }
      //  }
      // ******************* EXAM ROOM A*****************************
      if (strcmp(topic, "Chaperone/PSA") == 0) {
        // Need a chaperone in Exam Room A
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '0')
            Serial.println (F("PSA LED ON"));
          digitalWrite(officeaPin, HIGH);
          if (receivedChar == '1')
            Serial.println (F("PSA LED OFF"));
          digitalWrite(officeaPin, LOW);
        }
      }
      // ******************* EXAM ROOM B *****************************
      if (strcmp(topic, "Chaperone/PSB") == 0) {
        // Need a chaperone in Exam Room B
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '1')
            Serial.println (F("PSB LED OFF"));
          digitalWrite(officebPin, HIGH);
          if (receivedChar == '0')
            Serial.println (F("PSB LED ON"));
          digitalWrite(officebPin, LOW);
        }
      }
      // ******************* EXAM ROOM C *****************************
      if (strcmp(topic, "Chaperone/PSC") == 0) {
        // Need a chaperone in Exam Room C
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '1')
            Serial.println (F("PSC LED OFF"));
          digitalWrite(officecPin, HIGH);
          if (receivedChar == '0')
            Serial.println (F("PSC LED ON"));
          digitalWrite(officecPin, LOW);
        }
      }
      // ******************* EXAM ROOM D *****************************
      if (strcmp(topic, "Chaperone/PSD") == 0) {
        // Need a chaperone in Exam Room D
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '1')
            Serial.println (F("PSD LED OFF"));
          digitalWrite(officedPin, HIGH);
          if (receivedChar == '0')
            Serial.println (F("PSD LED ON"));
          digitalWrite(officedPin, LOW);
        }
      }
    }
    // COMMENTED OUT SINCE WE'RE USING THE "STRCMP()" ABOVE
    //void callback(char* topic, byte* payload, unsigned int length) {
    //  Serial.print (F("Message arrived ["));
    //  Serial.print(topic);
    //  Serial.print("] ");
    //  for (int i = 0; i < length; i++) {
    //  char receivedChar = (char)payload[i];
    //  Serial.print(receivedChar);
    //  if (receivedChar == '0')
    //  digitalWrite(officeaPin, HIGH);
    //    if (receivedChar == '1')
    //      digitalWrite(officeaPin, LOW);
    //  }
    //  Serial.println();
    //}
    
    
    boolean reconnect() {  // **********************************************************
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print (F("Contacting MQTT server..."));
        // Attempt to connect
        if (client.connect("Reception")) {//assign a "client name".  Each wemos must have a unique name
          Serial.println (F("connected"));
          // ... SUBSCRIBE TO TOPICS
          client.subscribe("Chaperone/PSA");
          client.subscribe("Chaperone/PSB");
          client.subscribe("Chaperone/PSC");
          client.subscribe("Chaperone/PSD");
          return client.connected();
    
          Serial.print (F("Failed to connect. "));
          Serial.println (F(" Attempting connection again in 3 seconds"));
          // Wait 3 seconds before retrying
          delay(3000);
          return 0;
        }
      }
    }
    
    
    void setup()
    {
      {
        Serial.begin(9600);
        client.setServer(mqtt_server, 1883);
        client.setCallback(callback);
        pinMode(buttonPin, INPUT);
        pinMode(chartsPin, OUTPUT);
        pinMode(officeaPin, OUTPUT);
        pinMode(officebPin, OUTPUT);
        pinMode(officecPin, OUTPUT);
        pinMode(officedPin, OUTPUT);
      }
    
    
      // Connect to WiFinetwork
      Serial.println();
      Serial.println();
      Serial.print (F("Connecting to "));
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
      WiFi.mode(WIFI_STA);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        //Serial.begin(9600);
        Serial.print (F("."));
      }
      Serial.println (F(""));
      Serial.println (F("WiFi connected"));
      // Print the IP address
      Serial.print (F("Local IP: "));
      Serial.println(WiFi.localIP());
    }
    
    
    void loop() {
      if (!client.connected()) {
        reconnect();
      }
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
    
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState) {
        // if the state has changed, increment the counter
        if (buttonState == HIGH) {
          client.publish("Charts/PS", "0");
          Serial.println (F("Charts-up!"));
          digitalWrite(chartsPin, HIGH);
          // if the current state is HIGH then the button
          // went from off to on:
        } else {
          // if the current state is LOW then the button
          // went from on to off:
          client.publish("Charts/PS", "1");
          Serial.println (F("YAY!  No more charts."));
          digitalWrite(chartsPin, LOW);
        }
        // Using Adafruit Capacitive Touch Toggle Switch
        delay(50);// This seems to be the sweet spot for the delay time
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
      client.loop();
    }
    

    I'm focusing on Exam Room A; Once fixed, I will finish off the others
    What's happening is, the led comes on for a split second instead of just turning on and staying on until digitalWrite(d5, LOW).


Log in to reply
 

Looks like your connection to WEMOS Forum was lost, please wait while we try to reconnect.