A. Daniel J. GómezUbiMaker
Published

Solar-Powered Water Monitoring System

Build an online water monitoring system that never stops.

Full instructions provided3,911
Solar-Powered Water Monitoring System

Things used in this project

Hardware components

Fona Sim800L GPRS module
×1
Seeeduino Stalker - Waterproof Solar Kit
×1
Tight box
×1
Phidgets pH/ORP Adapter
×2
PH Sensor
×1
ORP Sensor
×1

Story

Read more

Code

file_13577.txt

C/C++
#include <DallasTemperature.h>
#include <OneWire.h>
#define Pin 2 //Pin for DATA
 
OneWire ourWire(Pin); //Pin declared like a bus for OneWire communication
 
DallasTemperature sensors(&ourWire); //instance of DallasTemperature library
 
void setup() {
delay(1000);
Serial.begin(9600);
sensors.begin(); //Init the sensors
}
 
void loop() {
sensors.requestTemperatures(); //Prepare the sensor for reading
 
Serial.print(sensors.getTempCByIndex(0)); //Read and print the temperature in Celsius degree.
Serial.println(" C");
 
delay(1000); // 1 second for delay
 
}

file_13623.txt

C/C++
/*
 Basic sketch for pH/ORP Adapter.

 This is a basic example to test the analog values from pH/ORP Adapter.

 You'll need:
 * An Arduino Uno
 * pH/ORP Adapter 1130
 
 Pinout:
 

 created 20 Feb. 2015
 by Alejandro D.J Gomez Florez for Ubidots Inc.

 This example code is in the public domain.

*/

void setup() {
  // Initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}


void loop() {
  // read the input on analog pin 0:
  int ph = analogRead(A0);
  // read the input on analog pin 1:
  int orp = analogRead(A1);
  // print out the value you read:
  Serial.println(ph);
  Serial.println(orp);
  // delay in between reads for stability
  delay(500);
}

file_13868.txt

C/C++
/*
 Basic sketch for Adafruit FONA module. Based on Adafruit FONAtest Example

 This is a basic example to read voltage value.

 You'll need:
 * An Arduino
 * A FONA Module
 
 Pinout:
 

 created 20 Feb. 2015
 by Alejandro D.J Gomez Florez for Ubidots Inc.

 This example code is in the public domain.
*/

void setup() {  
  //For Serial comunnication
  Serial.begin(115200);
  analogReference(INTERNAL);
  
  delay(2000);
}

void loop(){
  //Read the Analog Batery voltage raw data
  int battery = analogRead(A7);
  Serial.println(battery);
}

file_13879.txt

C/C++
/*

 Basic sketch for Adafruit FONA module. Based on Adafruit FONAtest Example

 This is a basic example to post a value on Ubidots with a simple
 function "sendata2Ubidots".

 You'll need:
 * An Arduino
 * An Adafruit FONA Module

 Pinout:


 created 20 Feb. 2015
 by Alejandro D.J Gomez Florez for Ubidots Inc.

 This example code is in the public domain.
*/


#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#include <stdlib.h>
#include <DallasTemperature.h>
#include <OneWire.h>

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
#define FONA_KEY 7
#define FONA_PS 8
#define Pin 10          //Pin for temperature data


char token[] = "kA8XWNQRHFrXVQakaJVY3Zg2qRU1lq";
char id1[] = "54eb894d7625423fe93f0f3c";
char id2[] = "54eb896d762542419938b415";
char id3[] = "54eb89537625423f923e0501";
char id4[] = "54eb89597625423f78057de8";



// this is a large buffer for replies

char replybuffer[255];



SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);



//For Temperature Sensor
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
int get_int_len(int);
void sendDataUbidots(void);

int value1, value2, value3, value4;
char buffer [33];



void setup() {  
  //For FONA MODULE
  Serial.begin(115200);
  fonaSS.begin(4800);
  pinMode(FONA_KEY, OUTPUT);
  pinMode(FONA_PS, INPUT);
  analogReference(INTERNAL);

  //Init the temperature sensor
  sensors.begin();

  delay(2000);
  TurnOffFona();

}



void loop() {
  flushSerial();
  TurnOnFona();
  checkFona();
  senseValues();

  while (fona.available()) {
    Serial.write(fona.read());
  }

  TurnOffFona();
  delay(600000);

}



void test(int value, char* myid){

  //Make the url string
  char url1[] = "things.ubidots.com/api/v1.6/variables/";
  char* url2 = myid;
  char url3[] = "/values?token=";
  char* url4 = token;
  int lurl = strlen(url1) + strlen(url2) + strlen(url3) + strlen(url4);

  char url[lurl];
  sprintf(url,"%s%s%s%s",url1,url2,url3,url4);
  Serial.println(url);
   
  char data1[] = "{\"value\":";
  char data2[get_int_len(value)];
  char data3[] = "}";
  itoa(value,data2,10);
  int ldata = strlen(data1) + strlen(data2) + strlen(data3);

  char data[ldata];
  sprintf(data,"%s%s%s",data1,data2,data3);
  Serial.println(data);

}



void senseValues(){
  //Prepare for the temperature sensor for lecture
  sensors.requestTemperatures();
  //Read the temperature in Celsius degree.
  value1 = sensors.getTempCByIndex(0)*100;
  //Read the Analog Batery voltage data
  value2 = analogRead(A7);
  value3 = analogRead(A0);
  value4 = analogRead(A1);

  gprsOnFona();
  sendDataUbidots(value1, id1);
  gprsOffFona();

  gprsOnFona();
  sendDataUbidots(value2, id2);
  gprsOffFona();

  gprsOnFona();
  sendDataUbidots(value3, id3);
  gprsOffFona();

  gprsOnFona();
  sendDataUbidots(value4, id4);
  gprsOffFona();  

}



void sendDataUbidots(int value, char* myid){

  uint16_t statuscode;
  int16_t length;

  char url1[] = "things.ubidots.com/api/v1.6/variables/";
  char* url2 = myid;
  char url3[] = "/values?token=";
  char* url4 = token;
  int lurl = strlen(url1) + strlen(url2) + strlen(url3) + strlen(url4);

  char url[lurl];
  sprintf(url,"%s%s%s%s",url1,url2,url3,url4);
//  Serial.println(url);

  char data1[] = "{\"value\":";
  char data2[get_int_len(value)+1];
  char data3[] = "}";
  itoa(value,data2,10);
  int ldata = strlen(data1) + strlen(data2) + strlen(data3);

  char data[ldata];
  sprintf(data,"%s%s%s",data1,data2,data3);
  Serial.print(F("http://"));
  Serial.println(url);
  Serial.println(data);
  Serial.println(F("****"));

  if(!fona.HTTP_POST_start(url, F("application/json"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {

    Serial.println("Failed!");

   }

   while (length > 0) {

     while (fona.available()) {
       char c = fona.read();
       
  #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
       loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
       UDR0 = c;
  #else
       Serial.write(c);
  #endif
       
       length--;
       if (! length) break;
     }
   }
   Serial.println(F("\n****"));
   fona.HTTP_POST_end();
    
  // flush input
  flushSerial();
  
}



void flushSerial() {
    while (Serial.available()) 
    Serial.read();
}



char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
  }

  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;

}

  

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {

  uint16_t buffidx = 0;

  boolean timeoutvalid = true;

  if (timeout == 0) timeoutvalid = false;

  

  while (true) {

    if (buffidx > maxbuff) {

      //Serial.println(F("SPACE"));

      break;

    }



    while(Serial.available()) {

      char c =  Serial.read();



      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);



      if (c == '\r') continue;

      if (c == 0xA) {

        if (buffidx == 0)   // the first 0x0A is ignored

          continue;

        

        timeout = 0;         // the second 0x0A is the end of the line

        timeoutvalid = true;

        break;

      }

      buff[buffidx] = c;

      buffidx++;

    }

    

    if (timeoutvalid && timeout == 0) {

      //Serial.println(F("TIMEOUT"));

      break;

    }

    delay(1);

  }

  buff[buffidx] = 0;  // null term

  return buffidx;

}



int get_int_len (int value){

  int l=1;

  while(value>9){ l++; value/=10; }

  return l;

}



//Check if FONA works

void checkFona(){

  // See if the FONA is responding

  if (! fona.begin(fonaSS)) {           // can also try fona.begin(Serial1) 

    Serial.println(F("Couldn't find FONA"));

    while (1);

  }

  Serial.println(F("FONA is OK"));



  //configure a GPRS APN

  fona.setGPRSNetworkSettings(F("web.vmc.net.co"), F(""), F(""));

}



//Turn on FONA

void TurnOnFona(){  

  Serial.println("Turning on Fona: ");

  while(digitalRead(FONA_PS)==LOW)

    {

    digitalWrite(FONA_KEY, LOW);

    }

    digitalWrite(FONA_KEY, HIGH);

    delay(4000);

}



//Turn off FONA

void TurnOffFona(){

  Serial.println("Turning off Fona ");

  while(digitalRead(FONA_PS)==HIGH)

  {

    digitalWrite(FONA_KEY, LOW);

    }

    digitalWrite(FONA_KEY, HIGH); 

    delay(4000);

}



//Turn on GPRS on FONA

void gprsOnFona(){

  while(!fona.enableGPRS(true));

  Serial.println(F("Turn on"));

}



//Turn off on FONA

void gprsOffFona(){

  while (!fona.enableGPRS(false));

  Serial.println(F("Turn off"));

}

Credits

A. Daniel J. Gómez

A. Daniel J. Gómez

3 projects • 13 followers
UbiMaker

UbiMaker

52 projects • 168 followers
Maker @ ubidots.com

Comments

Add projectSign up / Login