Friday, October 29, 2021

Build your own rain and water level sensor

 For an index of all my stories click this text.

Recently I was building a project in which I needed a water level sensor. And a water level sensor is obvious the same technique as a rain sensor. The basic difference between the two is that a rain sensor has a large surface to catch rain drops while a water level sensor just needs two small contacts.

A few years ago I build a rain sensor with an ESP8266. The program was written in LUA and the project used a ready-made sensor. You can re-read that story here if you like:
https://lucstechblog.blogspot.com/2016/08/rain-sensor-using-esp8266.html

That version worked but was rather primitive. I have now much more expertise in sending notifications when it rains. You can send a signal to Domoticz, to your Phone, to a Blynk app, or to Telegram just to name a few.

But first you need a sensor. So lets build one.

Water sensor.

Basically what I am going to show you is a water sensor. The sensor just detects water and that can be rain, a water tank level, or a leaking washing machine it is all the same.



The sensor consists of an NPN transistor, a resistor and a capacitor. That's all.

The schematic is made with the free design program EasyEDA designer which can be found here: https://easyeda.com/

The output will give normally a 0 until the two probes are connected. That connection can be made by wet-fingers, or submerged in water or a damp piece of cloth.

The transistor can be a general NPN version. I build versions with a 2N222 and a BC547 transistor that worked equally well.



Above you see how the pins of the 2N222 are arranged.


And the picture above shows the pinout of the BC547 transisitor.

The difference between these two is that the Emiitor and Collector have switched places.

The resistor should be 1K and the capacitor 100 Nf or close to that. Using a larger capacitor makes the circuit take longer switching from 0 to 1 and the reverse.

First thing was doing some tests. For that I added a current delimiting resistor of 220 Ohm and a led for monitoring the output.

For convenience I herebye give you the breadboard setup.



Above is the version with the 2N222 transistor.


And here is the version with a BC547 transistor.

As stated both work equally wel.

Testing the sensor.

At the left side of the breadboard there are two purple wires. In reality I just used breadboard (Dupont) wires.

To test the sensor just connect the wires, wet your fingers and put the wires between them, put the wires in a glass of water or put them on a piece of cloth and start dropping water on that cloth. In all cases the led will go ON and as soon as the wires are seperated or dry again the led will go OFF again.

This sensor is very sensitive. So putting both ends at one side of a bowl of water is enough to set the led ON.

Connecting to an ESP

You can use this sensor with an Arduino, AT Tiny85, ESP8266, ESP32, Raspberry Pico or any other controller you want to use in your project.

For testing I used an ESP8266.



Here is the breadboard setup for a sensor using the 2N222 with a Wemos D1 mini.



And here is the setup with a BC547 transistor.

In both versions the led is omitted and the output is direct connected to D8 of the Wemos D1.

ESP8266 program

// Rain sensor test
// The sensor is connected to D8

void setup() 
{
  Serial.begin(9600);
  delay(1000); 
  Serial.println("ESP8266 rain test");
  pinMode(D8, INPUT);
}

void loop() 
{
  Serial.println(digitalRead(D8));
  delay(1000);
}


This is just a basic program that displays the sensor reading in the Serial Monitor.

In the setup() the serial monitor is initiated at 9600 baud and D8 is defined as input pin. The loop just prints the reading of pin D8 in the serial monitor.

Pin D8 will give LOW (0) till the input detects water. Then D8 will change to HIGH (1). So beware that the input is reversed to what we normally use when connecting a button or some other sensor.

Practical uses.

This is like said above a very sensitive sensor. It can be used for a lot of purposes. Examples are a rain-sensor, water level sensor, soil moisture sensor but also as a contact sensor for doors etc. I am going to use it for a water level sensor in an aquaponics system.

You do not even need a microcontroller for using this sensor. Attach a led or relay and you can make a local alarm or control a pump.

It can be used with any microcontroller you want to use in your project like the Arduino, Attiny, ESP32, ESP8266 and Raspberry Pico.

That's it for now.
Have fun

Luc Volders

Friday, October 8, 2021

ESP32 Cam doorbell

 For an index to all my stories click this text

The regular readers of this blog know that I wrote a few stories on Blynk. You can find them here:
http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-2-sending-data-to-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-3-your-own-blynk-server.html
http://lucstechblog.blogspot.com/2021/06/blynk-part-4-using-blynk-with-multiple.html

And I did some stories on the ESP32 Cam. You can find these here:
http://lucstechblog.blogspot.com/2021/09/esp32cam-programming-board.html
http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html

So how about combining these two and send pictures from the ESP32Cam to Blynk.

If you have no experience with Blynk I really urge you to read the above mentioned stories first and do some experiments with Blynk. This will make you getting familiar with Blynk which helps a lot with reproducing this project.

As this project uses 800 energy points I really urge you to build your own Blynk server like demonstrated in the 4th story. It is not necessary but there is little energy left for other projects if you use the free Blynk server unless you are willing to spend some money on buying energy.

What is the intention.

I am going to show you how to make a doorbell with an ESP32 Cam that sends a picture to Blynk when someone pushes a button. Next to that in the Blynk app there is a virtual button. When you press that button you will be able to make a picture wether or not there is someone at the door. So you can keep an eye on your door.

There are multiple purposes for this. And I will give you some examples at the end of this story.

The breadboard setup.

For programming the ESP32Cam I used my own ESP32Cam programming board that can be found (and re-build by yourself) here:http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html

When the programming and testing finished I used my ESP32Cam experimenting board which you can be found (and build by yourself) here :
http://lucstechblog.blogspot.com/2021/10/esp32cam-experiment-board.html



I put the ESP32Cam on the experimenting board and put a small breadboard next to it. On the breadboard I put a pushbutton with a pull-up resistor. The pushbutton is connected to IO pin 13.

Blynk project

For this experiment I started a new project. Surprisingly I called it Espcam doorbell.
If you have no experience with Blynk start with the first of my stories. That story tells you how to add a button and other items to Blynk. You can re-read that story here: http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html



There are just 2 items on the screen.



The first is a button which is attached to GPIO 14. I gave it the name Photo but you can use any name you like. The button was attached to GP14 and is normally low (0) and sends a high signal(1) when pushed. The text is in a large font and colored red. Off the button has the text Photo and when it is pushed the text alters to Done.



The second item is an Image Gallery. You can find that in the Widget Box in the DISPLAYS section.



The Image Gallery is attached to Virtual pin V1. I made the Image Gallery box screen wide and used FIT to make the picture fit into that box.
The most important part is to tell Blynk where to get the picture.

HTTP://192.168.1.86/capture

This is the reference where Blynk can fetch the Photo. The problem at this moment is that you do not know the IP number of the ESP32-Cam. So first we need to flash the software into the ESP32Cam.

The program.

Actually this is the program that is supplied with the ESP32 Cam board in the Arduino IDE.


 

So open the Arduino IDE. Choose as board AI Thinker ESP32-Cam and then load the CameraWebServer example.
To get this working with Blynk we need to make some changes to the program. First I will give you the complete program. You can just copy and paste it if you are not interested in the modifications.

#include "esp_camera.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";
char auth[] = "BLYNK AUTHENTIFICATION CODE"; 

//
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
//            or another board which has PSRAM enabled
//

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER

#include "camera_pins.h"

#define button 13
#define Virtbut 14
#define flashled 4

String camip;
void startCameraServer();

void blynkphoto()
{
  digitalWrite(flashled, HIGH);
  delay(200);
  Blynk.setProperty(V1, "urls", "http://"+camip+"/capture");
  digitalWrite(flashled, LOW);
}

void setup() {
  Serial.begin(115200);
  Serial.println();

  pinMode(flashled,OUTPUT);
  pinMode(button,INPUT_PULLUP);
  Serial.println();

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  //init with high specs to pre-allocate larger buffers
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t * s = esp_camera_sensor_get();
  //initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);//flip it back
    s->set_brightness(s, 1);//up the blightness just a bit
    s->set_saturation(s, -2);//lower the saturation
  }
  //drop down frame size for higher initial frame rate
  s->set_framesize(s, FRAMESIZE_QVGA);

#if defined(CAMERA_MODEL_M5STACK_WIDE)
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
#endif

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  startCameraServer();

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  camip = WiFi.localIP().toString();
  Serial.println("' to connect");
  Blynk.begin(auth, ssid, password, IPAddress(XXX.XXX.XXX.XXX), 8080);
}

void loop()  
{
  Blynk.run();
  if(digitalRead(button) == LOW)
    {; 
    Serial.println("Send Notification");
    Serial.println("Someone rang the bell");
    Serial.println("Getting a photo ");
    blynkphoto();
    }
  if(digitalRead(Virtbut) == HIGH)
    {
    Serial.println("Blynk virtual button pressed");
    Serial.println("Getting the Photo");
    blynkphoto();
    } 
}


The additions for Blynk

Let's have a look at the modifications I made for getting the standard CameraWebServer program.

#include "esp_camera.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

Next to the Wifi libraries the Blynk library must be included.

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";
char auth[] = "BLYNK AUTHENTIFICATION CODE";


You need to fill in your routers credentials and the Blynk authetification code you received from Blynk when you created the Blynk project.

#define button 13
#define Virtbut 14
#define flashled 4

Here the pins for the buttons are defined. The physical button is attached to GPIO pin 13 at the ESP32Cam just as the breadboard showed. The Virtual pin is attached to GPIO14. Next to that The ESP32 Cam's internal flash led is attached to GPIO 4. I am using the flashlight to brighten te pictures in my rather dark mancave.

String camip;

A String variable is defined in which the IP number of the camera is stored. This is later on going to be used to inform Blynk of the camera's IP number.

void blynkphoto()
{
  digitalWrite(flashled, HIGH);
  delay(200);
  Blynk.setProperty(V1, "urls", "http://"+camip+"/capture");
  digitalWrite(flashled, LOW);
}

This routine is called from the loop() when the physical button or the virtual button is pressed. The flashlight is set ON and thye program waits 200ms to be sure that the led is ON. Then the IP adress of the ESP32-Cam (campip as found in the setup) together with the command /capture. This activates the camera, makes the photo and sends it to the Image Gallery Which has as reference in Blynk V1.

If you do not need the flashlight just remove the digitalWrite(flashled, HIGH); and digitalWrite(flashled, LOW);
Another option is to comment these lines out. Then they are easier to edit when you might need the flashlight again.
You could also add another virtual button to Blynk to set the flashlight on or off.

To test wether taking a photograph works you can put the line:
http://XXX.XXX.XXX.XXX/capture in your browser where you have to replace the XXX.XXX.XXX.XXX with the IP number of the ESP32-Cam. That way you can check wether the ESP32Cam works ok and that the resolution is to your liking.

The cams settings can be altered by putting the ESP32-cam's IP number in your browser. The cams webpage will open and there you can alter all settings like resolution. So next to the Blynk connection the ESP-cam's webpage keeps on functioning. Even the streaming function works although that will be stopped for a split second when you take a picture with Blynk.  

  pinMode(flashled,OUTPUT);
  pinMode(button,INPUT_PULLUP);

These lines are added to the beginning of the setup() routine to set the ESP32 Cam's pins in the right mode.

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  camip = WiFi.localIP().toString();
  Serial.println("' to connect");

In these lines the program gets the local IP number of the cam. We will need this to tell Blynk at what IP number the photo to be send can be obtained.

  Blynk.begin(auth, ssid, password, IPAddress(XXX.XXX.XXX.XXX), 8080);

The last line has been added. This line gives all the information to connect to my local Blynk server. Replace the XXX's with the IP number of your own local Blynk server.

Blynk.begin(auth, ssid, pass);

If you do not have your own local Blynk server you can replace the line by the above line. This connects the ESP32 Cam to the cloud based Blynk server.

void loop() 
{

  Blynk.run();
  if(digitalRead(button) == LOW)
    {
    Serial.println("Someone rang the bell");
    Serial.println("Getting a photo ");
    blynkphoto();
    }
  if(digitalRead(Virtbut) == HIGH)
    {
    Serial.println("Blynk virtual button pressed");
    Serial.println("Getting the Photo");
    blynkphoto();
    } 
}

The loop tests wether the physical button on the ESP32-Cam (the doorbell buutton) is pressed. If that is true it calls the blynkphoto() routine that actually sends the photo.
Besides the physical button there is also the virtual button attached to GP14 whic is controlled from the Blynp App. If that is pressed there also will be a photo send to the app.

The doorbell in action

Well not really. This was a proof of concept and I wanted to test wether the ESP32Cam was capable of working as a doorbell. And it is. I the webcams webpage I set the resolution low so pictures would be transferred fast to Blynk. Better resolution is better quality.



Here is a sample picture I took from my 3D printer.

More options.

The doorbell is one way for using this. You could change the physical button by a PIR or a RCWL-0516 radar module to detect movement instead of pushing a button. Use velostat and define an IO pin as an analog input to detect pressure. Use these methods to detect who is stealing your cookies. Or build a bird feeding place that sends you a picture when a bird comes to dinner.
Re-read the story about using multiple ESP's with Blynk. With this information you can build a remote controlled car with a camera on it.

I bet you can come up with a lot of projects too. The price of the ESP32Cam should be no objection.

Till next time
have fun

Luc Volders







Friday, October 1, 2021

ESP32Cam experiment board

For an index to all my stories click this text

As the regular readers of this weblog know I have been experimenting with the ESP32-Cam a lot lately. The ESP32-Cam is a nice little board that has an ESP32, an SDcard interface and a real camera. It can be used as an IP-Cam, photocam and safety cam.

Programming the ESP32-Cam can be done by attaching wires to an FTDI but is more easily done with a programming board such as the one I described here: http://lucstechblog.blogspot.com/2021/09/esp32cam-programming-board.html

After it has been programmed I want to attach the ESP32-Cam to a breadboard. You can do that by using female to male Dupont wires. An easier and (short-circuit-wise) safer option is to build an experiment-board.

Stripboard.


To build the board I used stripboard.
The stripboard is small, just 10 holes wide and 13 holes high.
I removed the copper in the middle of the board so the upper headers would not be in contact with the lower headers. Just like the center of a breadboard does.



And here it is in real life.

Extra bonus

Here is something I just realised when the board was done. The ESP32-Cam has exact the same footprint as a Wemos D1 mini. So you can use this board for experimenting with both controllers.

Till next time.

Have fun,

Luc Volders