1

My NodeMCU wont connect to Blynk with my code but works fine with example code and same auth token and WiFi info. Cant figure out why its not working. Must be something in my code interfering with it (Or something stupid I'm missing). Any ideas? Thanks!

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "AUTH";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "PASSWD";



const int analogPin = A0;

const int muxA = 9;// MUX pin A
const int muxB = 10;//MUX pin B
const int muxC = 16;//MUX pin C

const int outputPin0 = 4;// output pin 0
const int outputPin1 = 5;// output pin 1
const int outputPin2 = 12;//Output pin 2
const int outputPin3 = 13;//output pin 3
const int outputPin4 = 14;//output pin 4

int startWatering0 = 1023;//start watering value 0
int startWatering1 = 1023;//start watering value 1
int startWatering2 = 1023;//start watering value 2
int startWatering3 = 1023;//start watering value 3
int startWatering4 = 1023;//start watering value 4

int currentMoisture0 = 0;//current moisture value 0
int currentMoisture1 = 0;//current moisture value 1
int currentMoisture2 = 0;//current moisture value 2
int currentMoisture3 = 0;//current mousture value 3
int currentMoisture4 = 0;//current moisture value 4

int wateringTime0 = 0;//watering time 0
int wateringTime1 = 0;//watering time 1
int wateringTime2 = 0;//watering time 2
int wateringTime3 = 0;//watering time 3
int wateringTime4 = 0;//watering time 4

bool isWatering0 = false;//is 0 currently watering
bool isWatering1 = false;//is 1 currently watering
bool isWatering2 = false;//is 2 currently watering
bool isWatering3 = false;//is 3 currently watering
bool isWatering4 = false;//is 4 currently watering

long wateringStart0 = 0;//watering 0 start value
long wateringStart1 = 0;//watering 1 start value
long wateringStart2 = 0;//watering 2 start value
long wateringStart3 = 0;//watering 3 start value
long wateringStart4 = 0;//watering 4 start value

BlynkTimer timer;

void setup()
{
  //initialize outputs
  pinMode(muxA, OUTPUT);
  pinMode(muxB, OUTPUT);
  pinMode(muxC, OUTPUT);

  pinMode(outputPin0, OUTPUT);
  pinMode(outputPin1, OUTPUT);
  pinMode(outputPin2, OUTPUT);
  pinMode(outputPin3, OUTPUT);
  pinMode(outputPin4, OUTPUT);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  timer.setInterval(1000L, checkSensors);
}

void checkSensors()
{
  //check sensors here
  setMux0();
  currentMoisture0 = analogRead(analogPin);
  setMux1();
  currentMoisture1 = analogRead(analogPin);
  setMux2();
  currentMoisture2 = analogRead(analogPin);
  setMux3();
  currentMoisture3 = analogRead(analogPin);
  setMux4();
  currentMoisture4 = analogRead(analogPin);
  setMux5();
  setMux6();
  setMux7();
  
  Blynk.virtualWrite(V11, currentMoisture0);
  Blynk.virtualWrite(V12, currentMoisture1);
  Blynk.virtualWrite(V13, currentMoisture2);
  Blynk.virtualWrite(V14, currentMoisture3);
  Blynk.virtualWrite(V15, currentMoisture4);



  //starts watering if soil is drier than set value, and if it is not already watering
  if(currentMoisture0 >= startWatering0 && isWatering0 == false) 
  {
    isWatering0 = true;
    digitalWrite(outputPin0, HIGH);
    wateringStart0 = millis();
  }
  
  if(currentMoisture1 >= startWatering1 && isWatering1 == false)
  {
    isWatering1 = true;
    digitalWrite(outputPin1, HIGH);
    wateringStart1 = millis();
  }
  
  if(currentMoisture2 >= startWatering2 && isWatering3 == false)
  {
    isWatering2 = true;
    digitalWrite(outputPin2, HIGH);
    wateringStart2 = millis();
  }
  
  if(currentMoisture3 >= startWatering3 && isWatering3 == false)
  {
    isWatering3 = true;
    digitalWrite(outputPin3, HIGH);
    wateringStart3 = millis();
  }
  
  if(currentMoisture4 >= startWatering4 && isWatering4 == false)
  {
    isWatering4 = true;
    digitalWrite(outputPin4, HIGH);
    wateringStart4 = millis();
  }


///////////////////////////////////////////////////////////////////////////////////
  //stops watering if it is currently watering, and it has been watering for longer than the set time
  if(millis() >= wateringStart0 + (wateringTime0 * 60000 ) && isWatering0 == true)
  {
    isWatering0 = false;
    digitalWrite(outputPin0, LOW);
  }
  
  if(millis() >= wateringStart1 + (wateringTime1 * 60000 ) && isWatering1 == true)
  {
    isWatering1 = false;
    digitalWrite(outputPin1, LOW);
  }

  if(millis() >= wateringStart2 + (wateringTime2 * 60000 ) && isWatering2 == true)
  {
    isWatering2 = false;
    digitalWrite(outputPin2, LOW);
  }

  if(millis() >= wateringStart3 + (wateringTime3 * 60000 ) && isWatering3 == true)
  {
    isWatering3 = false;
    digitalWrite(outputPin3, LOW);
  }

  if(millis() >= wateringStart4 + (wateringTime4 * 60000 ) && isWatering4 == true)
  {
    isWatering4 = false;
    digitalWrite(outputPin4, LOW);
  }
}

void setMux0()//sets mux to pin 0
{
  digitalWrite(muxA, LOW);
  digitalWrite(muxB, LOW);
  digitalWrite(muxC, LOW);
}

void setMux1()//sets mux to pin 1
{
  digitalWrite(muxA, HIGH);
  digitalWrite(muxB, LOW);
  digitalWrite(muxC, LOW);
}

void setMux2()//sets mux to pin 2
{
  digitalWrite(muxA, LOW);
  digitalWrite(muxB, HIGH);
  digitalWrite(muxC, LOW);
}

void setMux3()//sets mux to pin 3
{
  digitalWrite(muxA, HIGH);
  digitalWrite(muxB, HIGH);
  digitalWrite(muxC, LOW);
}

void setMux4()//sets mux to pin 4
{
  digitalWrite(muxA, LOW);
  digitalWrite(muxB, LOW);
  digitalWrite(muxC, HIGH);
}

void setMux5()//sets mux to pin 5
{
  digitalWrite(muxA, HIGH);
  digitalWrite(muxB, LOW);
  digitalWrite(muxC, HIGH);
}

void setMux6()//sets mux to pin 6
{
  digitalWrite(muxA, LOW);
  digitalWrite(muxB, HIGH);
  digitalWrite(muxC, HIGH);
}

void setMux7()//sets mux to pin 7
{
  digitalWrite(muxA, HIGH);
  digitalWrite(muxB, HIGH);
  digitalWrite(muxC, HIGH);
}

//sets values when they are changed in app

//start watering values

BLYNK_WRITE(V1)
{
  int startWatering0 = param.asInt();
}

BLYNK_WRITE(V2)
{
  int startWatering1 = param.asInt();
}

BLYNK_WRITE(V3)
{
  int startWatering2 = param.asInt();
}

BLYNK_WRITE(V4)
{
  int startWatering3 = param.asInt();
}

BLYNK_WRITE(V5)
{
  int startWatering4 = param.asInt();
}


//watering time values

BLYNK_WRITE(V6)
{
  int wateringTime0 = param.asInt();
}

BLYNK_WRITE(V7)
{
  int wateringTime1 = param.asInt();
}

BLYNK_WRITE(V8)
{
  int wateringTime2 = param.asInt();
}

BLYNK_WRITE(V9)
{
  int wateringTime3 = param.asInt();
}

BLYNK_WRITE(V10)
{
  int wateringTime4 = param.asInt();
}

void loop()
{
  Blynk.run();
  timer.run();
}
8
  • where does it fail? ... you have no debugging code in your sketch ... please add code that prints to the serial monitor or code that blinks an LED, so that you can trace the progress of your code
    – jsotola
    Jun 14 at 21:54
  • In general it's not a good idea to post actual auth tokens or passwords. Jun 14 at 22:29
  • I'm a bit lost with this code--for example, BLYNK_WRITE(V10) creates a local wateringTime4 and immediately returns, throwing it away. I don't think you intended to create a local variable there. The might also need to be volatile, I don't recall. Jun 14 at 22:36
  • 1
    Tangentially related but the code might be a little easier to reason about if some of this data was encapsulated and shoved into an array or something. Jun 14 at 22:39
  • you are brave to use pins 9 and 10. Do you have DOUT as flash mode?
    – Juraj
    Jun 15 at 7:59
1

Ok I got it figured out, it was because I was using pin 9 and 10, I didnt know they couldn't be used. Thanks @Juraj!

0

I'd start by cutting it down to just the setup() and loop() functions (without the timer call) and establish whether it connects, and if not, why not.
Then add back the timer call and a very short version of checkSensors(), and try again.
Continue adding back sections of code until it fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.