0

A few days ago I posted a thread as I was not being able to send data to my database online. It worked when I changed the GPS module. Now I am trying to retrieve data from the same Server and i'm not being able to do it. The PHP code parses the outputs the data as string and i'm not being able to receive it.

when I visit the URL I am able to see the output on a browser window.

I will Basically get a address which i plan to use substring into 3 lines and display on an 20x4 LCD. is there a better way to go about it?

Thank You!

Arduino Code

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <Wire.h>
#include <DS1307new.h>
#include <LiquidCrystal_I2C.h>
#include "LiquidCrystal.h" // For LCD
#include "LiquidCrystal_I2C.h"

#define DEBUG true

TinyGPS gps;

SoftwareSerial sGPS(10,11);

LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);  // SET I2C Address

uint16_t startAddr = 0x0000;  //Start address to store in NVRam
uint16_t lastAddr;        //New address to store in NVRam
uint16_t TimeIsSet = 0xaa55;  //Helper that time must not be set again

int currentDay = 0;
int currentMonth = 0;
int currentYear = 0;
int currentHour = 0;
int currentMinute = 0;

float flat, flon;
float previousFLat = 0.0;
float previousFLon = 0.0;

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;    //call Cluster
const int buttonPin6 = 7;    //call User

const int ledPin1 =  22;
const int ledPin2 =  24;
const int ledPin3 =  26;
const int ledPin4 =  28;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;
int button6State = 0;


String rcv=""; //Variable in which the server response is recorded.

void setup()
{
  Serial.begin(9600);
  sGPS.begin(9600);
  Serial1.begin(9600);
  lcd.begin(20,4);  // Initialinze LCD.
  lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
  lcd.setBacklight(HIGH);

  /*
  Control the clock.
  Clock will only be set if NV-RAM Address does not contain 0xaa.
  DS1307 should have a battery backup.
  */

  RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
  if (TimeIsSet != 0xaa55)
  {
    RTC.stopClock();

    RTC.fillByYMD(2011,4,8);
    RTC.fillByHMS(22,7,0);

    RTC.setTime();
    TimeIsSet = 0xaa55;
    RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
    RTC.startClock();
  }
  else
  {
    RTC.getTime();
  }

  /*
   Control Register for SQW pin which can be used as an interrupt.
*/
  RTC.ctrl = 0x00;                      // 0x00=disable SQW pin, 0x10=1Hz,
                                        // 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
  RTC.setCTRL();
  delay(5000);

    pinMode(ledPin1, OUTPUT);
    pinMode(buttonPin1, INPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(buttonPin2, INPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(buttonPin3, INPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(buttonPin4, INPUT);
    pinMode(buttonPin5, INPUT);
    pinMode(buttonPin6, INPUT);


  }

  void loop()
{
    RTC.getTime();

    currentDay = RTC.day;
    currentMonth = RTC.month;
    currentYear = RTC.year;
    currentHour = RTC.hour;
    currentMinute = RTC.minute;
  String rcv="";


  bool newData = false;

  //For one second we parse GPS data and report some key values

  for (unsigned long start = millis(); millis() - start < 1000;)


  while (Serial1.available())
    {
      char c = Serial1.read();
     //Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
      newData = true;
    }



  if (newData)
  {
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);

    if ((flat !=previousFLat)||(flon !=previousFLon))
    {
      previousFLat = flat;
      previousFLon = flon;
      //Serial.print (previousFLat,6)
      //Serial.println(previousFLon,6);
      SendSQL();
      PullSQL();
      startTrip();
    }
  }

}


void waitUntilReponse(int delayMs)
{
// Delay in ms
while (sGPS.available() < 0) { delay(delayMs); } while (sGPS.available() > 0) 
{
Serial.write(sGPS.read());
}
}


  void SendSQL()
  {

    Serial.println("Start Send");
    waitUntilReponse(500);

    sGPS.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=1,1\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=2,1\r\n");
    delay(3000);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPINIT\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPINIT=?\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPPARA=\"CID\",1\r\n");
    delay(500);
   waitUntilReponse(500);

    sGPS.print("AT+HTTPPARA=\"URL\",\"http://quickshawdic.000webhostapp.com/geta.php?");
    sGPS.print("desig=R001");

     sGPS.print("&addDay=");

     sGPS.print(currentYear);
     sGPS.print("-");

     if (currentMonth < 10) { sGPS.print("0"); sGPS.print(currentMonth); }
      else { sGPS.print(currentMonth); } 
      sGPS.print("-");

      if (currentDay <10) { sGPS.print("0"); sGPS.print(currentDay); }
      else { sGPS.print(currentDay); }


      sGPS.print("&addHour="); 
      if (currentHour <10) { sGPS.print("0"); sGPS.print(currentHour); }
      else { sGPS.print(currentHour); }
      sGPS.print(":");

        if (currentMinute <10) { sGPS.print("0"); sGPS.print(currentMinute); }
        else { sGPS.print (currentMinute); }

          sGPS.write("&addLati=");
          sGPS.print(previousFLat,6);
          sGPS.write("&addLongti=");
          sGPS.print(previousFLon,6);
          sGPS.println("\"");
          delay(3000);
          waitUntilReponse(500);
          sGPS.write("AT+HTTPACTION=0\r\n");
          delay(15000);
          sGPS.write("AT+HTTPTERM\r\n");
          delay(500);
          sGPS.write("AT+SAPBR=0,1\r\n");
          delay(500);
          waitUntilReponse(500);
          Serial.println("Finish");
        }

void PullSQL()

{
  lcd.clear();
  lcd.home();
  Serial.println("Start Recieve");
    waitUntilReponse(500);

    sGPS.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=1,1\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+SAPBR=2,1\r\n");
    delay(3000);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPINIT\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPINIT=?\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.println("AT+HTTPPARA=\"CID\",1\r\n");
    delay(500);
    waitUntilReponse(500);
    sGPS.print("AT+HTTPPARA=\"URL\",\"http://quickshawdic.000webhostapp.com/getr.php?");
    sGPS.print("desig=R001");
    waitUntilReponse(5000);


    String line = sGPS.readString(); //Read the server response line by line..
    rcv+=line; //And store it in rcv.
    Serial.println("Received string: ");
    Serial.print(line);
     delay(3000);
  waitUntilReponse(500);


  sGPS.write("AT+HTTPACTION=0\r\n");
          delay(15000);
          sGPS.write("AT+HTTPTERM\r\n");
          delay(500);
          sGPS.write("AT+SAPBR=0,1\r\n");
          delay(500);
          waitUntilReponse(500);
          Serial.println("Pull Finish");
}


    void startTrip()

    {

      button1State = digitalRead(buttonPin1);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (button1State == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
  }

    }

PHP Code

<?php

 $dbusername = "username";  
    $dbpassword = "password"; 
    $server = "localhost"; 

 $dbconnect = mysqli_connect($server, $dbusername, $dbpassword);
    //$dbselect = mysqli_select_db($dbconnect, "request");

 $sql="SELECT user_name FROM id8371368_quickshaw.rides
 WHERE rickshaw_id = '".$_GET["desig"]."'";

 $records=mysqli_query($dbconnect,$sql);
 $string="";

    $row=mysqli_fetch_assoc($records);
 $string=$row["user_name"];
 echo $string;
?>

Arduino Serial Output

  • Try adding newline after sending the URL: sGPS.println("desig=R001"); – Johnny Mopp Feb 12 at 16:46
  • That's what I did at first, same no output. – iHertz Feb 13 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.