"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.
#REQUIREMENT
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
#EXPLANATION & CODE
Transmitter :
# CODE :
- #include <ESP8266WiFi.h>
- const int trigPin = 12; //d6
- const int echoPin = 14; //d5
- const int ledPin = 2; //d4
- //define sound velocity in cm/uS
- #define SOUND_VELOCITY 0.034
- #define CM_TO_INCH 0.393701
- const char *ssid = "Sempai";
- const char *password = "watchanime";
- float distanceCm;
- int sensorValue0 = 0;
- int sensorValue1 = 0;
- int sensorValue2 = 0;
- int sensorValue3 = 0;
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(ledPin, OUTPUT);
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- }
- void loop() {
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- long duration = pulseIn(echoPin, HIGH);
- // Calculate the distance
- distanceCm = duration * SOUND_VELOCITY/2;
- // Convert to inches
- float distanceInch = distanceCm * CM_TO_INCH;
- // Check if distance is 10cm
- if(distanceCm <= 10) {
- digitalWrite(ledPin, LOW); // turn off LED
- sensorValue0 = 0;
- } else {
- delay(5000);
- digitalWrite(ledPin, HIGH); // turn on LED
- sensorValue0 = 1;
- }
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- const char * host = "192.168.4.1"; //default IP address
- const int httpPort = 80;
- if (!client.connect(host, httpPort)) {
- Serial.println("connection failed");
- return;
- }
- // We now create a URI for the request. Something like /data/?sensor_reading=123
- String url = "/data/";
- url += "?sensor_reading=";
- url += "{\"sensor0_reading\":\"sensor0_value\",\"sensor1_reading\":\"sensor1_value\",\"sensor2_reading\":\"sensor2_value\",\"sensor3_reading\":\"sensor3_value\"}";
- url.replace("sensor0_value", String(sensorValue0));
- url.replace("sensor1_value", String(sensorValue1));
- url.replace("sensor2_value", String(sensorValue2));
- url.replace("sensor3_value", String(sensorValue3));
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- while (client.available() == 0) {
- if (millis() - timeout > 5000) {
- Serial.println(">>> Client Timeout !");
- client.stop();
- return;
- }
- }
- }
Receiver :
# CODE :
- // Reciver (turns relay ob/off)
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <ArduinoJson.h>
- #define relayPin 16 //D0
- DynamicJsonDocument jsonDoc(2048);
- const char *ssid = "Sempai";
- const char *password = "watchanime";
- int sensorValue0 = 0;
- int sensorValue1 = 0;
- int sensorValue2 = 0;
- int sensorValue3 = 0;
- String sensor_values;
- ESP8266WebServer server(80);
- void handleSentVar() {
- if (server.hasArg("sensor_reading"))
- {
- sensor_values = server.arg("sensor_reading");
- Serial.println(sensor_values);
- }
- DeserializationError error = deserializeJson(jsonDoc, sensor_values);
- if (error) {
- Serial.println("deserializeJson() failed");
- return;
- }
- sensorValue0 = jsonDoc["sensor0_reading"].as<int>();
- sensorValue1 = jsonDoc["sensor1_reading"].as<int>();
- sensorValue2 = jsonDoc["sensor2_reading"].as<int>();
- sensorValue3 = jsonDoc["sensor3_reading"].as<int>();
- Serial.println(sensorValue0);
- Serial.println(sensorValue1);
- Serial.println(sensorValue2);
- Serial.println(sensorValue3);
- toggle_relays();
- server.send(200, "text/html", "Data received");
- }
- void setup() {
- Serial.begin(9600);
- WiFi.softAP(ssid, password);
- IPAddress myIP = WiFi.softAPIP();
- pinMode(relayPin, OUTPUT);
- server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
- server.begin();
- }
- void loop() {
- server.handleClient();
- }
- void toggle_relays()
- {
- if (sensorValue0 == 0) digitalWrite(relayPin, LOW);
- if (sensorValue1 == 0) digitalWrite(relayPin, LOW);
- if (sensorValue2 == 0) digitalWrite(relayPin, LOW);
- if (sensorValue3 == 0) digitalWrite(relayPin, LOW);
- if (sensorValue0 == 1) digitalWrite(relayPin, HIGH);
- if (sensorValue1 == 1) digitalWrite(relayPin, HIGH);
- if (sensorValue2 == 1) digitalWrite(relayPin, HIGH);
- if (sensorValue3 == 1) digitalWrite(relayPin, HIGH);
- }