Nodemcu + ultrasonic + OLED

Hello, i’m a new and need some help with my code or connections
The Oled is connected D1 5 // SCL and D2 4 // SDA
I don’t know why the Oled is not working, is the code not correct or not good connected

#define BLYNK_PRINT Serial

/*******Waterproof Ultrasonic**********/
#include <NewPing.h>
/*******NodeMCU**********/
#include <ESP8266WiFi.h>
/*******BLYNK***********/
#include <BlynkSimpleEsp8266.h>

/*******OLED****************/
#include <SPI.h>   //we need all those nasty libraries for OLED
#include <Wire.h> 
#include <Adafruit_GFX.h> 
#include <Adafruit_SSD1306.h>  
#define OLED_RESET 4 // this is the reset pin, IM NOT USING IT
Adafruit_SSD1306 display(OLED_RESET);


#define CommonSenseMetricSystem

/*********waterproof ultrasonic**************/
#define TRIGGERPIN D7
#define ECHOPIN    D8

/*************LED****************************/
#define LED1       D3
#define LE21       D4
#define LED3       D5
#define LED4       D6
//#define LED5       D7
//#define LED6       D8

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

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

/************LCD**************/
WidgetLCD lcd(V15);

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(TRIGGERPIN, OUTPUT);
  pinMode(ECHOPIN, INPUT);
  Blynk.begin(auth, ssid, pass);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
  display.clearDisplay();

 //*****Aantal LED***********
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  //pinMode(D7, OUTPUT);
  //pinMode(D8, OUTPUT); 

}

void loop()
{
  lcd.clear();  //Use it to clear the LCD Widget
  lcd.print(0, 0, "Distance in cm"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  // Please use timed events when LCD printintg in void loop to avoid sending too many commands
  // It will cause a FLOOD Error, and connection will be dropped
  
  long duration, distance;
  digitalWrite(TRIGGERPIN, LOW);  
  delayMicroseconds(3); 
  digitalWrite(TRIGGERPIN, HIGH);
  delayMicroseconds(12); 
  digitalWrite(TRIGGERPIN, LOW);
  
  duration = pulseIn(ECHOPIN, HIGH);
  //distance = duration/58.2;

  #ifdef CommonSenseMetricSystem
  distance = (duration/2) / 29.1;
  #endif
 
  display.setCursor(2,0);        //x,y coordinates
 display.setTextSize(4);         //size of the text
 display.setTextColor(WHITE);    //if you write BLACK it erases things
 display.println(distance);      //print our variable
 display.setCursor(85,5);       //set size,print the units (cm/in)
 display.setTextSize(3); 
 
 #ifdef CommonSenseMetricSystem//if theres#define CommonSenseMetricSystem
 display.println("cm");        //print "cm" in oled
 #endif

 display.display();          //you need to actually display all that data
 delay(500);                 //wait!, human speed
 display.clearDisplay();      //clear black the display
  
 Serial.println(distance);//debug
  
//**********************
  if (distance <= 150) {
    Blynk.virtualWrite(V30, 0); // distance greater than 150cm LED OFF
    digitalWrite(D3,LOW);
}
  else {
    Blynk.virtualWrite(V30, 255); // distance smaler than 150cm LED ON
    digitalWrite(D3,HIGH);
  }

//*********************
 if (distance <= 100) {
    Blynk.virtualWrite(V31, 0); // distance greater than 100cm LED OFF
    digitalWrite(D4,LOW);
}
  else {
    Blynk.virtualWrite(V31, 255); // distance smaler than 1000cm LED ON
    digitalWrite(D4,HIGH);
  }

//**********************
   if (distance <= 80) {
    Blynk.virtualWrite(V32, 0); // distance greater than 80cm LED OFF
    digitalWrite(D5,LOW);
}
  else {
    Blynk.virtualWrite(V32, 255); // distance smaler than 80cm LED ON
    digitalWrite(D5,HIGH);
  }
  
//*********************
  if (distance <= 40) {
    Blynk.virtualWrite(V33, 0); // distance greater than 40cm LED OFF
    digitalWrite(D6,LOW);
    
}
  else {
    Blynk.virtualWrite(V33, 255); // distance smaler than 40cm LED ON
    digitalWrite(D6,HIGH);
  }

//**********************
   if (distance <= 20) {
    Blynk.virtualWrite(V34, 0); // distance greater than 20cm LED OFF
    //digitalWrite(D7,LOW);   
}
  else {
    Blynk.virtualWrite(V34, 255); // distance smaler than 250cm LED ON
    //digitalWrite(D7,HIGH);
  }

//*************
   if (distance <= 0) {
    Blynk.virtualWrite(V35, 0); // distance greater than 0cm LED OFF
    //digitalWrite(D8,LOW);    
}
  else {
    Blynk.virtualWrite(V35, 255); // distance is 0cm LED ON
    //digitalWrite(D8,HIGH);
  }
  
  Serial.print(distance);
  Serial.println("Cm");
  Blynk.virtualWrite(V16, distance);
  lcd.print(7, 1, distance);
  Blynk.run();

  delay(1000); //refrech elke 1 sec, 1000=1sec
}


1 Like

First… Blynk does not control your OLED in any way, all device display connections, control, and data sent is via the library for your OLED.

Get it fully operational without Blynk, then you can add in Blynk and use a widget like Terminal to send text to your sketch and with further code, forward that to the OLED.

Second… Your void loop() is way too full for any practical Blynk operation. You need to use Timers to call indevidual function loops as needed.

First start with the bare bones sample sketch for the OLED library you are using. I connected one of the 0.96" OLED displays to my Nodemcu esp8266 last night and it worked perfect.

Hi @Pascal1800
Writing good code that works well with Blynk is quite different to what you can get away with in most situations, but in addition these ultrasonic sensors (I presume you’re using an HC-SR04 or something similar) are very sensitive to timing so need careful coding.

The golden rules for Blynk are:

  1. only put Blynk.run and Timer.run in your void.loop
  2. move your other code out of void.loop and call it with a timer
  3. don’t use delays

Your device needs to be continuously communicating with the Blynk server and having lots of code in the loop, or using delays which stall the device until the delay has elapsed, cause communication with the server to be lost.

It’s not clear what this device is for, but if it’s a simple digital tape measure that displays the result on the OLED then you probably don’t need to be tali g endings every second. You probably just need one racing when you press a button.
If it is a system that needs constant measurements then ask yourself if it needs to be every second. Very few systems really need this sort of frequency. Would once every 5 seconds work just as well?

There may be issues with how you’ve connected-up your system, or there may be conflicts with the drivers and hardware. I’ve not bothered to go and translate the NodeMCU “D” PIN numbers into GPIO numbers to work-out of you’re doing anything stupid, because the code structure is so flawed.

I’d suggest you search for your ultrasonic sensor’s model number on this forum and find Blynk some code that will work with it, and display the readings in the Blynk app, then once that’s working look at adding in the OLED functionality. Study the structure of the Blynk code you find, and understand how Timer sets are used, before trying to hook-up your OLED.

I’d also suggest that you use proper GPIO numbers in your code, not ‘D’ numbers.

Pete.

1 Like

Thanks already thank you for your answers.
The sensor is a JSN-SR04T waterproof sensor. I want to use it for the measurenent of the waterlevel in my tank.
At the same time i want to see it on the oled and the extra led’s when it’s almost empty
The time don’t need to be every sec, its just now for testing

So, can you do that without any Blynk code?..just with whatever OLED and sensor libraries required? If not, then that is where you start as Blynk will have nothing to do with getting those working together.

I would have put money on the fact that you were using your sensor for this, it seems that every time someone mentions these ultrasonic ranging sensors it’s to measure water level in a tank.

Search this forum for “water level” and you’ll find a number of threads with some nice Blynk friendly code.
One guy was calculating both the depth of the water and the remaining volume of water in his tank and displaying this in the Blynk app. Take something like that, get it running WITHOUT the OLED display, then add in the OLED code at slater stage.

If you have flow sensors on your inlet and/outlet pipes you could also calculate the remaining run time at the current rate of discharge, or how long it will take to fill at the existing rate of inlet flow. But, get the basics running first and take care to follow the rules about how to write Blynk friendly code.

Pete.

@Gunner I’ve got it working on a arduino uno with the same oled en ultrasonic but with the esp i don’t know were to put SDA and SCL

@PeteKnight
I have made already one with the uno and a HC-SR04 but after 3 months it stop working the SR04

Google to the rescue!!! (well, and me for doing all the typing for you :stuck_out_tongue_winking_eye: )

As from my examples topic - C++ Blynk (Legacy) - Code Examples for Basic Tasks

For the Wemos… but should be identical for the NodeMCU…
image

If not, another Google search found this…

I honestly don’t understand how people in this day and age seem unable to survive without their phones… but couldn’t Google an answer if their life depended on it :confounded:

1 Like

Thanks @Gunner & @PeteKnight for your input, i’ve got it working