Merhabalar Gps kodu send_database() functionını kullanmadan çalışıyor fakat onu kullanınca eklediğim resimdeki gibi 0000 olarak geliyor.
Lütfen Yardımcı olursanız sevinirim yarına yetiştirmem gerekiyor bu projeyi.
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#endif
#include <Wire.h>
#include <HCSR04.h>
#include <LiquidCrystal_I2C.h>
#include <CayenneMQTTESP8266.h>
static const int RXPin = 5, TXPin = 16; // GPIO 4=D2(conneect Tx of GPS) and GPIO 5=D1(Connect Rx of GPS
static const uint32_t GPSBaud = 9600; //if Baud rate 9600 didn't work in your case then use 4800
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial mygps(RXPin, TXPin); // The serial connection to the GPS device
float latitude; //Storing the Latitude
float longitude; //Storing the Longitude
float velocity; //Variable to store the velocity
float sats; //Variable to store no. of satellites response
String bearing; //Variable to store orientation or direction of GPS
String NMS;
//unsigned int move_index; // moving index, to be used later
unsigned int move_index = 1; // fixed location for now
// Update HOST URL here
#define HOST "**********" // Enter HOST URL without "http:// " and "/" at the end of URL
#define WIFI_SSID "*****" // WIFI SSID here
#define WIFI_PASSWORD "*********" // WIFI password here
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "*********";
char mqtt_password[] = "*************************";
char client_id[] = "*****";
#define MAX_HEIGHT 24.8 // your bin height
int Percentage_1;
int Percentage_2;
UltraSonicDistanceSensor distanceSensor1(D6, D5); //D1 trig, D2=ech
UltraSonicDistanceSensor distanceSensor2(D2, D3);
int pm;
int p = 0; //looop
WiFiServer server(80);
String sendval, sendval2, sendval3, sendval4, postData;
void setup()
{
Serial.begin(115200);
Serial.println();
mygps.begin(GPSBaud);
Cayenne.begin(username, mqtt_password, client_id, WIFI_SSID, WIFI_PASSWORD); // starting cayenne
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{ Serial.print(".");
delay(500); }
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
delay(30);
}
void loop()
{
if(WiFi.status()== WL_CONNECTED){
while (mygps.available() > 0){
{
// sketch displays information every time a new sentence is correctly encoded.
if (gps.encode(mygps.read()))
displayInfo();
Serial.print("SATS: ");
Serial.println(sats); // float to x decimal places
Serial.print("LATITUDE: ");
Serial.println(latitude, 6); // float to x decimal places
Serial.print("LONGITUDE: ");
Serial.println(longitude, 6);
Serial.print("SPEED: ");
Serial.print(velocity);
Serial.println("kmph");
Serial.print("DIRECTION: ");
Serial.println(bearing);
}
}
float actualCM1 = 1 * distanceSensor1.measureDistanceCm();
float actualCM2 = 1 * distanceSensor2.measureDistanceCm();
Percentage_1 = (MAX_HEIGHT - actualCM1) / (MAX_HEIGHT - 5) * 100 ;
Percentage_2 = (MAX_HEIGHT - actualCM2) / (MAX_HEIGHT - 5) * 100 ;
Serial.println(Percentage_1);
Serial.println(Percentage_2);
send_database();
}
}
void displayInfo()
{
if (gps.location.isValid() )
{
sats = gps.satellites.value(); //get number of satellites
latitude = (gps.location.lat()); //Storing the Lat. and Lon.
longitude = (gps.location.lng());
velocity = gps.speed.kmph(); //get velocity
bearing = TinyGPSPlus::cardinal(gps.course.value()); // get the direction
}
Serial.println();
}
void send_database(){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
// Convert integer variables to string
sendval = String(Percentage_1);
sendval2 = String(Percentage_2);
sendval3 = String(latitude, 6);
sendval4 = String(longitude, 6);
postData = "metal=" + sendval + "&plastic=" + sendval2 + "&lat=" + sendval3 + "&lng=" + sendval4;
http.begin(client,"http://******.***/dbwrite.php"); // Connect to host where MySQL databse is hosted
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); // Send POST request to php file and store server response code in variable named httpCode
Serial.println("Values are, sendval = " + sendval + " and sendval2 = "+sendval2 );
// if connection eatablished then do this
if (httpCode == 200) { Serial.println("Values uploaded successfully."); Serial.println(httpCode);
String webpage = http.getString(); // Get html webpage output and store it in a string
Serial.println(webpage + "\n");
}
// if failed to connect then return and restart
else {
Serial.println(httpCode);
Serial.println("Failed to upload values. \n");
http.end();
return; }
delay(3000);
digitalWrite(LED_BUILTIN, LOW);
delay(3000);
digitalWrite(LED_BUILTIN, HIGH);
}
void send_cayenne()
{
Cayenne.loop();
Cayenne.virtualWrite(1, Percentage_1);
Cayenne.virtualWrite(2, Percentage_2);
}