Hello hi everyone, sorry i need your helps to solve this problem, i need to send data from the system to the server by using gprs connection but data can't uploaded to the server i don't know why, here below is the code please help me to solve the issue.
#include <EEPROM.h>
#include <SPI.h>
//#include <SD.h>
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <NMEAGPS.h>
#include <NeoSWSerial.h>
#include <ArduinoJson.h>
StaticJsonBuffer<100> jsonBuffer;
#define ECHOPIN 3 // Pin to receive echo pulse
#define TRIGPIN 2 // Pin to send trigger pulse
char carID[12] = "T478STK";
/* Two "independant" timed events */
const long eventTime_1_LITERS = 1000; //in ms
const long eventTime_2_GPRS = 60000; //in ms
/* When did they start the race? */
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
float distance;
float fuelHeight;
float liters;
float initialDistance = 25.66;
float surfaceArea = 490.87;
//const int cs_sd=2;
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiWire oled;
NMEAGPS gps;
gps_fix fix;
//File myFile;
float odo;
float Speed;
float alt;
NeoGPS::Location_t lastLoc;
bool lastLocOK = false;
static const int RXPin = 5, TXPin = 6; //-- gps pin
static const uint32_t GPSBaud = 9600;
NeoSWSerial gpsPort(RXPin, TXPin);
const int SHOW_INTERVAL = 1;
const int INITIAL_SHOW = (2 * SHOW_INTERVAL) - 1;
int show = INITIAL_SHOW;
const int LED_PIN = 11; //-- led on at speed limit
const float SPEED_LIMIT = 0.0; // --setup speed limit value es: 55.0; kmh default 0.0
int maxs = 0;
void setup() {
pinMode (LED_PIN, OUTPUT);
Serial.begin(9600);
gpsPort.begin(GPSBaud);
Wire.begin();
DynamicJsonBuffer jsonBuffer;
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
oled.begin(& Adafruit128x64, I2C_ADDRESS);
oled.setFont(TimesNewRoman16);
oled.clear();
oled.println(" ");
oled.println(" FMS LOGGER"); //you can custom this text
oled.println(" ");
oled.println(" WELCOME"); //you can custom this text
delay(3000);
oled.clear();
/*
if(!SD.begin(cs_sd)){
oled.clear();
oled.println(" ");
oled.print("NO SD");
delay(3000);
return;}
*/
oled.println(" ");
oled.print(" EEPROM OK");
delay(2000);
oled.clear();
/*
File data = SD.open("log.csv",FILE_WRITE);
data.println("");
data.println("Dat Hr+ Lat Lon Alt Vel Dis" );
data.close();
*/
EEPROM.get(0, odo);
}
void loop() {
/* Updates frequently */
unsigned long currentTime = millis();
if ( currentTime - previousTime_1 >= eventTime_1_LITERS) {
fuelSensor();
previousTime_1 = currentTime;
}
if (gps.available( gpsPort )) {
gps_fix fix = gps.read();
show = (show + 1) % SHOW_INTERVAL;
if (fix.valid.speed && (fix.speed_kph() > SPEED_LIMIT)) {
digitalWrite( LED_PIN, HIGH );
} else {
digitalWrite( LED_PIN, LOW );
}
if (fix.valid.location) {
if (lastLocOK) {
odo += fix.location.DistanceKm( lastLoc );
Speed = (fix.speed_kph());
}
lastLoc = fix.location;
lastLocOK = true;
}
if ( Speed > maxs)
maxs = Speed;
if (fix.valid.altitude)
alt = ( fix.altitude () );
if (show == 0) {
#define MAX_CHARS 24
char displayBufffer[MAX_CHARS];
oled.setCursor(0, 0);
snprintf(displayBufffer, MAX_CHARS, " DIST: % 3d.%02d", (int)odo, (int)(odo * 100) % 100); //distance
oled.println(displayBufffer);
snprintf(displayBufffer, MAX_CHARS, " KMH: % 3d.%02d", (int)Speed, (int)(Speed * 100) % 100); //speed
oled.println(displayBufffer);
oled.setCursor(2, 60);
//oled.println(" LAT:");//LATITUDE
oled.print(fix.latitude(), 6);
oled.setCursor(74, 60);
//oled.println(" LON:");//LONGITUDE
oled.print(fix.longitude(), 6); //speed
//if (fix.dateTime);
//String Temps=String(fix.dateTime.hours )+(":")+(fix.dateTime.minutes)+(":")+(fix.dateTime.seconds);
//String Date=String(fix.dateTime.date )+("/")+(fix.dateTime.month)+("/")+(fix.dateTime.year);
//File data=SD.open("log.csv",FILE_WRITE);
//data.println(Date +(" ")+ Temps +(" ")+ String(fix.latitude(), 6)+(" ")+String(fix.longitude(), 6)+(" ")+(alt)+(" ") +(Speed)+(" ")+(odo));
//data.close();
EEPROM.put(0, odo);
}
}
/* This is my event_2 */
if (currentTime - previousTime_2 >= eventTime_2_GPRS) {
GPRS_DATA();
previousTime_2 = currentTime;
}
}
void GPRS_DATA() {
if (Serial.available())
Serial.write(Serial.read());
Serial.println("AT");
delay(500);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(500);
//ShowSerialData();
Serial.println("AT+SAPBR=3,1,\"APN\",\"internet\"");//APN
delay(500);
//ShowSerialData();
Serial.println("AT+SAPBR=1,1");
delay(500);
//ShowSerialData();
Serial.println("AT+SAPBR=2,1");
delay(500);
//ShowSerialData();
Serial.println("AT+HTTPINIT");
delay(500);
//ShowSerialData();
Serial.println("AT+HTTPPARA=\"CID\",1");
delay(500);
char carNumber[12] = "T478STK";
//ShowSerialData();
StaticJsonBuffer<100> jsonBuffer;
JsonObject& object = jsonBuffer.createObject();
object.set("truckNumber", carNumber);
object.set("truckFuel", liters, 2);
if (fix.valid.speed)
object.set("truckSpeed", Speed);
object.set("truckOdometer", odo, 2);
object.set("Latitude", fix.latitude(), 6);
object.set("Longitude", fix.longitude(), 6);
object.printTo(Serial);
Serial.println(" ");
String sendtoserver;
object.prettyPrintTo(sendtoserver);
delay(400);
Serial.println("AT+HTTPPARA=\"URL\",\"http://mis.dawasa.go.tz/fm_system/data.php\""); //Server address
delay(400);
//ShowSerialData();
Serial.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
delay(400);
//ShowSerialData();
Serial.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",100000");
Serial.println(sendtoserver);
delay(700);
//ShowSerialData();
Serial.println(sendtoserver);
delay(700);
//ShowSerialData;
Serial.println("AT+HTTPACTION=1");
delay(500);
//ShowSerialData();
Serial.println("AT+HTTPREAD");
delay(500);
//ShowSerialData();
Serial.println("AT+HTTPTERM");
delay(300);
Serial.println("AT+SAPBR=0,1"); /* Close GPRS context */
delay(300);
//ShowSerialData;
//delay(300);
}
void fuelSensor() {
// Start Ranging -Generating a trigger of 10us burst
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Distance Calculation
distance = pulseIn(ECHOPIN, HIGH);
distance = distance / 58;
fuelHeight = initialDistance - distance;
liters = (fuelHeight * surfaceArea) / 1000;
}