Wireless Communication using NodeMCUs as Transmitter and Receiver: A DIY Project

"NodeMCUs as a Transmitter and Receiver" is a project that utilizes NodeMCU boards to establish a wireless communication link between two devices. One NodeMCU board acts as a transmitter, sending data wirelessly to another NodeMCU board that acts as a receiver. This project is useful for a variety of applications, such as remote control of electronic devices, wireless data acquisition, and more. By implementing this project, you can take advantage of the power and flexibility of NodeMCU boards to build your own wireless communication system. This project is not only easy to implement but also cost-effective, making it an ideal solution for hobbyists and enthusiasts who want to experiment with wireless communication.


To run this sketch, you will need the following:

  • An ESP8266 board (such as the NodeMCU or Wemos D1 Mini)
  • An ultrasonic sensor (such as the HC-SR04)
  • A USB cable to connect the ESP8266 board to your computer
  • You will also need the Arduino IDE to upload the sketch to the ESP8266 board.


Diagram Make NodeMCUs as a Transmitter and Receiver


Transmitter : 

This is a sketch for an ESP8266 board that measures distance using an ultrasonic sensor and sends the reading to a server via Wi-Fi. The code defines the trigPin, echoPin, and ledPin, sets up the Wi-Fi connection, and then enters the main loop.

Inside the loop, the code first sends a pulse to the trigPin to start the ultrasonic sensor, and then measures the duration of the echo using the pulseIn() function. Using the duration and the speed of sound, the code calculates the distance to an object in front of the sensor.

The code then checks if the distance is less than or equal to 10 cm. If it is, it turns off the LED connected to the ledPin and sets sensorValue0 to 0. Otherwise, it turns on the LED and sets sensorValue0 to 1 after a delay of 5 seconds.

The code then creates a TCP connection to a server specified by the IP address and port number. It constructs a URL with the sensor readings and sends a GET request to the server with the URL. Finally, it waits for a response from the server, times out if it takes too long, and stops the connection.

It's worth noting that this code has some limitations and could be improved. For example, it only measures the distance to a single object in front of the sensor and sends only one reading to the server per loop iteration. It could also benefit from error handling and more robust Wi-Fi connectivity.

# CODE : 

  1. #include <ESP8266WiFi.h>

  2. const int trigPin = 12; //d6
  3. const int echoPin = 14; //d5
  4. const int ledPin = 2; //d4

  5. //define sound velocity in cm/uS
  6. #define SOUND_VELOCITY 0.034
  7. #define CM_TO_INCH 0.393701

  8. const char *ssid = "Sempai";
  9. const char *password = "watchanime";

  10. float distanceCm;
  11. int sensorValue0 = 0;
  12. int sensorValue1 = 0;
  13. int sensorValue2 = 0;
  14. int sensorValue3 = 0;

  15. void setup() {
  16. Serial.begin(115200);
  17. delay(10);

  18. pinMode(ledPin, OUTPUT);
  19. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  20. pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  21. WiFi.mode(WIFI_STA);
  22. WiFi.begin(ssid, password);

  23. while (WiFi.status() != WL_CONNECTED) {
  24. delay(500);
  25. }

  26. }

  27. void loop() {
  28. // Clears the trigPin
  29. digitalWrite(trigPin, LOW);
  30. delayMicroseconds(2);
  31. // Sets the trigPin on HIGH state for 10 micro seconds
  32. digitalWrite(trigPin, HIGH);
  33. delayMicroseconds(10);
  34. digitalWrite(trigPin, LOW);
  35. // Reads the echoPin, returns the sound wave travel time in microseconds
  36. long duration = pulseIn(echoPin, HIGH);
  37. // Calculate the distance
  38. distanceCm = duration * SOUND_VELOCITY/2;
  39. // Convert to inches
  40. float distanceInch = distanceCm * CM_TO_INCH;
  41. // Check if distance is 10cm
  42. if(distanceCm <= 10) {
  43. digitalWrite(ledPin, LOW); // turn off LED
  44. sensorValue0 = 0;
  45. } else {
  46. delay(5000);
  47. digitalWrite(ledPin, HIGH); // turn on LED
  48. sensorValue0 = 1;

  49. }

  50. // Use WiFiClient class to create TCP connections
  51. WiFiClient client;
  52. const char * host = ""; //default IP address
  53. const int httpPort = 80;

  54. if (!client.connect(host, httpPort)) {
  55. Serial.println("connection failed");
  56. return;
  57. }

  58. // We now create a URI for the request. Something like /data/?sensor_reading=123
  59. String url = "/data/";
  60. url += "?sensor_reading=";
  61. url += "{\"sensor0_reading\":\"sensor0_value\",\"sensor1_reading\":\"sensor1_value\",\"sensor2_reading\":\"sensor2_value\",\"sensor3_reading\":\"sensor3_value\"}";

  62. url.replace("sensor0_value", String(sensorValue0));
  63. url.replace("sensor1_value", String(sensorValue1));
  64. url.replace("sensor2_value", String(sensorValue2));
  65. url.replace("sensor3_value", String(sensorValue3));

  66. // This will send the request to the server
  67. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  68. "Host: " + host + "\r\n" +
  69. "Connection: close\r\n\r\n");
  70. unsigned long timeout = millis();
  71. while (client.available() == 0) {
  72. if (millis() - timeout > 5000) {
  73. Serial.println(">>> Client Timeout !");
  74. client.stop();
  75. return;
  76. }
  77. }
  78. }

Receiver : 

This is a sketch of an ESP8266 microcontroller that is connected to a relay module. The code receives sensor readings via an HTTP GET request to the server's "/data/" endpoint. It then deserializes the received JSON data to extract four sensor readings and stores them in the sensorValue0, sensorValue1, sensorValue2, and sensorValue3 variables.

The toggle_relays() function checks the values of the four sensor readings and turns the relay on or off accordingly. If a sensor reading is 0, the relay is turned off, and if it's 1, the relay is turned on. The relay is connected to the relayPin pin, which is set to output mode in the setup() function.

The code also sets up an access point with the SSID "Sempai" and password "watchanime" using the WiFi.softAP() function. It then listens for HTTP requests on port 80 using the server.begin() function and responds to requests to the "/data/" endpoint by calling the handleSentVar() function.

Overall, the code seems to be designed to receive sensor readings and control a relay based on those readings. However, there is no information provided on the sensor types, how they are connected to the ESP8266, or how the readings are being transmitted to the server.

# CODE : 

  1. // Reciver (turns relay ob/off)

  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ArduinoJson.h>

  5. #define relayPin 16 //D0

  6. DynamicJsonDocument jsonDoc(2048);

  7. const char *ssid = "Sempai";
  8. const char *password = "watchanime";

  9. int sensorValue0 = 0;
  10. int sensorValue1 = 0;
  11. int sensorValue2 = 0;
  12. int sensorValue3 = 0;
  13. String sensor_values;

  14. ESP8266WebServer server(80);

  15. void handleSentVar() {

  16. if (server.hasArg("sensor_reading"))
  17. {
  18. sensor_values = server.arg("sensor_reading");
  19. Serial.println(sensor_values);
  20. }

  21. DeserializationError error = deserializeJson(jsonDoc, sensor_values);
  22. if (error) {
  23. Serial.println("deserializeJson() failed");
  24. return;
  25. }

  26. sensorValue0 = jsonDoc["sensor0_reading"].as<int>();
  27. sensorValue1 = jsonDoc["sensor1_reading"].as<int>();
  28. sensorValue2 = jsonDoc["sensor2_reading"].as<int>();
  29. sensorValue3 = jsonDoc["sensor3_reading"].as<int>();

  30. Serial.println(sensorValue0);
  31. Serial.println(sensorValue1);
  32. Serial.println(sensorValue2);
  33. Serial.println(sensorValue3);

  34. toggle_relays();

  35. server.send(200, "text/html", "Data received");
  36. }

  37. void setup() {
  38. Serial.begin(9600);
  39. WiFi.softAP(ssid, password);
  40. IPAddress myIP = WiFi.softAPIP();

  41. pinMode(relayPin, OUTPUT);
  42. server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  43. server.begin();
  44. }

  45. void loop() {
  46. server.handleClient();
  47. }

  48. void toggle_relays()
  49. {
  50. if (sensorValue0 == 0) digitalWrite(relayPin, LOW);
  51. if (sensorValue1 == 0) digitalWrite(relayPin, LOW);
  52. if (sensorValue2 == 0) digitalWrite(relayPin, LOW);
  53. if (sensorValue3 == 0) digitalWrite(relayPin, LOW);

  54. if (sensorValue0 == 1) digitalWrite(relayPin, HIGH);
  55. if (sensorValue1 == 1) digitalWrite(relayPin, HIGH);
  56. if (sensorValue2 == 1) digitalWrite(relayPin, HIGH);
  57. if (sensorValue3 == 1) digitalWrite(relayPin, HIGH);
  58. }

# Conclusion

In conclusion, using NodeMCUs as a transmitter and receivers can be a useful and cost-effective way to communicate between two devices wirelessly. With the help of the ESP8266WiFi library and ArduinoJson library, it is easy to set up a web server on the transmitter NodeMCU that can send data to the receiver NodeMCU through an HTTP GET request. The receiver NodeMCU can then process the data and take appropriate actions based on the received information.

There are many potential applications for this project, such as home automation, remote control of devices, and monitoring systems. With some modifications and additions, this project can be expanded to include more sensors, actuators, and features, making it even more versatile and useful. Overall, using NodeMCUs as a transmitter and receivers provides an accessible and flexible way to create wireless communication systems.


If you have any doubts or encounter errors while working on this project, you can join our Telegram channel for assistance. We are always happy to help and guide you through any issues you may face. Thank you for considering this project, and we hope it provides you with a valuable learning experience! 

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post