Serial Communication between NodeMCU and Arduino with webserver!

#DIAGRAM:--

NOTE: REMOVE RX AND TX PIN WHILE UPLOADING CODE OR U WILL FACE ERROR

#Setting our UNO - IDE

#FOLLOW UP:)

1.OPEN-UNO-IDE: Add this link to the additional board manager URL

link: https://arduino.esp8266.com/stable/package_esp8266com_index.json


2.Install ESP8266 [fallow image]

3.HOW TO UPLOAD CODE!


#CODE FOR ARDUINO

  1. int led = 12;
  2. void setup() {
  3. pinMode(12,OUTPUT);
  4. Serial.begin(9600);

  5. while (!Serial);
  6. Serial.println("Input 1 to Turn LED on and 0 to off");


  7. }

  8. void loop() {
  9. if(Serial.available()){
  10. int state = Serial.parseInt();


  11. if (state == 3){
  12. digitalWrite(12,HIGH);
  13. Serial.println("Command received:1 LED turned ON");


  14. }

  15. if (state == 4){
  16. digitalWrite(12,LOW);
  17. Serial.println("Command received:0 LED turned OFF");
  18. }

  19. }}

#CODE FOR NODEMCU8266

REMEMBER TO PUT UR WIFI&PASSWORD

  1. # include <ESP8266WiFi.h>
  2. # include<SoftwareSerial.h>


  3. WiFiClient
  4. client;
  5. WiFiServer
  6. server(80);
  7. SoftwareSerial
  8. abc(13, 15);
  9. # define led D5
  10. # define led2 D6

  11. void
  12. setup()
  13. {
  14. // put
  15. your
  16. setup
  17. code
  18. here, to
  19. run
  20. once: \
  21. Serial.begin(9600);
  22. WiFi.begin("cool", "gaurav123");
  23. while (WiFi.status() != WL_CONNECTED)
  24. {
  25. delay(200);
  26. Serial.print("..");
  27. }
  28. Serial.println();
  29. Serial.println("NodeMCU is connected!");
  30. Serial.println(WiFi.localIP());
  31. server.begin();
  32. pinMode(led, OUTPUT);
  33. pinMode(led2, OUTPUT);
  34. }

  35. void
  36. loop()
  37. {
  38. // put
  39. your
  40. main
  41. code
  42. here, to
  43. run
  44. repeatedly:
  45. client = server.available(); // Gets
  46. a
  47. client
  48. that is connected
  49. to
  50. the
  51. server and has
  52. data
  53. available
  54. for reading.
  55. if (client == 1)
  56. {
  57. String
  58. request = client.readStringUntil('\n');
  59. Serial.println(request);
  60. request.trim();
  61. if (request == "GET /led1on HTTP/1.1")
  62. {Serial.write("3");

  63. }
  64. if (request == "GET /led1off HTTP/1.1")
  65. {Serial.write("4");

  66. }
  67. if (request == "GET /led2on HTTP/1.1")
  68. {
  69. digitalWrite(led2, HIGH);
  70. }
  71. if (request == "GET /led2off HTTP/1.1")
  72. {
  73. digitalWrite(led2, LOW);
  74. }
  75. }
  76. client.println("HTTP/1.1 200 OK"); //

  77. client.println("Content-Type: text/html");
  78. client.println("");
  79. client.println("<!DOCTYPE HTML>");
  80. client.println("<html>");
  81. client.println("<h1>Welcome to the Webpage!</h1>");
  82. client.println("<h3>LED Controls<h3>");
  83. client.println("<br>");
  84. client.println("<a href=\"/led1on\"\"><button>LED 1 ON</button></a>");
  85. client.println("<a href=\"/led1off\"\"><button>LED 1 OFF</button></a><br/>");
  86. client.println("<a href=\"/led2on\"\"><button>LED 2 ON</button></a>");
  87. client.println("<a href=\"/led2off\"\"><button>LED 2 OFF</button></a><br/>");
  88. client.println("</html>");
  89. }

#OPEN MONITOR AND & PRESS THE RESET BUTTON TO SEE THE IP WHERE U CAN CONTROL THE LED


Post a Comment

If you have any doubts, please let me know

Previous Post Next Post