Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Edgar Antonio Arteaga Ricoalexcyanez
Created June 30, 2018

Smart Pipes for Smart Cities

IoT Sense, + Notifications Systems + AWS = SAVE WATER

413

Things used in this project

Hardware components

DevKit Sigfox NXTIoT
×1
Water Sensor
×2
Solenoid Valve
×3
Arduino 4 Relays Shield
Arduino 4 Relays Shield
×1
12V/29A Power Supply
OpenBuilds 12V/29A Power Supply
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Pipes
×5
myDigital Protoboard for NI myDAQ & myRIO
Digilent myDigital Protoboard for NI myDAQ & myRIO
×1

Software apps and online services

Windows 10
Microsoft Windows 10
AWS IoT
Amazon Web Services AWS IoT
AWS DynamoDB
Amazon Web Services AWS DynamoDB
Sigfox
Sigfox
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Dremel Mototool
Screwdrivers
tweezers
Tape
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

Sensors Box

All the Sensors and Sigfox Protocol are in the box

Schematics

Schematic Smart Pipe

Code

Smart Pipes with Sigfox

Arduino
unsigned long delayTime;
const int boton=6;  // THIS IS A BLUE BOTTON

String bufer; //variable to save our payload
String bufer2="\n";   

const int led6=8;
char RespuestaSigfox[50];
char ID[51];
char PAC[51];
char res[51];

int relay1 = 2;   // Relay 2 from Shield
int relay2 = 3;   // Relay 3 from Shield
int relay3 = 4;   // Relay 4 from Shield
int porcentaje1=0;   // Percent of water dectected of sensor #1
int porcentaje2=0;   // Percent of water dectected of sensor #2
int porcentaje3=0;   // Percent of water dectected of sensor #3
int valor_sensor1 = 0;
int valor_sensor2 = 0;
int valor_sensor3 = 0;
int nivel_de_agua;
float porcentaje;
const int sensor_nivel_agua1 = A0;
const int sensor_nivel_agua2 = A1;
const int sensor_nivel_agua3 = A2;

int val1=0;
int val2=0;
int val3=0;

void setup() {
  
  Serial.begin(9600);   
  #ifdef SigfoxDeb
  mySerial.begin(9600);    // Open Serial with Sigfox
  #endif
  pinMode(boton, INPUT);
  pinMode(7, OUTPUT);
  pinMode(led6, OUTPUT);
  #ifdef SigfoxDeb
  mySerial.println("\n\n\n\n\n\n\n\n\rInicio");
  #endif
  pinMode(relay1, OUTPUT);    // Control relay_1
  pinMode(relay2, OUTPUT);    // Control relay_2
  pinMode(relay3, OUTPUT);    // Control relay_3
}

void loop() {

  cerrar_valvula1();   // Function to open the solenoid valve #1
  cerrar_valvula2();   // Function to open the solenoid valve #2
  cerrar_valvula3();   // Function to open the solenoid valve #3
  
  val1= sensor_agua1();
  val2= sensor_agua2();
  val3= sensor_agua3();

  if(val1>=40){                   // CHECK VALVE #1
    Serial.println("TRY_1");
    abrir_valvula1();             // CLOSE VALVE #1
    printValues();                // SEND SIGFOX BACKEND
    delay(5000);
    Serial.println("SEND_1");
    delay(1000);
    cerrar_valvula1();            // OPEN VALVE #1
  }
  
  if(val2>=40){                   // CHECK VALVE #2
    Serial.println("TRY_2");
    abrir_valvula2();             // CLOSE VALVE #2
    printValues();                // SEND SIGFOX BACKEND
    delay(5000);
    Serial.println("SEND_2");
    delay(1000);
    cerrar_valvula2();            // OPEN VALVE #2
  }
  
  if(val3>=40){                   // CHECK VALVE #3
    Serial.println("TRY_3");
    abrir_valvula3();             // CLOSE VALVE #3
    printValues();                // SEND SIGFOX BACKEND
    delay(5000);
    Serial.println("SEND_3");
    delay(1000);
    cerrar_valvula3();            // OPEN VALVE #3
  }
  delay(2000);
  val1=0;
  val2=0;
  val3=0;
}

void printValues() {
  
    val1= sensor_agua1();
    val2= sensor_agua2();
    val3= sensor_agua3();

    //-----------------------------------------------------
    //AT$SF= command to sendo info. to sigfox
    bufer="AT$SF=";   //Maximo 12 bytes
    //-----------------------------------------------------
    add_int(val1);     //add 1 byte to our payload
    add_int(val2);     //add 1 byte to our payload
    add_int(val3);     //add 1 byte to our payload
    //----------------------------------------------------
    send_message(bufer); //send information
    delay(500);
}

void add_float(float var1)//function to add floats to the payload
{
  byte* a1 = (byte*) &var1;
  String str1;
  //add command AT$SF= our info. to send it.
  for(int i=0;i<4;i++)
  {
    str1=String(a1[i], HEX);
    if(str1.length()<2)
    {
      bufer+=0+str1;
    }
    else
    {
      bufer+=str1;
    }
  }
}

void add_int(int var2) //function to add int to the payload (until 255)
{
  byte* a2 = (byte*) &var2;
  String str2;
  str2=String(a2[0], HEX);
  if(str2.length()<2)
  {
    bufer+=0+str2;
  }
  else
  {
    bufer+=str2;
  }
}
void send_message(String payload)
{
  Serial.println("MESSAGE_to send");
  bufer+=bufer2;
  //*******************
  //Enable Sigfox module
  digitalWrite(7, HIGH);
  delay(1000);
  //Reset canal to send message in the correct frequency
  Serial.print("AT$RC\n"); 
  //************************
  //Send information by sigfox
  Serial.print(bufer);
  delay(8000);
  //disable Sigfox module
  digitalWrite(7, LOW);
  delay(500);
}
//-----------------------------------WATER SENSOR---------------------------------------------
int sensor_agua1(){
  valor_sensor1 = analogRead(sensor_nivel_agua1);   //Read  analog sensor #1 
  Serial.print("WATER_SENSOR_1:  ");
  //nivel_de_agua = valor_sensor1*100/1024;
  porcentaje1 = map(valor_sensor1,0,1024,0,90);     //Mapping the value of sensor
  Serial.print(porcentaje1);
  Serial.println("%");
  delay(500);
  return porcentaje1;                               //Return the value of sensor #1
}
int sensor_agua2(){
  valor_sensor2 = analogRead(sensor_nivel_agua2);   //Read  analog sensor #2
  Serial.print("WATER_SENSOR_2:  ");
  //nivel_de_agua = valor_sensor1*100/1024;
  porcentaje2 = map(valor_sensor2,0,1024,0,90);     //Mapping the value of sensor
  Serial.print(porcentaje2);
  Serial.println("%");
  delay(500);
  return porcentaje2;                               //Return the value of sensor #2
}
int sensor_agua3(){
  valor_sensor3 = analogRead(sensor_nivel_agua3);   //Read  analog sensor #3
  Serial.print("WATER_SENSOR_3:  ");
  //nivel_de_agua = valor_sensor1*100/1024;
  porcentaje3 = map(valor_sensor3,0,1024,0,90);     //Mapping the value of sensor
  Serial.print(porcentaje3);
  Serial.println("%");
  delay(500);
  return porcentaje3;                               //Return the value of sensor #3
}

//------------------------------------OPEN------------------------------------------------
void cerrar_valvula1(){                     // open valve #1    
  digitalWrite(relay1,HIGH);
  Serial.println("VALVULA 1 CERRADA");
}
void cerrar_valvula2(){                     // open valve #2
  digitalWrite(relay2,HIGH);
  Serial.println("VALVULA 2 CERRADA");
}
void cerrar_valvula3(){                     // open valve #3
  digitalWrite(relay3,HIGH);
  Serial.println("VALVULA 3 CERRADA");
}
//--------------------------------------------------------------

//-----------------------------CLOSE----------------------------
void abrir_valvula1(){                      // close valve #1
  digitalWrite(relay1,LOW);
  Serial.println("VALVULA 1 ABIERTA");
}
void abrir_valvula2(){                      // close valve #2
  digitalWrite(relay2,LOW);
  Serial.println("VALVULA 2 ABIERTA");
}
void abrir_valvula3(){                      // close valve #3
  digitalWrite(relay3,LOW);
  Serial.println("VALVULA 3 ABIERTA");
}
//--------------------------------------------------------------

Credits

Edgar Antonio Arteaga Rico

Edgar Antonio Arteaga Rico

1 project • 0 followers
alexcyanez

alexcyanez

0 projects • 0 followers

Comments