Pages

Friday, April 24, 2020

ESP makes Google Assistant talk part 2

For an index to all my stories click this line.

Before I go on you have to know that this story relies heavily on the previous story in this series. So please read the previous story thoroughly. You can find it here.
https://lucstechblog.blogspot.com/2020/03/esp32-makes-google-assistant-talk.html

The previous story showed you how to have the Google Assistant say all kind of sentences. I hope you had fun with that and did not make it to gross.
If you played around with it and already made some projects with this technique then you could skip this story and wait for the next one. Something really cool is coming up.

Before I give you the breadboard setup and program please refer to the first story in this series and make sure you have the libraries installed and know the name of your Google Assistant.

This time we are going to have the Assistant say some meaningfull things. We are going to have her read out loud some sensor values. You'll need a puhbutton and a Dallad DS18B20 thermometer for this. You can adapt the setup easily for other sensors though.

Breadboard

I am using a DOIT ESP32 Devkit and as you know this will not fit on a regular breadboard. Use this trick for getting the ESP32 properly on a breadboard:
http://lucstechblog.blogspot.com/2018/08/breadboard-hack-for-esp32-and-esp8266.html


 

I attached a Dallas DS18B20 digital thermometer to the ESP32 on GPIO 23 and connected a 4.7k pull-up resistor to its datapin.
Further I placed a pushbutton which is connected with a 10K pull-up resistor to GPIO 34.

The program.

What I am going to do is to have the program wait for the button to be pushed and when that is done it will let the Google Assistant say that the button was pressed and speak out the present temperature. This is an easy example to demonstrate how you can have the Google Assistant speak out sensor values.

There are a few pitfalls which I will show along the road. First the program.



#include <WiFi.h>
#include <esp8266-google-home-notifier.h>

const char* ssid     = "YOURROUTERNAME";
const char* password = "YOURPASSWORD";
String command ="";
int Button = 34;

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 23    

OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature dallas(&oneWire);

GoogleHomeNotifier ghn;

void setup() 
{
  pinMode(Button, INPUT);
  
  Serial.begin(115200);
  Serial.println("");
  Serial.print("connecting to Wi-Fi");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  const char displayName[] = "Living Room speaker";

  Serial.println("connecting to Google Home...");
  if (ghn.device(displayName, "en") != true) 
  {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.print("found Google Home(");
  Serial.print(ghn.getIPAddress());
  Serial.print(":");
  Serial.print(ghn.getPort());
  Serial.println(")");

  dallas.begin();
}

void loop() 
{
   int buttonState = digitalRead(Button);
   if (buttonState==0)
   {
   dallas.requestTemperatures(); 
   Serial.print("Temperature is: ");
   Serial.println(dallas.getTempCByIndex(0));

   ghn.notify("You pressed the button, let me check the temperature");
   delay (1000);
   
   command = "The temperature is ";
   dallas.requestTemperatures();
   command = command + String(int(dallas.getTempCByIndex(0)));
   command = command + "degrees celsius";
   ghn.notify (command.c_str());
   delay (5000);
   }
}


The first lines will not present any problem.
The needed libraries are loaded and some variables are defined. Do not forget to replace YOURROUTERNAME and YOURPASSWORD


#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 23   

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature dallas(&oneWire);

The above lines define the Dallas DS18B20 sensor and attach it to pin 23 of the ESP32.

The setup starts with connecting to the router and establishing an internet connection. When done it prints the IP number of the ESP32 in the serial monitor.

const char displayName[] = "Living Room speaker";

This line tells the library the name of your Google Assistant. Replace this with your own Assistant's name like I showed you in the previous article otherwise the library can not connect to the Assistant.

When connected to the Assistant the program shows the IP number of the Assistant in the Serial Monitor. This way you'll now that all connections are made.

dallas.begin()

This line actually starts the library for the Dallas thermometer.

In the loop you will find the tricky parts.

First the loop tests wether the button is pressed.

   dallas.requestTemperatures();
   Serial.print("Temperature is: ");
   Serial.println(dallas.getTempCByIndex(0));

If the button is pressed the temperature is requested and printed in the serial monitor.

   ghn.notify("You pressed the button, let me check the temperature");
   delay (1000);

We know that the button was pressed so we'll have the Google Assistant say that. This is how the state of the first sensor (the button) is spoken by the Assistant.

delay (1000);

As you might remember from the previous story we are actually using two libraries to have the Assistant speak to us. The first library sends the text to a Google service to translate it into an MP3 file and the second library plays that MP3 file. This will take some time. Therefore the delay is needed. If you send the second command directly after this without the delay the first sentence will not be spoken completely as it will be overwritten by the new MP3 file.
Just play with the delay function (and try leaving it out) so you can check for yourself what the effetct is.

command = "The temperature is ";

Here we start a new sentence that will be spoken by the Assistant.

dallas.requestTemperatures();

This does a new request for the temperature. Leave this out and you will get false readings. Actually the esp8266-google-home-notifier interferes with the DallasTemperature libarary and therefore we need to initiate it again.

command = command + String(int(dallas.getTempCByIndex(0)));
command = command + "degrees celsius";

here the sentence is completed. The complete sentence is now:
The temperature is XX degrees celsius

ghn.notify (command.c_str());

This is a real pitfall and took me some time to figure out.

The command for the library to send text to the Google Assistant is:
ghn.notify("string");

However we are not sending a string but a variable. And the library does not understand that. So this is a neat trick to tell the compiler where to find the variable in the ESP's memory and then send that chunk of bytes.

What can you use it for.

Besides the fact that this is fun to play with you can make some real projects with this.

How about attaching a vibration sensor to your cookie-jar. Let the Google assistant speak a warning when someone tries to take a cookie. Sometime ago I made an alarm in ESP-Basic with a vibration sensor but you can easily adapt it to Arduino Language (C++). You can find the information for the vibration sensor here:
http://lucstechblog.blogspot.com/2018/05/vibration-detection.html

You can make an alarm with a PIR or a radar module. Have your Assistant speak a warning when someone enters your room or mancave. The demonstration program above can easily be adapted. You can find information over the PIR and radar in the following links:
http://lucstechblog.blogspot.com/2017/01/pir-basics-movement-detection.html
http://lucstechblog.blogspot.com/2018/08/motion-detection-with-rcwl-0516-radar.html

How about putting some velostat in your chair and have your assistant warn you if you stay too long seated:
http://lucstechblog.blogspot.com/2018/04/flex-and-pressure-sensors.html

I can imagine at least half a dozen other projects which can be realised with this technique. And do not forget that the ESP32 has many IO pins so you can have it check a lot of sensors simultane.

If you make an interesting project do not hestitate to mail me. My emailadress is on the top right side of this page.

Something really Cool coming up.

While working with this libary I realised I could do something really cool with it. I could use this to ask my Assistant for the status of the sensors in my house. Not by pressing a button but by really just asking the question. It is more complicated as the above setup but really worthwile. Now I can check, and command sensors and the status of lamps etc. throughout my whole house from my lazy chair, just by asking my Assistant. Cool !! Just wait for the next part in this series.

Till then.
Have fun


Luc Volders

Saturday, April 11, 2020

Hand washing aid

For an index to all my stories click this text

I do not have to remind you that we have a world-wide crisis. So keep safe. Keep a distance to otjhers of at least 1.5 meter and wash your hands regularly for at least 20 seconds.

The hand washing however poses a problem. You need to operate the washing tap and the soap dispenser. There is however a chance that your hands are contaminated and therefore you can contaminate the soap dispenser and the washing tap.
Next to that there is the timing problem. How long is 20 seconds. When you are washing your hands it is longer as you think.

The solution to this is to use an automatic soap dispenser that offers a mixture of water and soap or even alcohol to sanitise your hands. The dispenser also has a led that lights up for 20 seconds. So you are sure that you are cleaning your hands thorough.

The objective of this build.

Well you guessed it. Build a sterile setup to wash your hands.

What I am going to show you is how to build a device that has a container with soap. Inside the container is a pump. Outside there is a sensor and a led. When your hand nears the sensor the pump will dispense some soap. After that a red led is lit and stays on for 20 seconds. So you will have a visisble feedback for the minimum time you need to wash your hands.


What you will need to build this project.

- An ESP8266 or a member of the Arduino family
- 2 220 ohm resistors for the leds
- 2 leds a red one and a green one
- 1 100 ohm resistor
- A small pump
- A TIP 120 power transistor
- An HC-SR04 ultrasonic distance sensor.
- An USB breadboard connector
- An USB power supply

And for the build itself
- a container for the soap/water mixture
- a rubber thule
- a hose that fits the pump

The rubber thule is not really required. If you do not have one just use hot glue to fix the hose to the container.
The container itself can be a large jar or anything you might find.
The hose can be found at most home improvement shops.

All the electronics and the pump can be found at your local electronics shop, on ebay or at your favorite chinese supplier. Although the last option might take much too long for this situation.
The color of the leds is not critical and neither is the TIP120. You can replace it with any other suitable power transistor or a mosfet.

The schematics.

I'll give you two versions of the schematics. The Fritzing breadboard setup and a schematic.






Real life setup.



The picture shows my prototype on a breadboard. It works as figured. I will try this setup for a short time and then build a real housing for permanent use.

The program



// Get some soap without contact and
// wash your hands timer
// Luc Volders 
// http://lucstechblog.blogspot.com/

// define pin numbers
const int trigPin = 14;  //D5
const int echoPin = 16;  //D0
const int ledGPin = 12;  //D5
const int ledRPin = 13;  //D5
const int Motorpin = 15; //D8

// define variables
long duration;
int distance;

void setup() 
  {
  pinMode(trigPin, OUTPUT); // Set trigPin as Output
  pinMode(echoPin, INPUT); // Set echoPin as Input
  pinMode(ledGreenPin, OUTPUT);
  pinMode(ledRedPin, OUTPUT);
  pinMode(Motorpin, OUTPUT);
  digitalWrite(ledGreenPin, LOW);
  digitalWrite(ledRedPin, LOW);
  digitalWrite(Motorpin, LOW);

  Serial.begin(9600); // Start the serial communication
  }

void loop() 
{
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read echoPin, 
  // return the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance= duration*0.034/2;
  // Print the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  // The distance at what the pump starts
  if (distance < 10)
  {
    digitalWrite(ledGPin, HIGH);
    digitalWrite(Motorpin, HIGH);
    // The pump runs 5 seconds then it shuts down
    delay (5000);
    digitalWrite(ledGPin, LOW);
    digitalWrite(Motorpin, LOW);
    digitalWrite(ledRPin, HIGH);
    // The led will stay on for 20 seconds
    delay (20000);
    digitalWrite(ledRPin, LOW);
  }
  delay(2000);


I dont think it will pose any problems. You can copy it and paste it into the Arduino IDE. The tricky part is the distance calculation but that works as is. I will not go into detail about this now as I just want to offer you a working project here.

Adjusting for your own needs

You can use an ESP8266, ESP32 or any member of the Arduino family when building this.  You'll only need 4 I/O pins when leaving the green led out. So an Attiny 85 could do the job.

By using an ESP32 or Arduino you could add another sensor and pump to have seperate soap and water dispensors.

I set the distance from your hands to the sensor at 10 cm. So when your hands are within that range the pump will start. Adjust this to your own build.

In the program the setting for the pump is 5 seconds. Adjust this to your own needs. This timing is dependend on the length of the hose you are using and the kind of soap mixture. So test.


Depending on the soap or mixture you are using it may be possible to have the motor run reverse for an instance so no soap or alcohol is spilled when the pump stops. You can use an H-Bridge for that. You can find a description on the H-Bridge here:
http://lucstechblog.blogspot.com/2019/02/remote-controlled-car-with-wifi.html

After the pump is shut down the led will light for 20 seconds. You could build in a small delay between the stopping of the pump and the lighting of the led. You can also alter the 20 seconds that the led will stay on. Again test and adjust to what you need.

Demonstration of the prototype





Stay at home, safe and healthy
And have fun during these miserable days.

Luc

Wednesday, April 1, 2020

Toiletroll stock calculator

For an index to all my stories click this text

What's the worst thing that can happen during the corona crisis ? Getting Covid-19 ??? No there is something even worse. Running out of toiletpaper. That is why everyone is stashing toiletpaper.

But how do you know you have enough. Guessing in these kind of situations is totally out of order, You NEED to know if you have enough stock. So therefore I created the TOILETROLL STOCK calculator.




Above shows you the screen on an Android phone. I made an APP because you certainly want to take this to your grocery store when filling up your stash. Just fill in all the fields and you will know how long your stock will last.

Where to get the app.

There are several ways to get the app.




If you have a QR code reader on your phone you can scan the QR code shown above. The QR-code program will then re-direct you to the webpage where the APP can be downloaded.

The second method is to point the browser on your phone to the following URL:

https://www.appcreator24.com/app957076

You can do this with any browser on your phone. My favorite is Firefox.




The above screen will be presented. Press the download button.



The above screen asks to confirm that you want to complete the action.



After loading you will get a notification that the APP has been loaded.



With the file program in Android you will find the APP in the download folder. Click on it and install it. When Android asks you if it is OK to do so. Just agree and install. There are no dependencies. The app does not want your location or access to your contacts. It needs no permissions at all. It does not use your data bundle and it will also work off-line.

How does the app work.

Well actually it is just a piece of Javascript code made into a web-app. As always I give you the source code here. You can download it, save it as an HTML file and use it on your browser also!!



<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body style='background-color:powderblue;'>
<body>

<style>
table 
{ 
  display: table;
  border-collapse: separate;
  border-spacing: 2px;
  border-color: gray;
}

th 
{
  text-align: left;
}

h1 
{
  color: red;
text-align: center;
}
</style>

<h1>Toiletrol stock lifespan</h1>

<table>
<tr>
<th>How many toilet rolls do you have </th>
<th><input type="text" id="myText1" maxlength="4" size="4" value="0"></th>
</tr>

<tr>
<th>How many sheets are there per roll </th>
<th><input type="text" id="myText2" maxlength="4" size="4" value="0"></th>
</tr>

<tr>
</tr>

<tr>
<th>How often do you visit the toilet per day </th>
<th><input type="text" id="myText3" maxlength="2" size="2" value="0"></th>
</tr>

<tr>
<th>How many sheets do you use per visit </th>
<th><input type="text" id="myText4" maxlength="2" size="2" value="0"></th>
</tr>

<tr>
</tr>

<tr>
<th>How many people in your household </th>
<th><input type="text" id="myText5" maxlength="2" size="2" value="0"></th>
</tr>

</table>
<br>

<p>Click the "Calculate" button to get the amount of days.</p>

<button onclick="myFunction()">Calculate</button>

<h1> Your stock wil last &nbsp<a id="demo1"></a> days</h1>



<script>
function myFunction() 
{
  var x1 = document.getElementById("myText1").value;
  var x2 = document.getElementById("myText2").value;
  var x3 = document.getElementById("myText3").value;
  var x4 = document.getElementById("myText4").value;
  var x5 = document.getElementById("myText5").value;

  var totvel = x1 * x2;
  var totvisit = x3 * x4 * x5;
  var days = totvel / totvisit;

  document.getElementById("demo1").innerHTML = Math.round(days);
}
</script>

</body>
</html>

Stay safe and healthy
and have fun.

Luc Volders