Close

Google Home Version /w TTS

A project log for Washing Machine IoT

This is a non-invasive WiFi Washing Machine Monitor

profitpRoFiT 03/10/2021 at 04:560 Comments

Okay so i found someones code on how to send tts to google homes. after some major modifications and copy paste from my old code i have it working.

Here is the github location i borrowed it from https://github.com/debsahu/GoogleSay

his was cool and overkill for what i needed. Maybe

//https://github.com/horihiro/esp8266-google-home-notifier

 was all i really needed to look at.

Now when the wash completes it will tell me on my google mini

I think i will add a loop where it keeps telling me once a minute until i open the wash door and the light goes off. Otherwise i may miss the notification

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include   //https://github.com/horihiro/esp8266-google-home-notifier
//https://github.com/horihiro/esp8266-google-tts

const char* ssid     = "SSID";
const char* password = "PASSWORD";

const char GoogleHomeName[] = "NAME OF GOOGLE HOME DEVICE";  //Must match the name on Google Home APP


// analog portion

int add1=0;
int add2=5;
int add3=2;
int an=A0;

const int triggerLevel =  200; // level to trigger change in ledstate

int sendWashComplete = 0; // set 0 at start, 1 when trigger is hit, 2 once sent, back to 0 during if start has started or all leds off like when wash is done and lid is opened.
int sendWashStart = 0; // set 0 at start, 1 when trigger is hit, 2 once sent, back to 0 during if complete or all leds off
int ledStates = 0; // use bitwise for keeping track. one number to compare against
int lastLedState = 0; // set same at start. only check once they change in a loop.


GoogleHomeNotifier ghn;

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


String ipToString(IPAddress ip){
  String s="http:// ";
  for (int i=0; i<4; i++)
    s += i  ? " dot " + String(ip[i]) : String(ip[i]);
  return s;
}

void setup(void) {

  Serial.begin(115200);
  Serial.println("trying to connect...");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid,password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  connectToGH();

  String init_text = "Washing Machine Application has started";

  if (ghn.notify(init_text.c_str()) != true) {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.println("Done.");

  pinMode(add1,OUTPUT); //analog control
  pinMode(add2,OUTPUT); 
  pinMode(add3,OUTPUT);

  

}

void loop(void)
{
  byte i;
  for( i=0;i<=5;i++)
  {
    setAddress(i); // switch analog port before reading
    delay(50); // give 50ms to let analog switch complete.
    bitWrite(ledStates, i, 0); // set 0 before loop
    for(int x=0;x<1500;x++) // comes out to about 1 second scan rate for all 6 leds with given switch delays etc...
    {
      if(analogRead(an)>triggerLevel) // read level output and compare
      {
        bitWrite(ledStates, i, 1); // if we see it go high after anytime during the 1500 loops, set high or LED ON STATE. This should eliminate 50/60hz led issues on the wshing machine...ugh....pain.
      }
    }    
  }

  Serial.println(ledStates); // simpler i guess

  if(ledStates!=lastLedState) // if there was a change from last loop do something
  {
    switch(ledStates)
    {
      case 16 : // wash complete state [0,1,0,0,0,0] = 16
        if(sendWashComplete == 0)
        {
          sendWashComplete = 1; // 1 means we need to send notification.
          sendWashStart = 0; // 0 means it can be triggered again
        }
      break;

      case 33 : // 33 is start and locked. sometimes both are on at same time.
        if(sendWashStart == 0)
        {
          sendWashStart = 1; // 1 means we need to send notification.
          sendWashComplete = 0; // 0 means it can be triggered again
        }
      break;
      
      case 1 : // 1 is start only
        if(sendWashStart == 0)
        {
          sendWashStart = 1; // 1 means we need to send notification.
          sendWashComplete = 0; // 0 means it can be triggered again
        }
      break;
      
      case 0 : // clear notifications if all leds are off machine is off or completed and lid opened.
        sendWashComplete = 0; // 0 means it can be triggered again
        sendWashStart = 0; // 0 means it can be triggered again
      break;
      default : {}
    }
  }

  if(sendWashComplete == 1) washComplete();
  if(sendWashStart == 1) washStart();
  
  lastLedState = ledStates; // at end of loop so we can test next loop
}


void washComplete()
{
  Serial.println("Wash Complete");
 String init_text = "The wash has finished";

  if (ghn.notify(init_text.c_str()) != true) {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.println("Done.");
}

void washStart()
{
  Serial.println("Wash Started");
 String init_text = "The wash is starting";

  if (ghn.notify(init_text.c_str()) != true) {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.println("Done.");
}


/******************************************
*
* setAddress(byte i)
*
* Take byte value and output the bits, 3 bit address
*
*******************************************/
void setAddress(byte i)
{
  if(i & B00000001) digitalWrite(add1,HIGH);
  else  digitalWrite(add1,LOW);
  
  if(i & B00000010) digitalWrite(add2,HIGH);
  else  digitalWrite(add2,LOW);
  
  if(i & B00000100) digitalWrite(add3,HIGH);
  else  digitalWrite(add3,LOW);   
}

Discussions