8266 post wifi

#include <ESP8266WiFi.h>
#include <time.h>

const char *ssid = "Airtel";
const char *pass = "Airtel";

const char *   host = "uo0cpjwmc7m9wro-subratdw.adb.ap-mumbai-1.oraclecloudapps.com";
const uint16_t  port = 443;
const char *   path = "/ords/rest/IOT/PLAIN";
//const char *   data = "{id=2345}";

// Set time via NTP, as required for x.509 validation
time_t setClock() {
  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  Serial.print("Waiting for NTP time sync: ");
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    delay(500);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println("");
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print("Current time: ");
  Serial.print(asctime(&timeinfo));
  return now;
}

void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) {
  if (!path) {
    path = "/";
  }

  Serial.printf("Trying: %s:443...", host);
  client->connect(host, port);

  if (!client->connected()) {
 
    Serial.printf("*** Can't connect. ***\n-------\n");
    Serial.printf("---------- \n");
    Serial.printf("client->getLastSSLError() output is : ");
    Serial.println (client->getLastSSLError());
    Serial.printf("\n ----------");
 
    return;
  }

  Serial.printf("Connected!\n-------\n");
  client->write("POST ");
  client->write(path);
  client->write(" HTTP/1.1\r\nHost: ");
  client->write(host);
  client->write("\r\nContent-Type: application/x-www-form-urlencoded\r\n");
  client->write("Content-Type: application/x-www-form-urlencoded\r\n");
  client->write("Content-Length: 7\r\n");
  client->write("\r\n") ;
  client->write("id=1001\r\n") ;
 // client->write("\r\nUser-Agent: ESP8266\r\n");
 // client->write("\r\n");
 // client->write("POST /ords/rest/IOT/PLAIN HTTP/1.1\r\n") ;
 // client->write("Host: uo0cpjwmc7m9wro-subratdw.adb.ap-mumbai-1.oraclecloudapps.com\r\n");
 // client->write("Content-Type: application/x-www-form-urlencoded\r\n");
 // client->write("Content-Length: 6\r\n");
 // client->write("\r\n") ;
 // client->write("id=1001\r\n") ;

  uint32_t to = millis() + 5000;
  if (client->connected()) {
    do {
      char tmp[32];
      memset(tmp, 0, 32);
      int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1);
      yield();
      if (rlen < 0) {
        break;
      }
      // Only print out first line up to \r, then abort connection
      char *nl = strchr(tmp, '\r');
      if (nl) {
        *nl = 0;
        Serial.print(tmp);
        break;
      }
      Serial.print(tmp);
    } while (millis() < to);
  }
  client->stop();
  Serial.printf("\n-------\n\n");
}

void fetchInsecure() {
  Serial.printf(R"EOF(
This is absolutely *insecure*, but you can tell BearSSL not to check the
certificate of the server.  In this mode it will accept ANY certificate,
which is subject to man-in-the-middle (MITM) attacks.
)EOF");
  BearSSL::WiFiClientSecure client;
  client.setInsecure();
  fetchURL(&client, host, port, path);
}

void setup(){
  Serial.begin(115200);
  //Serial.setDebugOutput(true);
  Serial.println();
  Serial.println();

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  fetchInsecure();


}

void loop() {
  // put your main code here, to run repeatedly:

}

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud