Close

Testing Has Started

A project log for Washing Machine IoT

This is a non-invasive WiFi Washing Machine Monitor

profitpRoFiT 03/13/2016 at 10:020 Comments

Okay after seeing hackaday post someone elses washing machine notifier on the page i had to go back and try to finish my project. Following their use of pushbullet and a post on the esp8266 forums i came up with the following script. I will remove my apikey, get your own ;)

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "........";

const char* host = "api.pushbullet.com";
const int httpsPort = 443;
const char* PushBulletAPIKEY = ".........................................."; //get it from your pushbullet account

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "2C BC 06 10 0A E0 6E B0 9E 60 E5 96 BA 72 C5 63 93 23 54 B3"; //got it using https://www.grc.com/fingerprints.htm


// 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.


void setup() {

  pinMode(add1,OUTPUT); //analog control
  pinMode(add2,OUTPUT); 
  pinMode(add3,OUTPUT);
  
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  
}



void loop()
{
  
  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");
  SendNotification(1);
  sendWashComplete=2; // set sent
}

void washStart()
{
  Serial.println("Wash Started");
  SendNotification(0);
  sendWashStart=2; // set sent
}



void SendNotification(bool on)
{
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  if (client.verify(fingerprint, host)) {
    Serial.println("certificate matches");
  } else {
    Serial.println("certificate doesn't match");
  }
  String url = "/v2/pushes";
  String messagebody;
  if(on) messagebody = "{\"type\": \"note\", \"title\": \"Washing Machine\", \"body\": \"Wash Complete\"}\r\n";
  else messagebody = "{\"type\": \"note\", \"title\": \"Washing Machine\", \"body\": \"Wash Started\"}\r\n";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Authorization: Bearer " + PushBulletAPIKEY + "\r\n" +
               "Content-Type: application/json\r\n" +
               "Content-Length: " +
               String(messagebody.length()) + "\r\n\r\n");
  client.print(messagebody);

  Serial.println("request sent");

  //print the response

  while (client.available() == 0);
  while (client.available())
  {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }
} 


/******************************************
*
* 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);   
}
Most of the pushbullet script i found here http://www.esp8266.com/viewtopic.php?f=29&t=7116 Thanks to DedeHai. Exactly what i needed.


This isn't completed yet but i'm running a load of wash now. Wash Started message sent once this time. To my pc and phone using the pushbullet software. Need to install on HTPC so i get notified while watch tv.


first couple runs though i was getting non stop messages. i think the 60hz was messing with my readings at full speed. didn't really notice when outputting serial. Maybe runs faster when serial is not connected? Anyways the 1500 loops per analog read fixes it i think. i set the reading to 0 and then read 1500 times to see if it ever goes above the threashold. if it does set it true and continue....maybe i should break as soon as i read high to make it faster????

I'll try to post a video tomorrow if everything is working.

Discussions