So I want to make a program using GPS, relay, bluetooth modules and webserver with ESP32. I tried the code one by one and managed to display the data. but when I combine the code. GPS data (long, lat) is not showing in serial monitor.
The GPS code
#include <TinyGPS++.h> // library for GPS module
#include <SoftwareSerial.h>
#include <WiFi.h>
//#include <ESP8266WiFi.h>
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial ss(17, 16); // The serial connection to the GPS device 4tx/5rx
const char* ssid = "RidwanSoleh"; //ssid of your wifi
const char* password = "ridwananaksoleh"; //password of your wifi
float latitude , longitude;
int year , month , date, hour , minute , second;
String date_str , time_str , lat_str , lng_str;
int pm;
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
ss.begin(9600);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //connecting to wifi
while (WiFi.status() != WL_CONNECTED)// while wifi not connected
{
delay(500);
Serial.print("."); //print "...."
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP()); // Print the IP address
}
//95187266
void loop()
{
while (ss.available() > 0) //while data is available
if (gps.encode(ss.read())) //read gps data
{
if (gps.location.isValid()) //check whether gps location is valid
{
latitude = gps.location.lat();
lat_str = String(latitude , 6); // latitude location is stored in a string
longitude = gps.location.lng();
lng_str = String(longitude , 6); //longitude location is stored in a string
}
if (gps.date.isValid()) //check whether gps date is valid
{
date_str = "";
date = gps.date.day();
month = gps.date.month();
year = gps.date.year();
if (date < 10)
date_str = '0';
date_str += String(date);// values of date,month and year are stored in a string
date_str += " / ";
if (month < 10)
date_str += '0';
date_str += String(month); // values of date,month and year are stored in a string
date_str += " / ";
if (year < 10)
date_str += '0';
date_str += String(year); // values of date,month and year are stored in a string
}
if (gps.time.isValid()) //check whether gps time is valid
{
time_str = "";
hour = gps.time.hour();
minute = gps.time.minute();
second = gps.time.second();
minute = (minute + 25); // converting to IST
if (minute > 59)
{
minute = minute - 60;
hour = hour + 1;
}
hour = (hour + 5) ;
if (hour > 23)
hour = hour - 24; // converting to IST
if (hour >= 12) // checking whether AM or PM
pm = 1;
else
pm = 0;
hour = hour % 12;
if (hour < 10)
time_str = '0';
time_str += String(hour); //values of hour,minute and time are stored in a string
time_str += " : ";
if (minute < 10)
time_str += '0';
time_str += String(minute); //values of hour,minute and time are stored in a string
time_str += " : ";
if (second < 10)
time_str += '0';
time_str += String(second); //values of hour,minute and time are stored in a string
if (pm == 1)
time_str += " PM ";
else
time_str += " AM ";
}
}
Serial.print("Longitude2:");
Serial.println(lng_str);
Serial.print("Latitude:");
Serial.println(lat_str);
WiFiClient client = server.available(); // Check if a client has connected
if (!client)
{
return;
}
// Prepare the response
String b = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n <!DOCTYPE html> <html> <head> <title>GPS DATA</title> <style>";
b += "a:link {background-color: YELLOW;text-decoration: none;}";
b += "table, th, td </style> </head> <body> <h1 style=";
b += "font-size:300%;";
b += " ALIGN=CENTER> GPS DATA</h1>";
b += "<p ALIGN=CENTER style=""font-size:150%;""";
b += "> <b>Location Details</b></p> <table ALIGN=CENTER style=";
b += "width:50%";
b += "> <tr> <th>Latitude</th>";
b += "<td ALIGN=CENTER >";
b += lat_str;
b += "</td> </tr> <tr> <th>Longitude</th> <td ALIGN=CENTER >";
b += lng_str;
b += "</td> </tr> <tr> <th>Date</th> <td ALIGN=CENTER >";
b += date_str;
b += "</td></tr> <tr> <th>Time</th> <td ALIGN=CENTER >";
b += time_str;
b += "</td> </tr> </table> ";
b += "</body> </html>";
client.print(b); // all the values are send to the webpage
delay(100);
}
the combined code
#include <TinyGPS++.h> // library for GPS module
#include <SoftwareSerial.h>
#include <WiFi.h>
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial ss(17, 16); // The serial connection to the GPS device 4tx/5rx 17rx 16tx
const char* ssid = "RidwanSoleh"; //ssid of your wifi
const char* password = "ridwananaksoleh"; //password of your wifi
float latitude , longitude;
int year , month , date, hour , minute , second;
String date_str , time_str , lat_str , lng_str;
int pm;
WiFiServer server(80);
// GPS
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#define RELAY_NO true
#define NUM_RELAYS 2
int relayGPIOs[NUM_RELAYS] = {4, 5,};
const char* PARAM_INPUT_1 = "relay";
const char* PARAM_INPUT_2 = "state";
// relay
#include <BLEDevice.h>
#define ADDRESS "fff:ff:22:2a:f2:fa"
#define RELAY_PIN 15
#define SCAN_INTERVAL 100
#define TARGET_RSSI -100
#define MAX_MISSING_TIME 100
int hitung=0;
BLEScan* pBLEScan;
uint32_t lastScanTime = 0;
boolean found = false;
uint32_t lastFoundTime = 0;
int rssi = 0;
//BT
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
h2 {font-size: 3.0rem;}
p {font-size: 3.0rem;}
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
input:checked+.slider {background-color: #2196F3}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style>
</head>
<body>
<h2>ESP Web Server</h2>
%BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
xhr.send();
}</script>
</body>
</html>
)rawliteral";
// Replaces placeholder with button section in your web page
String processor(const String& var){
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons ="";
for(int i=1; i<=NUM_RELAYS; i++){
String relayStateValue = relayState(i);
buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
}
return buttons;
}
return String();
}
String relayState(int numRelay){
if(RELAY_NO){
if(digitalRead(relayGPIOs[numRelay-1])){
return "";
}
else {
return "checked";
}
}
else {
if(digitalRead(relayGPIOs[numRelay-1])){
return "checked";
}
else {
return "";
}
}
return "";
} // web relay
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
Serial.print("Device found: ");
Serial.println(advertisedDevice.toString().c_str());
if(advertisedDevice.getAddress().toString() == ADDRESS)
{
found = true;
advertisedDevice.getScan()->stop();
rssi = advertisedDevice.getRSSI();
Serial.println("RSSI: " + rssi);
}
}
}; //BT
void setup()
{
Serial.begin(115200);
ss.begin(9600);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //connecting to wifi
while (WiFi.status() != WL_CONNECTED)// while wifi not connected
{
delay(500);
Serial.print("."); //print "...."
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP()); // Print the IP address
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
for(int i=1; i<=NUM_RELAYS; i++){
pinMode(relayGPIOs[i-1], OUTPUT);
if(RELAY_NO){
digitalWrite(relayGPIOs[i-1], HIGH);
}
else{
digitalWrite(relayGPIOs[i-1], LOW);
}
}
}
//95187266
void loop()
{
while (ss.available() > 0) //while data is available
if (gps.encode(ss.read())) //read gps data
{
if (gps.location.isValid()) //check whether gps location is valid
{
latitude = gps.location.lat();
lat_str = String(latitude , 6); // latitude location is stored in a string
longitude = gps.location.lng();
lng_str = String(longitude , 6); //longitude location is stored in a string
}
if (gps.date.isValid()) //check whether gps date is valid
{
date_str = "";
date = gps.date.day();
month = gps.date.month();
year = gps.date.year();
if (date < 10)
date_str = '0';
date_str += String(date);// values of date,month and year are stored in a string
date_str += " / ";
if (month < 10)
date_str += '0';
date_str += String(month); // values of date,month and year are stored in a string
date_str += " / ";
if (year < 10)
date_str += '0';
date_str += String(year); // values of date,month and year are stored in a string
}
if (gps.time.isValid()) //check whether gps time is valid
{
time_str = "";
hour = gps.time.hour();
minute = gps.time.minute();
second = gps.time.second();
minute = (minute + 25); // converting to IST
if (minute > 59)
{
minute = minute - 60;
hour = hour + 1;
}
hour = (hour + 5) ;
if (hour > 23)
hour = hour - 24; // converting to IST
if (hour >= 12) // checking whether AM or PM
pm = 1;
else
pm = 0;
hour = hour % 12;
if (hour < 10)
time_str = '0';
time_str += String(hour); //values of hour,minute and time are stored in a string
time_str += " : ";
if (minute < 10)
time_str += '0';
time_str += String(minute); //values of hour,minute and time are stored in a string
time_str += " : ";
if (second < 10)
time_str += '0';
time_str += String(second); //values of hour,minute and time are stored in a string
if (pm == 1)
time_str += " PM ";
else
time_str += " AM ";
}
}
Serial.print("Longitude2:");
Serial.println(lng_str);
Serial.print("Latitude:");
Serial.println(lat_str);
uint32_t now = millis();
if(found){
lastFoundTime = millis();
found = false;
if(rssi > TARGET_RSSI){
hitung=5;
digitalWrite(RELAY_PIN, HIGH);
delay(0);
}
else{
hitung-=1;
if(hitung<0){
hitung=0;
}
if(hitung==0){
digitalWrite(RELAY_PIN, LOW);
}
}
}
else if(now - lastFoundTime > MAX_MISSING_TIME){
digitalWrite(RELAY_PIN, LOW);
}
if(now - lastScanTime > SCAN_INTERVAL){
lastScanTime = now;
pBLEScan->start(1);
}
WiFiClient client = server.available(); // Check if a client has connected
if (!client)
{
return;
}
}