Tania DamhaJames BennettKalpesh SolankiDiego Santillan
Published

Cloud Based Smart Water Meter

Team H2FLOW brings a smart metering network to water deprived Californians. The H2FlOW network allows users to optimize their water usage.

Work in progress2,175
Cloud Based Smart Water Meter

Things used in this project

Hardware components

Intel Edison
Intel Edison
×1
LV Maxsonar Ulrasonic Sensor
×1
Servo Continious Rotation
×1
Solidworks Design custom parts. 3D Printed
×1
Squarespace Built a Website
×1
Photoshop and Illustrator
×1

Software apps and online services

Microsoft Azure
Microsoft Azure

Story

Read more

Schematics

Schematic

H20 schematic

Code

Code H2Flow

C/C++
#include <WiFi.h>
#include <Servo.h>
#include <Time.h>

Servo myservo;

const int pwPin = 7; 
//variables needed to store values
long pulse, inches, cm;
long closeValue = 15;
long prevValue = 0;
long openCount = 0;
int mode = 0;
int prevMode = 0;
long oldTime = 0;
int servoPin = 9;


char ssid[] = "rhubarbstudios"; //  your network SSID (name) 
char pass[] = "buildcoolstuff";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Initialize the Ethernet client library 
// with the IP address and port of the server  
// that you want to connect to (port 80 is default for HTTP): 
WiFiClient client; 

// if you don't want to use DNS (and reduce your sketch size) 
// use the numeric IP instead of the name for the server: 
//IPAddress server(204,79,197,200);  // numeric IP for Bing (no DNS) 
char server[] = "waterhack.azure-mobile.net";  

// Azure Mobile Service table name
// The name of the table you created
const char *table_name = "flow";

// Azure Mobile Service Application Key

// You can find this key in the 'Manage Keys' menu on the dashboard

const char *ams_key = "RuxsfiNqqXClrtdUXFfWMaHzWcTmyW31";
char buffer[64];

void setup() {
   myservo.attach(servoPin); 
  //This opens up a serial connection to shoot the results back to the PC console
  Serial.begin(9600);
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    // don't continue:
    while(true);
  } 

  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    WiFi.disconnect();
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  }
  

}

void loop() {
  
  pinMode(pwPin, INPUT);
    //Used to read in the pulse that is being sent by the MaxSonar device.
  //Pulse Width representation with a scale factor of 147 uS per Inch.

  pulse = pulseIn(pwPin, HIGH);
  //147uS per inch
  inches = pulse/147;
  //change inches to centimetres
  cm = inches * 2.54;
  
  if(cm <= closeValue && prevValue > closeValue){
    mode = 1;
    openCount++;
  }
  else if(cm > closeValue && prevValue <= closeValue){
    mode = 0;
  }
  
  if(mode==1 && prevMode==0){
  //  Serial.println("OPEN");
    openCount++;
  }
  else if(mode==0 && prevMode==1){
  //  Serial.println("CLOSE");
  }
  
  time_t curtime = now();
  long _cur = curtime;
  //Serial.println(_cur);
  if((_cur-oldTime)==10){
    int rpm = openCount*3;
    Serial.println(rpm);
    send_request(rpm/10);
    wait_response();
    read_response();
    end_request();
    
    openCount = 0;
    oldTime = _cur;
  }
  
  
  prevValue = cm;
  prevMode = mode;
//  Serial.print(inches);
 // Serial.print("in, ");
 // Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  delay(10);
}

void send_request(int value)
{
  Serial.println("\nconnecting...");

  if (client.connect(server, 80)) {

    Serial.print("sending ");
    Serial.println(value);

    // POST URI
    sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name);
    client.println(buffer);

    // Host header
    sprintf(buffer, "Host: %s", server);

    client.println(buffer);

    // Azure Mobile Services application key
    sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);

    client.println(buffer);

    // JSON content type
    client.println("Content-Type: application/json");

    // POST body
    sprintf(buffer, "{\"Volume\": %d}", value);

    // Content length
    client.print("Content-Length: ");

    client.println(strlen(buffer));
    // End of headers
    client.println();

    // Request body
    client.println(buffer);

  } else {
    Serial.println("connection failed");
  }
}

void wait_response()
{
  while (!client.available()) {
    if (!client.connected()) {
      return;
    }
  }
}

/*
** Read the response and dump to serial
*/

void read_response()
{
  bool print = true;
  while (client.available()) {

    char c = client.read();
    
    // Print only until the first carriage return
    if (c == '\n')
      print = false;
      
    if (print)
      Serial.print(c);
  }

}

void end_request()
{
  client.stop();
}

Credits

Tania Damha

Tania Damha

1 project • 7 followers
James Bennett

James Bennett

1 project • 2 followers
I tinker. I play. I create.
Kalpesh Solanki

Kalpesh Solanki

1 project • 3 followers
Diego Santillan

Diego Santillan

1 project • 3 followers
Los Angeles City College student. Electrical Engineering major.

Comments