Вы находитесь на странице: 1из 6

//datos iniciales para la electrovalvula

const int buttonValvula = 5; // the number of the pushbutton pin


const int electrovalvula = 3; // the number of the LED pin

// variables will change:


int buttonState = 0; // variable for reading the pushbutton status

//datos iniciales para el sensor de flujo

const int sensor = 7; // Pin digital para el sensor de flujo YF-S201


int litros_Hora; // Variable que almacena el caudal (L/hora)
volatile int pulsos = 0; // Variable que almacena el n�mero de pulsos
unsigned long tiempoAnterior = 0; // Variable para calcular el tiempo
transcurrido
unsigned long pulsos_Acumulados = 0; // Variable que almacena el n�mero de pulsos
acumulados
float litros; // // Variable que almacena el n�mero de litros acumulados

//datos iniciales para el CLIENTE SERVIDOR

int8_t answer;
int onModulePin= 2; // SUBE LOS DATOS AL SERVIDOR

char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0;

char pin[]="1111";
char apn[]="claro.com.ec";
char user_name[]="jony";
char password[]="jony.321";
char url[ ]="http://medidor.enjambre.ec/api/medicion/0924638469/300.6/";

/***********************************************************************/

// DECLARACION DE FUNCIONES
int funcion_valvula(int buttonValvula, int electrovalvula);
int funcion_sensorFlujo(int litros_Hora);

// Rutina de servicio de la interrupci�n (ISR)


void flujo()
{
pulsos++; // Incrementa en una unidad el n�mero de pulsos
}

/****************************************************************************/
//FUNCIONES
int funcion_valvula(int buttonValvula, int electrovalvula) {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonValvula);

// check if the pushbutton is pressed.


// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(electrovalvula, HIGH);
return 1;
} else {
// turn LED off:
digitalWrite(electrovalvula, LOW);
return 0;
}
}

int funcion_sensorFlujo(int litros_Hora){


if(millis() - tiempoAnterior > 1000)
{
// Realizo los c�lculos
tiempoAnterior = millis(); // Actualizo el nuevo tiempo
pulsos_Acumulados += pulsos; // N�mero de pulsos acumulados
litros_Hora = (pulsos * 60 / 7.5); // Q = frecuencia * 60/ 7.5 (L/Hora)
Serial.println("Litros por hora\n");
Serial.print(litros_Hora);
litros = pulsos_Acumulados*1.0/450; // Cada 450 pulsos son un litro
Serial.println("Litros \n");
Serial.print(litros);
//NO OLVIDES DE ENVIAR LOS LITROS AL SERVIDOR
return litros;
pulsos = 0; // Pongo nuevamente el n�mero de pulsos a cero
}
}
// FUNCIONES PARA EL CLIENTE SERVIDOR
void power_on(){

uint8_t answer=0;

// checks if the module is started


answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);

// waits for an answer from the module


while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout)


{

uint8_t x=0, answer=0;


char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string


delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer


Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1,


char* expected_answer2, unsigned int timeout){

uint8_t x=0, answer=0;


char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command

x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}
/
***********************************************************************************
********************/
/
***********************************************************************************
********************/
//PROGRAMA EN DESARROLLO

void setup() {

// para la electrovalvula
pinMode(electrovalvula, OUTPUT);
pinMode(buttonValvula, INPUT);

//para el sensor de flujo


pinMode(sensor, INPUT_PULLUP); // Pin digital como entrada con conexi�n PULL-UP
interna
interrupts(); // Habilito las interrupciones
// Interrupci�n INT0, llama a la ISR llamada "flujo" en cada flanco de subida en
el pin digital 2
attachInterrupt(digitalPinToInterrupt(sensor), flujo, RISING);
tiempoAnterior = millis(); // Guardo el tiempo que tarda el ejecutarse el setup

//CONFIGURACION PARA CLIENTE - SERVIDOR

pinMode(onModulePin, OUTPUT);
Serial.begin(115200);

Serial.println("EMPEZANDO LA CONEXION CON LA WEB...");


power_on();

delay(3000);

//ajustes de PIN del chip


snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
sendATcommand(aux_str, "OK", 2000);

delay(3000);

while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);

// ajustar el APN, usuario y contrase�a


sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn);
sendATcommand(aux_str, "OK", 2000);

snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name);


sendATcommand(aux_str, "OK", 2000);

snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password);


sendATcommand(aux_str, "OK", 2000);

while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)


{
delay(5000);
}

void loop()
{
// Inicializa el servidor http
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// ajuste del parametro CID
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Ajuste de url
snprintf(aux_str, sizeof(aux_str), "AT+HTTPPARA=\"URL\",\"%s\"", url);
answer = sendATcommand(aux_str, "OK", 5000);
if (answer == 1)
{
// EMPEZAR LA ACCION GET
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
if (answer == 1)
{
x=0;
do{
sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
{
data_size = 0;
while(Serial.available()==0);
aux = Serial.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Serial.available()==0);
aux = Serial.read();
}while(aux != 0x0D);

Serial.print("Datos recibidos: ");


Serial.println(data_size);

if (data_size > 0)
{
while(Serial.available() < data_size);
Serial.read();

for (int y = 0; y < data_size; y++)


{
data[x] = Serial.read();
x++;
}
data[x] = '\0';
}

else
{
Serial.println("Descarga finalizada\n");
}
}
else if (answer == 2)
{
Serial.println("Error desde el HTTP");
}
else
{
Serial.println("No hay datos disponibles\n");
data_size = 0;
}

sendATcommand("", "+HTTPACTION:0,200", 20000);


}while (data_size > 0);

Serial.print("Data received: ");


Serial.println(data);
}
else
{
Serial.println("Error getting the url");
}
}
else
{
Serial.println("Error setting the url");
}
}
else
{
Serial.println("Error setting the CID");
}
}
else
{
Serial.println("Error initializating");
}

sendATcommand("AT+HTTPTERM", "OK", 5000);

delay(5000);
}

Вам также может понравиться