[SOLVED] GPRS module not send location (lat & lang)


#1

hy, please help me.
I have project gps. I’ve tried several time and always can not send data lat and long to ubidots.
this is my program
please check it and help me

#include <SoftwareSerial.h>
#include <String.h>
#include "TinyGPS.h"

SoftwareSerial mySerial(6, 7);                                                      //your pins to serial communication

TinyGPS gps;
#define GPS_TX_DIGITAL_OUT_PIN 4
#define GPS_RX_DIGITAL_OUT_PIN 5

long startMillis;
long secondsToFirstLocation = 0;

#define DEBUG

float latitude = 0.0;
float longitude = 0.0;

String Location= "";
int value1=0;
 
int valor;
//-------------------------------------------------------------
//---------------------Ubidots Configuration-------------------s
//-------------------------------------------------------------
String token = "pIUs3esm2UYQTV3JW0A1PWasZfHccU";      //your token to post value
String idvariable = "5730507d7625424b921a8ebe";                                     //ID of your variable

int PowerKeySIM900=9;
void setup()
{
   //#ifdef DEBUG8
  Serial.begin(19200);
  //#endif
  
  // Serial1 is GPS
  Serial1.begin(9600);

  //serial module gsm
  mySerial.begin(9600);
  delay(1000);
  
  // prevent controller pins 5 and 6 from interfering with the comms from GPS
  pinMode(GPS_TX_DIGITAL_OUT_PIN, INPUT);
  pinMode(GPS_RX_DIGITAL_OUT_PIN, INPUT);

  pinMode(PowerKeySIM900,OUTPUT);
  //SIM900power();
  //delay(10000);
  
  startMillis = millis();
 Serial.println("Starting"); // if uncoment untuk ditampilkan pada serial
}

void loop ()
{ //String value ="";
 Send2Ubidots(value1);
 value1++;
}

boolean Send2Ubidots(int value)
{
  int num = 0;
  String var = "";
  String le = "";
  readLocation();              // Update the location value from the GPS feed
  //var="{\"value\":"+ value + ", \"context\":"+ location + "}";
  var="{\"value\": " && value + ", \"context\":"+ Location + "}";
  num=var.length();                                       // How long is the payload
  le=String(num);                                             //this is to calcule the length of var
  
  //////////////////////////////////////////
  for(int i = 0;i<7;i++)
  {
    mySerial.println("AT+CGATT?");                                                   //this is made repeatedly because it is unstable
    delay(2000);
    ShowSerialData();
  }
  mySerial.println("AT+CSTT=\"www.xlgprs.net\"");                                    //replace with your providers' APN
  delay(1000);
  ShowSerialData();
  mySerial.println("AT+CIICR");                                                      //bring up wireless connection
  delay(3000);
  ShowSerialData();
  mySerial.println("AT+CIFSR");                                                      //get local IP adress
  delay(2000);
  ShowSerialData();
  mySerial.println("AT+CIPSPRT=0");
  delay(3000);
  ShowSerialData();
  mySerial.println("AT+CIPSTART=\"tcp\",\"things.ubidots.com\",\"80\"");             //start up the connection
  delay(3000);
  ShowSerialData();
  mySerial.println("AT+CIPSEND");                                                    //begin send data to remote server
  delay(3000);
  ShowSerialData();
  mySerial.print("POST /api/v1.6/variables/"+idvariable);
  delay(100);
  ShowSerialData();
  mySerial.println("/values HTTP/1.1");
  delay(100);
  ShowSerialData();
  mySerial.println("Content-Type: application/json");
  delay(100);
  ShowSerialData();
  mySerial.println("Content-Length: "+le);
  delay(100);
  ShowSerialData();
  mySerial.print("X-Auth-Token: ");
  delay(100);
  ShowSerialData();
  mySerial.println(token);
  delay(100);
  ShowSerialData();
  mySerial.println("Host: things.ubidots.com");
  delay(100);
  ShowSerialData();
  mySerial.println();
  delay(100);
  ShowSerialData();
  mySerial.println(var);
  delay(100);
  ShowSerialData();
  mySerial.println();
  delay(100);
  ShowSerialData();
  mySerial.println((char)26);
  delay(7000);
  mySerial.println();
  ShowSerialData();
  mySerial.println("AT+CIPCLOSE");                                                //close the communication
  delay(1000);
  ShowSerialData();
}

void ShowSerialData()
{
  while(mySerial.available()!=0)
  Serial.write(mySerial.read());
}

void readLocation()
{
  bool newData = false;
  unsigned long chars = 0;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (Serial1.available())
    {
      int c = Serial1.read();
//      Serial.print((char)c); // if you uncomment this you will see the raw data from the GPS
      ++chars;
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }
  
  if (newData)
  {
/*
    // we have a location fix so output the lat / long and time to acquire
    if(secondsToFirstLocation == 0){
      secondsToFirstLocation = (millis() - startMillis) / 1000;
      Serial.print("Acquired in:");
      Serial.print(secondsToFirstLocation);
      Serial.println("s");
    }
*/    
    
    unsigned long age;
    gps.f_get_position(&latitude, &longitude, &age);
    
    latitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : latitude;
    longitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : longitude;
  /*  
    Serial.print("Location: ");
    Serial.print(latitude, 6);
    Serial.print(" , ");
    Serial.print(longitude, 6);
    Serial.println("");
   */ 


   String Lat  = String (latitude,6);
   String Long = String (longitude,6);
   Location = "{\"lat\":" + String(Lat) + ",\"lng\":" + String(Long) + "}";
   Serial.println(Location);
  /*  
    char Lat[10];
    char Long[10];
    dtostrf(latitude,1,2,Lat);
    dtostrf(longitude,5,2,Long);
    location = "{\"lat\":" + String(Lat) + ",\"lng\":" + String(Long) + "}";
    Serial.println(location);
   */
  }
  
  if (chars == 0){
    // if you haven't got any chars then likely a wiring issue
    Serial.println("Check wiring");
  }
  else if(secondsToFirstLocation == 0){
    // still working
  }
}
//===================================================================================//
//===================================================================================//
void SIM900power() // software berfungsi sama seperti memencet tombol power pada shield
{
digitalWrite(PowerKeySIM900, HIGH);
delay(1000);
digitalWrite(PowerKeySIM900, LOW);
delay(5000);
}

#2

Hey! @sandipurwiro.
you could try with the Ubidots GPRS library!
In the Ubidots documentation there are all steps, find it here:
http://ubidots.com/docs/devices/gprs.html
And use this simple code to send values:

#include <Ubidots_Arduino_GPRS.h>
#include <SoftwareSerial.h> 
//Serial Relay - Arduino will patch a 
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART 
#define APN "Your_apn_of_your_SIM_here" 
#define USER "Your_username_here"  // If your apn doesnt have username just put ""
#define PASS "Your_password_here"  // If your apn doesnt have password just put ""
#define TOKEN "Your_token_here"  // Replace it with your Ubidots token
#define ID "Your_id_here" // Replace it with your Ubidots' variable ID
// You can send 1 to 10 variable at the same time
#define ID1 "Your_id_here" // Replace it with your variable ID
#define ID2 "Your_id_here" // Replace it with your variable ID
#define ID3 "Your_id_here" // Replace it with your variable ID


Ubidots client(TOKEN);  
  
void setup() {
  Serial.begin(19200);             // the Serial port of Arduino baud rate.  
  client.powerUpOrDown();
  client.setApn(APN,USER,PASS);
}

void loop() {
  float value_1 = analogRead(A0);
  float value_2 = analogRead(A1);
  float value_3 = analogRead(A2);

  // To send a value with a context is like: (ID,value,"NAME_CONTEXT:VALUE_CONTEXT","")
  // If you don't want to send any context only put "" like this (ID,value,"","")
  // Example with context:
  // client.add(ID1, value_1, "lat:9.786589", "lng:1.8688797");

  client.add(ID1, value_1, "lat:9.786589", "lng:1.8688797");
  client.add(ID2,value_2, "", "");
  client.add(ID3,value_3, "", "");

  client.sendAll();
}

#3

hy metavix.
I have tried but the dead gprs module. can not turn on the signal. therefore I tried with other programs


#4

@sandipurwiro
Maybe the GPRS shield is off, GPRS shield has a jumper to turn on the module. In this documentation you can read about it.
http://ubidots.com/docs/devices/gprs.html

Best regards,
Metavix


#5

How should we type python requests in python??Should we type inside the print() as shown here??II’m new to this…please help me and please also tell about sending the location data along with the sensor value to dashboard using http requests. I have seen the Rest API Reference but I could not understand…


#6

Greetings @Lakshana, you can find an example for sending data using python here: http://help.ubidots.com/developer-guides/send-data-to-ubidots-using-python-requests, also attached you can find an example python script for sending latitude and longitude.

Hope it helps you.

post_example.py.zip (798 Bytes)