Tuesday, 6 April 2021

Own IOT website and Hardware Using Node MCU (cyber thinks)

NODE MCU code -- website link Cyberthinks

google drive link cyberthinks website files

#include <ESP8266WiFi.h>

#include <ArduinoJson.h>


const char* ssid     = "#Hacker";

const char* password = "M.A.K.E.R.B.O.T.";

const char* host = "cyberthinks.000webhostapp.com"; //replace it with your webhost url

String url;

int count = 0;



void setup() {

  Serial.begin(115200);

  delay(100);

  pinMode(D0, OUTPUT);

  digitalWrite(D1, 0);


  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);


  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }


  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());

  Serial.print("Netmask: ");

  Serial.println(WiFi.subnetMask());

  Serial.print("Gateway: ");

  Serial.println(WiFi.gatewayIP());

  digitalWrite(D1, 1);

  delay(500);

  digitalWrite(D1, 0);

  delay(500);

}


void loop() {


  Serial.print("connecting to ");

  Serial.println(host);


  WiFiClient client;

  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {

    Serial.println("connection failed");

    return;

  }


  Serial.print("Requesting URL: ");

  Serial.println(url);



  url = "/api/readcommand.php";

  count = count + 1;

  Serial.println("Here1");



  client.print(String("GET ") + url + " HTTP/1.1\r\n" +

               "Host: " + host + "\r\n" +

               "Connection: close\r\n\r\n");


  //Serial.println(String("GET ") + url + " HTTP/1.1\r\n" +

               //  "Host: " + host + "\r\n" +

               //  "Connection: close\r\n\r\n");


  delay(500);

  String section = "header";

  while (client.available()) {

    String line = client.readStringUntil('krkg');


    Serial.println("********************");

    Serial.println(line);

    Serial.println("......................");


    if (section == "header") { // headers..


      if (line == "\n") { // skips the empty space at the beginning

        section = "json";

      }

    }

    

    else if (section == "json") { // print the good stuff

      section = "ignore";

      String result = line.substring(1);


      // Parse JSON

      int size = result.length() + 1;

      char json[size];

      result.toCharArray(json, size);

      StaticJsonBuffer<200> jsonBuffer;

      JsonObject& json_parsed = jsonBuffer.parseObject(json);

      

      if (!json_parsed.success())

      {

        Serial.println("parseObject() failed");

        return;

      }

      

      String led = json_parsed["data"][0];


      Serial.print("mydata:");

      Serial.println(led);


      if (led == "500") {

        digitalWrite(D0, 1);

        delay(100);

        Serial.println("D1 is On..!");

      }

      else if (led == "600") {

        digitalWrite(D0, 0);

        delay(100);

        Serial.println("D1 is Off..!");

      }


    }

  }

  Serial.println();

  Serial.println("closing connection");

  delay(3000);

}


No comments:

Post a Comment

Own IOT website and Hardware Using Node MCU (cyber thinks)

NODE MCU code -- website link  Cyberthinks google drive link  cyberthinks website files #include <ESP8266WiFi.h> #include <ArduinoJ...