Serial monitor does not show anything

I have an problem. I use this code and can't get anything to show up in serial monitor. Just an empty white screen.

I use ESP32 and this is my code:

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

const int trigPin = 13;
const int echoPin = 14;

long duration;
int distance;


char auth[] = "***";

char ssid[] = "***";
char pass[] = "***";


BlynkTimer timer;

void GarageSensor() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Avstand: ");
  Serial.print(distance);
  Serial.println(" cm.");

  if(distance < 40){
  Blynk.virtualWrite(V1, "GARASJEPORT ER STENGT");
  }
  if(distance > 40){
  Blynk.virtualWrite(V1, "GARASJEPORT ER ÅPEN");
  }
}


void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600); // Starts the serial communication
  timer.setInterval(2000L, GarageSensor);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

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

When i just use this code it works and i can see the word "TEST" in monitor.

void setup() {
 Serial.begin(9600);
}

void loop() {
  Serial.println("TEST");
}

I use the same boud rate in code and monitor (9600).

Can someone see what i have did wrong in the first code?

GarageSensor() is being used here as a timer call back routine. I’m guessing that Serial commands do not work in this context. Try defining the variables you want to print as volatile and printing them from the loop().

I have tried that, but it does not work. It works only when i use the simple, last code.

Bjerknez:
I have tried that, but it does not work. It works only when i use the simple, last code.

OK.
Post the code where you attempted to print distance from the loop() as suggested, then someone can suggest further modifications. Hint: an integer consists of 4 bytes here so you should make a copy of it before attempting to use it in the loop(). You make the copy of it within a noInterrupts() / interrupts() block to prevent it being half written to from the ISR during the time you are attempting to make the copy.

I don't know Blynk but if, however, it interferes with Serial then this will anyway not work.

I'm not shure what you really mean here?

What if you put a print in setup, straight after the Serial.begin?

Bjerknez:
I'm not shure what you really mean here?

Let's start here. You said this :

Bjerknez:
I have tried that, but it does not work. It works only when i use the simple, last code.

Post the code where you have attempted to try "that".

I have done that in my first post here?

Okay, i have found the problem regards to serial monitor that not displyed anything. The probem dissapeared when i changed my blynk token...

So that part is now working.

But now i'm little confused regards to how i can make my ESP32 to send me an reminder if the garage door is still open after 30 minutes.

I get notice when the door opening and closing, but i want to get an reminder if the door is still open after 30 minutes.

How can i implate that in my code?

Her is my code tht works, espect that.

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

const int trigPin = 26;
const int echoPin = 27;

long duration;
int distance;


char auth[] = "***";

char ssid[] = "***";
char pass[] = "***";


BlynkTimer timer;


void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600); // Starts the serial communication
  timer.setInterval(5000L, GarageSensor);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

BLYNK_READ(V1){
  if(distance > 100){
    Blynk.virtualWrite(V1, "GARASJEPORT ER ÅPEN!");
  }
  else{
    Blynk.virtualWrite(V1, "GARASJEPORT ER STENGT!");
  }
}


void GarageSensor() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Avstand: ");
  Serial.print(distance);
  Serial.println(" cm.");
}


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

Someone?

I don't know about Blynk, but this doesn't look like a hard problem at all - so I'm not sure what you're stuck on.

Use millis() to grab the current run time in milliseconds when the door opens, then compare that to the current value of millis(). If it's greater than 30601000, that means it's been open for 30 minutes.

It's not obvious where you're stuck.

Thanks :slight_smile:

I just need help to the code you just explained. I need to see it. I have tested the "blink without delay" example, but i think is it many more ways to play with millis. I just do not know where to start.

So i just need to see the code that you explained, for getting forward.

Bjerknez:
I get notice when the door opening and closing, but i want to get an reminder if the door is still open after 30 minutes.

I'm not seeing how that happens. Seems to me that your sensor tests that the door is open or closed, but not openING or closING.

#include <WiFi.h>
BLYNK_READ(V1){
  if(distance > 100){
    Blynk.virtualWrite(V1, "GARASJEPORT ER ÅPEN!");
  }
  else{
    Blynk.virtualWrite(V1, "GARASJEPORT ER STENGT!");
  }
}
}

I'm wondering how it can be that you don't get spammed with messages; does Blynk take care of that?

Because to do the test for 30 minutes as suggested by binaryfissiongames, you will need to compare the sensor reading "now" to sensor reading "previous" in order to capture the instant at which it becomes open. And I don't see that you're doing that already.

(But it's early and I have so far had only 1 1/2 cups of coffee, so maybe I'm missing sth?)

Thanks.

Yes, blynk takes care of that. The code writes to blynk server and my mobile app (blynk) is set to get message from server eatch 5 second.

Then unless you can invoke further voodoo with Blynk, you would need to change your code along the lines of (but not identical to) the state change detect tutorial.

Instead of checking for a state change from high to low or low to high, look for a change from <100 to >100, at which point capture millis into a variable.

(Which means you would have a variable called say distancePrevious over and above the distance you already have.)

Okay, thanks. I try that :slight_smile:

But i want it to happend just one time. How can i make that 30min. Timing to just run ounce after eatch time the door opens?

I want a reminder that the door is open if it is, after 30 minutes, but only one time until it opens next time.

Bjerknez:
I want a reminder that the door is open if it is, after 30 minutes, but only one time until it opens next time.

Thats exactly what binaryfissiongames said here:

binaryfissiongames:
Use millis() to grab the current run time in milliseconds when the door opens, then compare that to the current

Use the state change from < to > 100 to indicate the door has just opened.

While it's open, the state change will be seeing distancePrevious the same as distance each time thru loop() so won't be triggering a new opening event.

If I were you, I'd write a simple sketch to do nothing but read your distance sensor and show on the monitor that the door had "just opened" or "just closed". Get that working without Blynk and any other distractions getting in the way.

Then once that's working reliably, capture the opening time as suggested earlier, and then print a message to say "been open for 1 minute" or whatever.

Only then, migrate that approach to your real code.