Disconnecting issues after adding any pinMode(xxxx, INPUT); and other random events

Hi all

I’ve been trying to adapt some sketches to use Blynk stubbornly for may sleepless nights.
I’ve read a lot and found most of the answers (that lead to more reading and a lot more questions) but this has me totally stumped.

Using my nodeMCU if I upload the Blynk example “Hello World” everything works perfect. If I add an input pinmode the serial monitor (9600baud) just displays garbled symbols (?)5⸮1⸮D⸮) while the on board led flashes ever 5s or so or if it manages to bootup it disconnects every 10s.

here is the sketch



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
WidgetLCD lcd(V1);

#define beta 4090 // from your thermistor's datasheet
#define resistance 33
int ledPin = 15;
int relayPin = 16;
int aPin = 8;
int bPin = 7;
int buttonPin = 6;
int analogPin = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
pinMode(buttonPin, INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(0, 0, "Hello"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  lcd.print(12, 0, "World");


}

void loop()
{
  Blynk.run();
}



void loop()

If I comment out the

{
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
//pinMode(aPin, INPUT);
//pinMode(bPin, INPUT);
//pinMode(buttonPin, INPUT);
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

It works??

Im trying to create a virtual Blynk version of this sketch, I was going to gut the hardware code so I may not need the pinMode(xxx, INPUT); section but it would be better if I could just mirror the hardware in Blynk. I read that I would need to move everything out of the void loop() for it to work with blynk, this sounds like a deal breaker for my coding experience.

#include <LiquidCrystal.h>
#define beta 4090 // from your thermistor's datasheet
#define resistance 33
// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4-7 on pins 5-2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int ledPin = 15;
int relayPin = 16;
int aPin = 8;
int bPin = 7;
int buttonPin = 6;
int analogPin = 0;
float setTemp = 20.0;
float measuredTemp;
char mode = 'C';
boolean override = false;
float hysteresis = 0.25;
void setup()
{
lcd.begin(2, 20);
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
pinMode(buttonPin, INPUT);
lcd.clear();
}
void loop()
{
static int count = 0;
measuredTemp = readTemp();
if (digitalRead(buttonPin))
{
override = ! override;
updateDisplay();
delay(500); // debounce
}
int change = getEncoderTurn();
setTemp = setTemp + change * 0.1;
if (count == 1000)
{
updateDisplay();
updateOutputs();
count = 0;
}
count ++;
}
int getEncoderTurn()
{
// return -1, 0, or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(aPin);
int newB = digitalRead(bPin);
if (newA != oldA || newB != oldB)
{
// something has changed
if (oldA == LOW && newA == HIGH)
{
result = -(oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
float readTemp()
{
long a = analogRead(analogPin);
float temp = beta / (log(((1025.0 * resistance / a) - 33.0) / 33.0) +
(beta / 298.0)) - 273.0;
return temp;
}
void updateOutputs()
{
if (override || measuredTemp < setTemp - hysteresis)
{
digitalWrite(ledPin, HIGH);
digitalWrite(relayPin, HIGH);
}
else if (!override && measuredTemp > setTemp + hysteresis)
{
digitalWrite(ledPin, LOW);
digitalWrite(relayPin, LOW);
}
}
void updateDisplay()
{
lcd.setCursor(0,0);
lcd.print("Actual: ");
lcd.print(adjustUnits(measuredTemp));
lcd.print(" o");
lcd.print(mode);
lcd.print(" ");
lcd.setCursor(0,1);
if (override)
{
lcd.print(" OVERRIDE ON ");
}
else
{
lcd.print("Set: ");
lcd.print(adjustUnits(setTemp));
lcd.print(" o");
lcd.print(mode);
lcd.print(" ");
}
}
float adjustUnits(float temp)
{
if (mode == 'C')
{
return temp;
}
else
{
return (temp * 9) / 5 + 32;
}
}

Any help would save at least some of my hair.

I’m a bit confused by this:

The numbers (15, 16, 8, 7, 6 & 0) are GPIO numbers used by your board.

Node MCU Dev boards don’t have GPIO 6, 7 or 8 available as pins on the board. At first I assumed that you were confusing GPIO numbers with the “D” numbers that are screen-printed onto the board, but these boards don’t have D15 or D16, so I guess you’re mixing and matching GPIO numbers and D numbers and expecting it to work - which it wont!

You need to use GPIO numbers throughout, and choose ones that are available as pins on the board.
However, you need to avoid the “Special” GPIOs…

It’s not recommended that you use GPIO16 as this is used as part of the wakeup functionality and can cause issues if you use it for other stuff
GPIO 1 and 3 are used by the serial interface, so will cause issues if you wnat to do serial debugging or upload code with these pins connected.
GPIO0 is used when flashing code, so avoid this as well.
GPIO2 is connected to the internal LED. That’s not necessarily a problem, depending on what you use this pin for.

This excellent post from @wanek gives more details:

Pete.

1 Like

Thank you Pete!

Ive looked at many other peoples code for a similar project and was testing the suitability for Blynk without reworking too much code I didn’t understand. This current one has a counter to write to the LCD not a timer so i thought i was going to get issues somewhere.
I definitely should have changed the pins to available nodemcu pins!
Thank you for the link i will read it and post my results for others.

Ive sorted out a few of the issues I had and the disconnecting thanks @PeteKnight
I cant seem to get the outputs to work properly however…

I took the original code from a book (30 arduino projects for the evil genius) and am porting it to nodemcu and Blynk.

Ive been testing the setup with a pot simulate the temperature and it updates on the LCD widget but no change in the relay state once it goes above the set point.

I tried to throw in updateOutputs() to no avail.
Im still working on the step widget and super chart if you notice some unnecessary code…

I like the idea of being able to adjust the hysteresis, so any help with getting it running would be awesome!

Thanks

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLCD lcd(V1);
char auth[] = "b494561879f843a890f803b9d375c619";
char ssid[] = "dlink";
char pass[] = "7Larkspur";

#define beta 4090 // from your thermistor's datasheet
#define resistance 33


int ledPin = 4  ;
int relayPin = 5;
int aPin = 12;
int bPin = 13;
int buttonPin = 14;

int sensor = analogRead(A0); //get value from analog pin 0 


int analogPin = 0;
float setTemp = 20;
float measuredTemp;
char mode = 'C';
boolean override = false;
float hysteresis = 0.25;
void setup()
{
Blynk.virtualWrite(V1, sensor);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);

pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
pinMode(buttonPin, INPUT);

lcd.clear();
}
void loop()
{
Blynk.run();  
static int count = 0;
measuredTemp = readTemp();
if (digitalRead(buttonPin))
{
override = ! override;
updateDisplay();
delay(500); // debounce
}
int change = getEncoderTurn();
setTemp = setTemp + change * 0.1;
if (count == 1000)
{
updateDisplay();
updateOutputs();
count = 0;
}
count ++;
}



int getEncoderTurn()
{
// return -1, 0, or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(aPin);
int newB = digitalRead(bPin);
if (newA != oldA || newB != oldB)
{
// something has changed
if (oldA == LOW && newA == HIGH)
{
result = -(oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
float readTemp()
{
long a = analogRead(analogPin);
float temp = beta / (log(((1025.0 * resistance / a) - 33.0) / 33.0) +
(beta / 298.0)) - 273.0;
return temp;
}
void updateOutputs()
{
if (override || measuredTemp < setTemp - hysteresis)
{
digitalWrite(ledPin, HIGH);
digitalWrite(relayPin, HIGH);
}
else if (!override && measuredTemp > setTemp + hysteresis)
{
digitalWrite(ledPin, LOW);
digitalWrite(relayPin, LOW);
}
}

void updateDisplay()
{
lcd.print(0, 0, "Actual: ");
lcd.print(8, 0, (measuredTemp));
lcd.print(13, 0, " oC");
//lcd.print(" ");
//
//if (override)
//{
//lcd.print(0, 1, "  ");
//}
//else
//{
lcd.print(0, 1, "Set: ");
lcd.print(8, 1, (setTemp));
lcd.print(13, 1, " oC");
//lcd.print(mode);
//lcd.print(" ");
}

float adjustUnits(float temp)
{
if (mode == 'C')
{
return temp;
}
else
{
return (temp * 9) / 5 + 32;
}
}

Thanks :slight_smile: