Advertisement
pleasedontcode

**Web Server** rev_01

Nov 16th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Web Server**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-11-16 14:40:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize the Sim800L module for */
  21.     /* GSM communication, ensuring reliable data */
  22.     /* transmission and reception via SoftwareSerial on */
  23.     /* specified pins. It must handle reset and DTR */
  24.     /* signals effectively for optimal performance. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SoftwareSerial.h> //https://github.com/plerup/espsoftwareserial
  31. #include <Sim800L.h>    //https://github.com/vittorioexp/Sim800L-Arduino-Library-revised
  32. #include <forcedBMX280.h>   //https://github.com/soylentOrange/Forced-BMX280
  33. #include <WiFi.h> // Include WiFi library for ESP32
  34. #include <WebServer.h> // Include WebServer library for handling HTTP requests
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void handleRoot(); // Function to handle root URL
  40. void handleLogin(); // Function to handle login
  41. void handleSignup(); // Function to handle signup
  42. void handleProfile(); // Function to handle profile page
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t Ny_SIM800L_RING_PIN_RX2 = RX2;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t Ny_SIM800L_RST_PIN_D14 = 14;
  49. const uint8_t Ny_SIM800L_DTR_PIN_TX2 = TX2;
  50.  
  51. /***** DEFINITION OF Software Serial *****/
  52. const uint8_t Ny_SIM800L_Serial_PIN_SERIAL_TX_D4 = 4;
  53. const uint8_t Ny_SIM800L_Serial_PIN_SERIAL_RX_D13 = 13;
  54. SoftwareSerial Ny_SIM800L_Serial(Ny_SIM800L_Serial_PIN_SERIAL_RX_D13, Ny_SIM800L_Serial_PIN_SERIAL_TX_D4);
  55.  
  56. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  57. bool Ny_SIM800L_RST_PIN_D14_rawData = 0;
  58. bool Ny_SIM800L_DTR_PIN_TX2_rawData = 0;
  59.  
  60. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  61. float Ny_SIM800L_RST_PIN_D14_phyData = 0.0;
  62. float Ny_SIM800L_DTR_PIN_TX2_phyData = 0.0;
  63.  
  64. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  65. // Initialize the web server on port 80
  66. WebServer server(80);
  67. Sim800L sim800l(Ny_SIM800L_Serial_PIN_SERIAL_RX_D13, Ny_SIM800L_Serial_PIN_SERIAL_TX_D4, Ny_SIM800L_RST_PIN_D14); // Instantiate Sim800L object
  68.  
  69. void setup(void)
  70. {
  71.     // put your setup code here, to run once:
  72.     pinMode(Ny_SIM800L_RING_PIN_RX2, INPUT_PULLUP);
  73.     pinMode(Ny_SIM800L_RST_PIN_D14, OUTPUT);
  74.     pinMode(Ny_SIM800L_DTR_PIN_TX2, OUTPUT);
  75.    
  76.     // Initialize Software Serial for SIM800L
  77.     Ny_SIM800L_Serial.begin(9600);
  78.    
  79.     // Initialize SIM800L
  80.     sim800l.begin(9600); // Set baud rate for SIM800L
  81.  
  82.     // Start WiFi connection
  83.     WiFi.begin("YOUR_SSID", "YOUR_PASSWORD"); // Replace with your WiFi credentials
  84.     while (WiFi.status() != WL_CONNECTED) {
  85.         delay(1000);
  86.     }
  87.  
  88.     // Define routes
  89.     server.on("/", handleRoot);
  90.     server.on("/login", handleLogin);
  91.     server.on("/signup", handleSignup);
  92.     server.on("/profile", handleProfile);
  93.  
  94.     // Start server
  95.     server.begin();
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     // put your main code here, to run repeatedly:
  101.     updateOutputs(); // Refresh output data
  102.     server.handleClient(); // Handle incoming client requests
  103. }
  104.  
  105. void updateOutputs()
  106. {
  107.     digitalWrite(Ny_SIM800L_RST_PIN_D14, Ny_SIM800L_RST_PIN_D14_rawData);
  108.     digitalWrite(Ny_SIM800L_DTR_PIN_TX2, Ny_SIM800L_DTR_PIN_TX2_rawData);
  109. }
  110.  
  111. void handleRoot() {
  112.     // Serve the login page
  113.     server.send(200, "text/html", R"rawliteral(
  114.    <!DOCTYPE html>
  115.    <html lang="en">
  116.    <head>
  117.        <meta charset="UTF-8">
  118.        <meta name="viewport" content="width=device-width, initial-scale=1.0">
  119.        <title>WeEarnZA - Get Started</title>
  120.        <link rel="stylesheet" href="styles.css">
  121.    </head>
  122.    <body>
  123.      <div class="logo"></div>
  124.      <div class="container">
  125.        <div class="partText">Participate and Earn</div>
  126.        <div class="loginText">Login</div>
  127.        <div class="fields">
  128.          <div class="username"><input type="text" class="user-input" placeholder="Username:"></div>
  129.          <div class="password"><input type="password" class="pass-input" placeholder="Password:"></div>
  130.        </div>
  131.        <button class="signin-button" onclick="location.href='/login'">Sign IN</button>
  132.        <div class="link"><a href="/signup">Sign Up</a></div>
  133.      </div>
  134.      <div class="bottom"></div>
  135.    </body>
  136.    </html>
  137.    )rawliteral");
  138. }
  139.  
  140. void handleLogin() {
  141.     // Here you would implement the login logic
  142.     // For now, just redirect to the profile page
  143.     server.sendHeader("Location", "/profile");
  144.     server.send(303); // 303 See Other
  145. }
  146.  
  147. void handleSignup() {
  148.     // Serve the signup page (similar to login)
  149.     server.send(200, "text/html", R"rawliteral(
  150.    <!DOCTYPE html>
  151.    <html lang="en">
  152.    <head>
  153.        <meta charset="UTF-8">
  154.        <meta name="viewport" content="width=device-width, initial-scale=1.0">
  155.        <title>Sign Up</title>
  156.        <link rel="stylesheet" href="styles.css">
  157.    </head>
  158.    <body>
  159.      <div class="container">
  160.        <div class="signupText">Sign Up</div>
  161.        <div class="fields">
  162.          <div class="username"><input type="text" class="user-input" placeholder="Username:"></div>
  163.          <div class="password"><input type="password" class="pass-input" placeholder="Password:"></div>
  164.        </div>
  165.        <button class="signup-button">Sign Up</button>
  166.        <div class="link"><a href="/">Back to Login</a></div>
  167.      </div>
  168.    </body>
  169.    </html>
  170.    )rawliteral");
  171. }
  172.  
  173. void handleProfile() {
  174.     // Serve the profile page
  175.     server.send(200, "text/html", R"rawliteral(
  176.    <!DOCTYPE html>
  177.    <html lang="en">
  178.    <head>
  179.        <meta charset="UTF-8">
  180.        <meta name="viewport" content="width=device-width, initial-scale=1.0">
  181.        <title>Profile</title>
  182.        <link rel="stylesheet" href="styles.css">
  183.    </head>
  184.    <body>
  185.      <div class="container">
  186.        <div class="profileText">User Profile</div>
  187.        <div class="fields">
  188.          <div class="username">Username: [Username]</div>
  189.          <div class="balance">Balance: R88.00</div>
  190.        </div>
  191.        <div class="link"><a href="/">Logout</a></div>
  192.      </div>
  193.    </body>
  194.    </html>
  195.    )rawliteral");
  196. }
  197.  
  198. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement