/**
* BasicHTTPClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <string.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#define USE_SERIAL Serial
WiFiMulti wifiMulti;
#include <CustomJWT.h>
char key[64]; // = "22Tat0n4sTkn";
char header[64];
char payload[1600];
char signature[64];
char out[2048];
char auth_x [512] = "";
char j_data[1024] = "{\"logger\":9,\"device\":4002,\"tlocal\":\"2023-11-09 15:19:42\",\"tzone\":420,\"tsample\":10,\"status\":1,\"latitude\":-7.7077970,\"longitude\":110.377650,\"location\":\"GPA LOGGER TATONAS\",\"uid\":\"20220325\",\"datetime\":1699517982,\"pintu\":0,\"WATER LEVEL\":[5348.000,5348.000,0,5000,6000,7000,8000,2,2],\"DEVICE TEMP.\":[33.758,33.758,0,0,0,0,0,0,0],\"BATTERY\":[12.120,12.120,0,0,0,0,0,0,0],\"RAINFALL\":[0.000,0.000,0,0,0,0,0,0,0]}";
CustomJWT jwt( key, header, sizeof(header), payload, sizeof(payload), signature, sizeof(signature), out, sizeof(out) );
//char key[] = "tester";
//char j_data[] = "{\"temp\":22.5,\"speed\":25.1}";
//char string[ 1800 ] = "{\"logger\":9,\"device\":4002,\"tlocal\":\"2023-11-02 10:30:00\",\"tzone\":420,\"tsample\":10,\"status\":1,\"latitude\":-7.7077970,\"longitude\":110.377650,\"location\":\"GPA LOGGER TATONAS\",\"uid\":\"20220325\",\"datetime\":1698895800,\"pintu\":0,\"WATER LEVEL\":[1000.000,1000.000,0,0,0,0,0,0,0],\"DEVICE TEMP.\":[31.742,31.742,0,0,0,0,0,0,0],\"BATTERY\":[12.161,12.161,0,0,0,0,0,0,0],\"RAINFALL\":[0,0,0,0,0,0,0,0,0]}";
//char httpRequestData [] = "{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dnZXIiOjksImRldmljZSI6NDAwMiwidGxvY2FsIjoiMjAyMy0xMS0wMiAxMDozMDowMCIsInR6b25lIjo0MjAsInRzYW1wbGUiOjEwLCJzdGF0dXMiOjEsImxhdGl0dWRlIjotNy43MDc3OTcwLCJsb25naXR1ZGUiOjExMC4zNzc2NTAsImxvY2F0aW9uIjoiR1BBIExPR0dFUiBUQVRPTkFTIiwidWlkIjoiMjAyMjAzMjUiLCJkYXRldGltZSI6MTY5ODg5NTgwMCwicGludHUiOjAsIldBVEVSIExFVkVMIjpbMTAwMC4wMDAsMTAwMC4wMDAsMCwwLDAsMCwwLDAsMF0sIkRFVklDRSBURU1QLiI6WzMxLjc0MiwzMS43NDIsMCwwLDAsMCwwLDAsMF0sIkJBVFRFUlkiOlsxMi4xNjEsMTIuMTYxLDAsMCwwLDAsMCwwLDBdLCJSQUlORkFMTCI6WzUuMDAwLDEwNC41MDAsMCwwLDAsMCwwLDAsMF19.860w-x8rdF-hmmTHKbPrH2HMU4EoUSjACxpmz4o7LRY\"}";
//char in_string[ 1800 ] = "{\"logger\":9,\"device\":4002,\"tlocal\":\"2023-11-02 10:30:00\",\"tzone\":420,\"tsample\":10,\"status\":1,\"latitude\":-7.7077970,\"longitude\":110.377650,\"location\":\"GPA LOGGER TATONAS\",\"uid\":\"20220325\",\"datetime\":1698895800,\"pintu\":0,\"WATER LEVEL\":[1000.000,1000.000,0,0,0,0,0,0,0],\"DEVICE TEMP.\":[31.742,31.742,0,0,0,0,0,0,0],\"BATTERY\":[12.161,12.161,0,0,0,0,0,0,0],\"RAINFALL\":[0,0,0,0,0,0,0,0,0]}";
char httpRequestData [ 2048 ] ; //= "{\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dnZXIiOjksImRldmljZSI6NDAwMiwidGxvY2FsIjoiMjAyMy0xMS0wOSAxNToxOTo0MiIsInR6b25lIjo0MjAsInRzYW1wbGUiOjEwLCJzdGF0dXMiOjEsImxhdGl0dWRlIjotNy43MDc3OTcwLCJsb25naXR1ZGUiOjExMC4zNzc2NTAsImxvY2F0aW9uIjoiR1BBIExPR0dFUiBUQVRPTkFTIiwidWlkIjoiMjAyMjAzMjUiLCJkYXRldGltZSI6MTY5OTUxNzk4MiwicGludHUiOjAsIldBVEVSIExFVkVMIjpbNTM0OC4wMDAsNTM0OC4wMDAsMCw1MDAwLDYwMDAsNzAwMCw4MDAwLDIsMl0sIkRFVklDRSBURU1QLiI6WzMzLjc1OCwzMy43NTgsMCwwLDAsMCwwLDAsMF0sIkJBVFRFUlkiOlsxMi4xMjAsMTIuMTIwLDAsMCwwLDAsMCwwLDBdLCJSQUlORkFMTCI6WzAuMDAwLDAuMDAwLDAsMCwwLDAsMCwwLDBdfQ.ksgZ1AquH8CxYeGAGxObs0FfUQ5fIytXyC7bKav26OA\"}";
// data : 403 = {"logger":9,"device":4002,"tlocal":"2023-11-09 15:19:42","tzone":420,"tsample":10,"status":1,"latitude":-7.7077970,"longitude":110.377650,"location":"GPA LOGGER TATONAS","uid":"20220325","datetime":1699517982,"pintu":0,"WATER LEVEL":[5348.000,5348.000,0,5000,6000,7000,8000,2,2],"DEVICE TEMP.":[33.758,33.758,0,0,0,0,0,0,0],"BATTERY":[12.120,12.120,0,0,0,0,0,0,0],"RAINFALL":[0.000,0.000,0,0,0,0,0,0,0]}
// jwt : 631 = {"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dnZXIiOjksImRldmljZSI6NDAwMiwidGxvY2FsIjoiMjAyMy0xMS0wOSAxNToxOTo0MiIsInR6b25lIjo0MjAsInRzYW1wbGUiOjEwLCJzdGF0dXMiOjEsImxhdGl0dWRlIjotNy43MDc3OTcwLCJsb25naXR1ZGUiOjExMC4zNzc2NTAsImxvY2F0aW9uIjoiR1BBIExPR0dFUiBUQVRPTkFTIiwidWlkIjoiMjAyMjAzMjUiLCJkYXRldGltZSI6MTY5OTUxNzk4MiwicGludHUiOjAsIldBVEVSIExFVkVMIjpbNTM0OC4wMDAsNTM0OC4wMDAsMCw1MDAwLDYwMDAsNzAwMCw4MDAwLDIsMl0sIkRFVklDRSBURU1QLiI6WzMzLjc1OCwzMy43NTgsMCwwLDAsMCwwLDAsMF0sIkJBVFRFUlkiOlsxMi4xMjAsMTIuMTIwLDAsMCwwLDAsMCwwLDBdLCJSQUlORkFMTCI6WzAuMDAwLDAuMDAwLDAsMCwwLDAsMCwwLDBdfQ.ksgZ1AquH8CxYeGAGxObs0FfUQ5fIytXyC7bKav26OA"}
char data_x [] = { 0 };
char data_y [] = { 0 };
//char header [] = { "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" };
int n_data = 1;
//const char get_server1 [] = "http://tatonas.co.id/rec/key";
//const char pos_server1 [] = "http://tatonas.co.id/rec/send";
//const char get_server2 [] = "http://tatonas.co.id/rec/key";
//const char pos_server2 [] = "https://1000.requestcatcher.com/test";
//const char get_server [] = "http://tatonas.co.id/recluwu/key";
//const char pos_server [] = "http://tatonas.co.id/recluwu/send";
void setup()
{
USE_SERIAL.begin( 115200 );
USE_SERIAL.println();
Serial.print("SYSTEM START..\n");
for ( uint8_t t = 4; t > 0; t-- )
{ USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush(); delay(1000);
}
wifiMulti.addAP("Tatonas MFG","kamunanya"); // SSID, PASSWORD
wifiMulti.addAP("kurnia","12345678");
wifiMulti.addAP("Wokwi-GUEST","");
//jwt.allocateJWTMemory();
}
char to_server[64];
char add_server1 [32] = "tatonas.co.id";
char pos_server1 [32] = "/rec/send";
char get_server1 [32] = "/rec/key";
char img_server1 [32] = "/rec/img64";
char ssl_server1 = 0;
int por_server1 = 80;
char add_server2 [32] = "1000.requestcatcher.com";
char pos_server2 [32] = "/test";
char get_server2 [32] = "/rec/key";
char img_server2 [32] = "/rec/img64"; // https://1000.requestcatcher.com/test";
char ssl_server2 = 1;
int por_server2 = 80;
char add_server3 [32] = "1000.requestcatcher.com"; //"ispu.menlhk.go.id"; //https://ispu.menlhk.go.id/api/v1/auth https://ispu.menlhk.go.id /api/v1/aqmdata
char pos_server3 [32] = "/api/v1/aqmdata";
char get_server3 [32] = "/api/v1/auth";
char img_server3 [32] = "/rec/img64"; // https://1000.requestcatcher.com/test";
char ssl_server3 = 1;
int por_server3 = 80;
// #define HTTP_HEADER_USER_AGENT = "RND_TATONAS";
//#include <ESP32Ping.h>
void sender( char server )
{
int tx_count = 1;
String ht_txt; // = "http";
//String httpRequestData = "{\"token\":\"";
//if ( server == 1 ) httpRequestData = String( httpRequestData + jwt.out + "\"}" );
ulangi:
ht_txt = "http";
//http.addHeader("Content-Type","application/json");
if ( server == 1 )
{ if ( ssl_server1 == 1 ) ht_txt = "https";
sprintf ( to_server,"%s://%s:%d%s", ht_txt,add_server1,por_server1,pos_server1 );
}
else if ( server == 2 )
{ if ( ssl_server2 == 1 ) ht_txt = "https";
//sprintf ( to_server,"%s://%s:%d%s", ht_txt,add_server2,por_server2,pos_server3 );
sprintf ( to_server,"%s://%s%s-%d", ht_txt,add_server2, pos_server2, n_data );
}
else if ( server == 3 )
{ if ( ssl_server3 == 1 ) ht_txt = "https";
sprintf ( to_server,"%s://%s%s", ht_txt,add_server3, get_server3 );
}
else if ( server == 4 )
{ if ( ssl_server3 == 1 ) ht_txt = "https";
sprintf ( to_server,"%s://%s%s", ht_txt,add_server3, pos_server3 );
}
HTTPClient http;
http.begin( to_server );
http.addHeader("User-Agent","RnD_Tat0nas");
if ( server == 1 )
{
//httpRequestData = "{\"token\":\"";
//httpRequestData = String( httpRequestData + jwt.out + "\"}" );
sprintf ( httpRequestData, "{\"token\":\"%s\"}", jwt.out );
}
if ( server > 1 ) http.addHeader("Content-Type","application/json" );
if ( server == 3 )
{
sprintf ( httpRequestData, "%s", data_x );
//httpRequestData = String( data_x );
}
if ( server == 4 )
{
//http.addHeader("Authorization", auth_x );
//http.addHeader("Authorization", auth_x );
sprintf ( httpRequestData, "Oauth %s", auth_x );
http.addHeader("Authorize", httpRequestData );
sprintf ( httpRequestData, "%s", data_y );
//httpRequestData = String( data_y );
}
//http.addHeader("User-Agent","RnD_Tat0nas");
//http.begin( to_server );
Serial.printf("\n[POST] %d - %d : %s\n", server, tx_count++, to_server );
//Serial.printf("[POST] %d = ", httpRequestData.length() ); Serial.println(httpRequestData);
Serial.printf("[POST] %d = %s\n", strlen(httpRequestData),httpRequestData );
if ( server == 4 ) Serial.printf("[AUTH] %d = %s\n", strlen(auth_x),auth_x );
//http.addHeader("User-Agent" ,"Tat0nas");
//http.addHeader("Content-Type","application/json");
int httpResponseCode = http.POST( httpRequestData );
String post_respon = http.getString();
Serial.printf("[POST] Response code : %3d\n", httpResponseCode );
//Serial.printf("[POST] Response text : %3d = %s\n", post_respon.length(), post_respon );
Serial.printf("[POST] Response text : %3d = ", post_respon.length() ); Serial.println(post_respon);
if ( httpResponseCode == 200 ) //&& post_respon == "Data Sent Successfully!" )
{ Serial.print("[POST] SEND = OK" );
if ( server == 1 )
{ if ( post_respon == "Data Sent Successfully!" )
{ Serial.print(" --> YES sukses bro..!\n");
if ( tx_count < 3 ) goto ulangi;
}
}
else if ( server == 3 )
{
int i_x=0,i_y=post_respon.length()+1;
post_respon.toCharArray( auth_x,i_y );
Serial.printf("\n[RESP] %d = %s", strlen(auth_x),auth_x );
if ( strncmp( auth_x,"{\"status\":1,\"message\":\"\",\"token\":\"", 34 ) == 0 )
{ i_x=0; while( i_x<i_y && auth_x[i_x+34]!='"' ) auth_x[i_x]=auth_x[i_x+34],i_x++; auth_x[i_x]=0; }
Serial.printf("\n[AUTH] %d = %s\n", strlen(auth_x),auth_x );
tx_count = 1, server = 4; goto ulangi;
}
// {"status":1,"message":"","token":" --- 2_QGEYN4eevL6PQLQxIJudXewusJvTc17N8Fq69P0HVH85c9uqFaoWMJMDWOlaTS7iuc5BieDEy7vmk3TRsLPP9NmqoUzS_7U8cBOx9Cy0uumi3xX4zmcMHJFxBwmYetOX-LKvmOK6Q7qgVmY4rGgl-_ETZoU2VFkHpNpEVMWrBBkoTpo_YBtRS44xoU__fC_Y2JtAlKhKpeh-Y7BhL6-fL4Kx84y3jVnlgjYvcQ5IU"}
else if ( server == 4 )
{ Serial.println();
if ( tx_count < 5 ) goto ulangi;
}
else Serial.println();
}
else
{
Serial.printf("[POST] SEND = ERROR CODE : %3d\n", httpResponseCode );
}
http.end();
}
void loop()
{
Serial.print("\nConnecting to WiFi..\n");
//WiFi.begin("Wokwi-GUEST", "", 6);
//while ( WiFi.status() != WL_CONNECTED ) delay(1000),Serial.print(".");
//if ( WiFi.status() == WL_CONNECTED )
if ( wifiMulti.run() == WL_CONNECTED ) // wait for WiFi connection
{
Serial.println("\n-------------------------------------------------------------------------");
Serial.printf ("\n[HTTP] Test %d begin...\n", n_data+=1 );
Serial.printf ("Connected SSID : "); Serial.println( WiFi.SSID() );
Serial.printf ("Connected IP : "); Serial.println( WiFi.localIP() );
Serial.printf ("Signal : %3d", WiFi.RSSI() );
Serial.println();
HTTPClient http;
sprintf ( to_server,"http://%s:%d%s", add_server1,por_server1,get_server1 );
Serial.printf("[GET] %s\n", to_server );
http.begin( to_server ); //HTTP
int httpCode = http.GET();
if ( httpCode > 0 )
{
Serial.printf("[GET] Response code : %3d\n", httpCode );
String get_respon = http.getString();
if ( httpCode == HTTP_CODE_OK )
{
Serial.printf("[GET] Response text : %3d = %s\n\n", get_respon.length(), get_respon );
sprintf( key, "%s", get_respon );
Serial.println("[JWT] start..");
jwt.encodeJWT(j_data);
Serial.printf("KEY : %3d = %s\n", strlen(key) , key );
Serial.printf("HEAD : %3d = %s\n", jwt.headerLength , jwt.header );
Serial.printf("DATA : %3d = %s\n", strlen(j_data) , j_data );
Serial.printf("LOAD : %3d = %s\n", jwt.payloadLength , jwt.payload );
Serial.printf("SIGN : %3d = %s\n", jwt.signatureLength, jwt.signature );
Serial.printf("JWT : %3d = %s\n", jwt.outputLength , jwt.out );
//Serial.printf("SEND : %3d = {\"token\":\"%s\"}\n", 12 + jwt.outputLength , jwt.out );
sender( 1 );
//sender( 2 );
//sender( 3 );
}
}
else
{
Serial.printf("[HTTP] Error code : %d = %s\n", httpCode,http.errorToString(httpCode).c_str() );
}
jwt.clear();
http.end();
Serial.println("\nend");
}
int x = 0; while ( ++x < 30 ) USE_SERIAL.printf("."),delay(1000);
}
/*
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > TIME_POST_DATA) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
String recv_token = "eyJ0eXAiOiJK..."; // Complete Bearer token
recv_token = "Bearer " + recv_token; // Adding "Bearer " before token
// Sending POST request for Location Data.
http.begin(client, serverName2);
http.addHeader("Authorization", recv_token); // Adding Bearer token as HTTP header
httpRequestData = "http://domain.com/api/v1/cattle/location?cattle_id=1&location_date=2022-05-10%2015:25:00&latitude=12.1231111&longitude=-151.82723 HTTP/1.1";
// Send HTTP POST request
httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
if (httpResponseCode>0) {
payload = http.getString();
Serial.println(payload);
}
// Free resources
http.end();
//while(1);
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
*/