You are on page 1of 4

DeepAI

AI Chat
Sign Up

AI Chat
AI Chat is an AI chatbot that writes text. You can use it to write stories,
messages, or programming code. You can use the AI chatbot as a virtual tutor in
almost any subject.

Chat History
Edit chat history
Delete
Here's an example code for a WiFi-based home automation system using NodeMCU. This
code allows you to control 4 devices through a relay module:

```
#include <ESP8266WiFi.h>

const char* ssid = "YOUR_SSID_HERE"; // Enter your WiFi name


const char* password = "YOUR_PASSWORD_HERE"; // Enter your WiFi password
const int relay1 = D1; // Define pin D1 as relay 1
const int relay2 = D2; // Define pin D2 as relay 2
const int relay3 = D3; // Define pin D3 as relay
Copy
Delete
Delete
#include <ESP8266WiFi.h>

const char* ssid = "YOUR_SSID_HERE"; // Enter your WiFi name


const char* password = "YOUR_PASSWORD_HERE"; // Enter your WiFi password
const int relay1 = D1; // Define pin D1 as relay 1
const int relay2 = D2; // Define pin D2 as relay 2
const int relay3 = D3; // Define pin D3 as relay 3
const int relay4 = D4; // Define pin D4 as relay 4

WiFiServer server(80); // Set the port for the server (port 80 is default for HTTP)

void setup() {
pinMode(relay1, OUTPUT); // Set the pin mode for relay 1
pinMode(relay2, OUTPUT); // Set the pin mode for relay 2
pinMode(relay3, OUTPUT); // Set the pin mode for relay 3
pinMode(relay4, OUTPUT); // Set the pin mode for relay 4

digitalWrite(relay1, HIGH); // Set relay 1 to off (HIGH voltage level)


digitalWrite(relay2, HIGH); // Set relay 2 to off (HIGH voltage level)
digitalWrite(relay3, HIGH); // Set relay 3 to off (HIGH voltage level)
digitalWrite(relay4, HIGH); // Set relay 4 to off (HIGH voltage level)

Serial.begin(115200); // Start the serial communication for debugging purposes


delay(10);

// Connect to the WiFi network


Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password); // Connect to the WiFi network


while (WiFi.status() != WL_CONNECTED) { // Wait for the WiFi connection to be
established
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // Print the local IP address
server.begin(); // Start the server
}

void loop() {
WiFiClient client = server.available(); // Check for client requests

if (client) { // If there is a client


Serial.println("New client");
String currentLine = ""; // Create a string to store the incoming data
while (client.connected()) { // While the client is connected
if (client.available()) { // If there is data available from the client
char c = client.read(); // Read the incoming data
Serial.write(c); // Print the incoming data to the Serial monitor
if (c == '\n') { // If the incoming data is a new line character
if (currentLine.startsWith("GET /1/on")) { // If the data contains
"GET /1/on"
digitalWrite(relay1, LOW); // Turn on relay 1
}
if (currentLine.startsWith("GET /2/on")) { // If the data contains
"GET /2/on"
digitalWrite(relay2, LOW); // Turn on relay 2
}
if (currentLine.startsWith("GET /3/on")) { // If the data contains
"GET /3/on"
digitalWrite(relay3, LOW); // Turn on relay 3
}
if (currentLine.startsWith("GET /4/on")) { // If the data contains
"GET /4/on"
digitalWrite(relay4, LOW); // Turn on relay 4
}
if (currentLine.startsWith("GET /1/off")) { // If the data contains
"GET /1/off"
digitalWrite(relay1, HIGH); // Turn off relay 1
}
if (currentLine.startsWith("GET /2/off")) { // If the data contains
"GET /2/off"
digitalWrite(relay2, HIGH); // Turn off relay 2
}
if (currentLine.startsWith("GET /3/off")) { // If the data contains
"GET /3/off"
digitalWrite(relay3, HIGH); // Turn off relay 3
}
if (currentLine.startsWith("GET /4/off")) { // If the data contains
"GET /4/off"
digitalWrite(relay4, HIGH); // Turn off relay 4
}
currentLine = ""; // Clear the string for new data
}
else if (c != '\r') { // If the incoming data is not a carriage return
character
currentLine += c; // Add the incoming data to the string
}

if (currentLine.length() > 100) { // If the incoming data is too long


client.println("HTTP/1.1 413 Payload Too Large"); // Print an error to
the client
client.stop(); // Stop the connection
Serial.println("Client disconnected because of payload too large");
break;
}
}
}
client.stop(); // Stop the connection to the client
Serial.println("Client disconnected");
}
}
```

Note: Make sure to replace "YOUR_SSID_HERE" and "YOUR_PASSWORD_HERE" with your own
WiFi name and password. Also, make sure to connect the relay module to the
specified pins (relay1=D1, relay2=D2, relay3=D3, relay4=D4) and connect the
necessary devices to the relay module.
Copy
Delete
Delete
This code uses an ESP8266-based NodeMCU board to create a WiFi-based home
automation system. It allows you to control 4 devices through a relay module by
sending HTTP requests to the NodeMCU board.

When you upload and run the code on the NodeMCU board, it connects to your WiFi
network and starts a web server on port 80. The server listens for incoming client
requests and processes them using the loop() function.

When a client sends a GET request to the server, the loop() function processes it
and checks whether it contains the strings "GET /1/on", "GET /2/on", "GET /3/on" or
"GET /4/on". If any of these strings are present, the function turns on the
corresponding relay (by setting it to a LOW voltage level).

If the client sends a GET request that contains the strings "GET /1/off", "GET
/2/off", "GET /3/off" or "GET /4/off", the loop() function turns off the
corresponding relay (by setting it to a HIGH voltage level).

The function also checks the length of the incoming data and prints an "HTTP error"
if the payload is too large.

In summary, the NodeMCU board creates a web server that listens for incoming client
requests. The client can send HTTP GET requests with certain string values (such as
"/1/on" or "/3/off") to control the relay states and turn on/off the devices
connected to the relay module.
Copy
Summarize
Delete

Go


Regenerate response
Chat History Editing Options:
Rename this chat history
Delete this chat history
Generate code with AI:
Code with AI Code Assistant!
Explore different AI Chat Modes:
Chat with Historical Figures!
AI Debate
AI Translate
AI Comedian
AI Poet
AI Drunk Friend
AI Disagreement Decider
AI Travel Guide
AI Terminal
AI Plagiarism Checker
AI Motivational Coach
AI Movie Critic
AI Relationship Coach
AI Philosophy Teacher
AI Blackbeard
AI Job Interviewer
AI Fortune Teller
AI Note-Taking Assistant
AI Proofreader
AI Song Recommender
AI Advertiser
AI Storyteller
AI Screenwriter
AI Novelist
AI Math Teacher
AI Magician
What is Genius Mode?
It is an enhanced version of AI Chat that provides more knowledge, fewer errors,
improved reasoning skills, better verbal fluidity, and an overall superior
performance. Due to the larger AI model, Genius Mode is only available via
subscription to DeepAI Pro or via Pay-as-you-go. However, the added benefits often
make it a worthwhile investment.

Learn more about AI Chat:


Ideas for chatting with the AI 💭
Strengths 🦾
Weaknesses 📌
DeepAI
contact
press
legal

You might also like