In this code, I'm just trying to send random data to make sure my code and website work properly. But I'm confused because the output was unexpected.
#include <WiFi.h>
#include <HTTPClient.h> //Board = Version 2.7.4
#include <ArduinoJson.h> //Library = arduinoJson 6.15.1
#include <LiquidCrystal_I2C.h> //Library LCD I2C
#include <OneWire.h>
#include <DallasTemperature.h>
const char* id_tracker= "1";
const char* ssid = "agung"; //masukkan ssid (hotspot/wifi)
const char* password = "agung123"; //masukkan password
byte server[] = {185,27,134,200};
const int oneWireBus = 4; //// GPIO where the DS18B20 is connected to
OneWire oneWire(oneWireBus); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire);
int indikator=LED_BUILTIN;
WiFiClient client;
const int httpPort = 80;
String url;
unsigned long timeout;
void setup () {
Serial.begin(115200);
//Begin serial communication Arduino IDE (Serial Monitor)
WiFi.begin(ssid, password);
pinMode (indikator, OUTPUT);
Serial.begin(115200);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting..");
digitalWrite(indikator,HIGH);
delay(500);
digitalWrite(indikator,LOW);
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connected!!!");
digitalWrite(indikator,HIGH);
delay(3000);
}
else{
Serial.println("Connected Failed!!!");
delay(1000);
}
}
void loop() {
int lat = random(1,100);
int lon = random(1,100);
int speed = random(1,100);
Serial.print("connecting to ");
if (!client.connect(server, httpPort)) {
Serial.println("connection failed");
}
url = "/Tracking/post/";
url += id_tracker;
url += "/";
url += lat;
url += "/";
url += lon;
url += "/";
url += speed;
Serial.print("Requesting URL: ");
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: callista.id\r\n" +
"Connection: close\r\n\r\n");
timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
Serial.println();
}
Unexpected Output =
connecting to Requesting URL: HTTP/1.1 200 OK
Server: nginx
Date: Wed, 16 Nov 2022 12:01:53 GMT
Content-Type: text/html
Content-Length: 220
Connection: close
Vary: Accept-Encoding
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache
<html><body><script>document.cookie="_test=1ad7861f0938cdca04c04995b64a9309 ; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/" ;document.location.href="http://callista.id/Tracking/post/1/79/93/63?i=1";</script></body></html>
closing connection