wero
3
#include <SoftwareSerial.h>
//#include <TinyGPS.h>
float lat,lon;
SoftwareSerial gprsSerial(7, 8);
//SoftwareSerial gpsSerial(4, 3);
const int buzz = 13;
//TinyGPS gps; // create gps object
void setup()
{
pinMode(buzz, OUTPUT);
gprsSerial.begin(19200);
Serial.begin(19200);
Serial.println("Config SIM900...");
delay(2000);
Serial.println("Done!...");
gprsSerial.flush();
Serial.flush();
gprsSerial.println("AT+CPIN=\"\"");
delay(25000);
toSerial();
// attach or detach from GPRS service
gprsSerial.println("AT+CGATT?");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"youinternet\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"USER\",\"\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"PWD\",\"\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=1,1");
delay(2000);
toSerial();
}
void loop()
{
lat=41.4376106;
lon=2.2249775;
//Check GPRS/GSM available
//while(gpsSerial.available()){
//CSi GPS tiene datos
//if(gps.encode(gpsSerial.read())){
//Recibir datos GPS y establecerlos en variables lat y lon
//gps.get_position(&lat,&lon);
//Print de los datos (Debug)
//TODO eliminar el debug
//Serial.print("Position: ");
//Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
//Serial.print("lon: ");Serial.println(lon); // print longitude
//}
//}
// initialize http service
gprsSerial.println("AT+HTTPINIT");
delay(3000);
toSerial();
// set http param value
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://foro.battlepvpmine.es/write_data.php?longitude=" + String(lon, 7) + "&latitude=" + String(lat, 7) + "&key=8A621E21A96A6E36\"");
delay(3000);
toSerial();
// set http action type 0 = GET, 1 = POST, 2 = HEAD
gprsSerial.println("AT+HTTPACTION=1");
delay(6000);
toSerial();
gprsSerial.println("");
gprsSerial.println("AT+HTTPTERM");
toSerial();
delay(3000);
gprsSerial.println("");
delay(10000);
///////////////////////////////////////////////
gprsSerial.println("AT+HTTPINIT"); //init the HTTP request
delay(3000);
toSerial();
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://foro.battlepvpmine.es/get_buzzing.php?key=8A621E21A96A6E36\"");// setting the httppara,
//the second parameter is the website from where you want to access data
delay(3000);
toSerial();
gprsSerial.println();
gprsSerial.println("AT+HTTPACTION=0");//submit the GET request
delay(8000);//the delay is important if the return datas are very large, the time required longer.
toSerial();
gprsSerial.println("AT+HTTPREAD=0,20");
delay(6000);
toSound();
/*if(Serial.read().equals("1"))
{
digitalWrite(buzz, HIGH);
Serial.println("Funciona");
}
else
{
digitalWrite(buzz,LOW);
Serial.println("No Funciona");
}
*/
gprsSerial.println("");
delay(3000);
gprsSerial.println("AT+HTTPTERM");// terminate HTTP service
toSerial();
}
void toSound(){
while (gprsSerial.available() != 0 ) {
int a = Serial.write(gprsSerial.println("AT+HTTPREAD=0,20"));
Serial.println("Esto es lo que obtiene: ");
Serial.println(a);
}
}
void toSerial()
{
while(gprsSerial.available()!=0)
{
Serial.write(gprsSerial.read());
}
}