Megan Weber
Published

Smart Water

Imagine a water bottle that records how much water you drink in a day! Created by Megan Weber and Marlena Meyer.

IntermediateFull instructions provided746
Smart Water

Things used in this project

Hardware components

Photon
Particle Photon
×2
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Built 30 oz Water bottle
×1
5 mm LED: Green
5 mm LED: Green
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

HC-SR04 Schematic

LED Schematic

Code

LED Code

C/C++
LED Code
void setup() {
    
    pinMode(D0,OUTPUT); 
    digitalWrite(D0,LOW);

    Particle.subscribe("volume", myHandler);
    
}

void myHandler(const char *event, const char *data) {

    if (strcmp(data,"lightOn")) {//Code recieves message from photon one, indicating that the lid is OPENED, and to turn the lights ON
      digitalWrite(D0,HIGH);
      
}

    else if (strcmp(data,"lightOff")) {//Code recieves message from photon one, indicating that the lid is CLOSED, and to turn the lights OFF
          digitalWrite(D0,LOW);

  }
}

Ultrasonic Sensor Code

C/C++
Ultrasonic sensor code
const int triggerPin = D1;
const int echoPin = D0;
int light = D7;
int waterlevel;

unsigned long duration;
int mydistance;

void setup() {

  pinMode(triggerPin, OUTPUT);

  pinMode(echoPin, INPUT);
  
  pinMode(light,OUTPUT);
  
Particle.subscribe("waterlevel", myGoal);
  
}

void myGoal(const char *event, const char *data) {

//first to make sure that the trigger pin is off, we'll make it 
//low for 2 microseconds:

digitalWrite(triggerPin, LOW);
delayMicroseconds(2);

//next we'll send the signal for 10 microseconds and then turn 
//it off (this is the ultrasonic wave)

digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

//this gets the time (duration) which the HIGH pulse was 
//recieved and then converts it to distance

duration = pulseIn(echoPin, HIGH);

// once duration is aquired, it is converted into both a string and an integer using the following formula

waterlevel = duration*.035/2;

String mydistance = String(duration*.035/2);

//this just publishes the distance to Thingspeak

Particle.publish("mydistance", mydistance, PRIVATE);

float volume;

volume=(3.14*10.5625*waterlevel);

delay(3000);

if (volume > 30 ) {

Particle.publish("volume", "lightOn", 20);
digitalWrite(light, HIGH);
delay(5000);
digitalWrite(light, LOW);
}

// this is the situation where the other photon indicates the water needs to be filled, but this photon does not

else if (volume < 30 ) {

Particle.publish("volume", "lightOff", 20);
digitalWrite(light, HIGH);
delay(5000);
digitalWrite(light, LOW);

}
}

Credits

Megan Weber

Megan Weber

1 project • 0 followers
Thanks to Marlena Meyer.

Comments