pleasedontcode

Ads Manager rev_01

Sep 9th, 2025
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.85 KB | None | 0 0
  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: Ads Manager
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-09-09 13:57:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* generate a website for advertisement managerment */
  21.     /* where users can view and vendors can add edit and */
  22.     /* delete. where more details of the items are placed */
  23.     /* in the view page */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. #include <SPI.h>
  30. #include <Ethernet.h>
  31.  
  32. /****** Advertisement Management Web Server for Arduino Mega with Ethernet Shield ******/
  33. /****** NOTE: This sketch serves a simple web interface to view ads and lets vendors add, edit, and delete ads.******/
  34.  
  35. // Hardware: Arduino Mega with W5100 Ethernet Shield or compatible
  36. // MAC address for Ethernet
  37. const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  38.  
  39. // Public IP configuration (used if DHCP fails or you want a static IP)
  40. IPAddress staticIP(192, 168, 1, 177);
  41. IPAddress gateway(192, 168, 1, 1);
  42. IPAddress subnet(255, 255, 255, 0);
  43.  
  44. EthernetServer server(80);
  45.  
  46. /****** Data Model ******/
  47. #define MAX_ADS 8
  48.  
  49. struct Ad {
  50.   uint32_t id;
  51.   char title[64];
  52.   char description[128];
  53.   float price;
  54.   char details[256];
  55.   char vendor[32];
  56.   bool active;
  57. };
  58.  
  59. Ad ads[MAX_ADS];
  60. int ad_count = 0;
  61. uint32_t next_id = 1;
  62.  
  63. // --------- Utility functions for ads ---------
  64. Ad* findAdById(uint32_t id){
  65.   for(int i=0;i<ad_count;i++){
  66.     if(ads[i].id==id) return &ads[i];
  67.   }
  68.   return NULL;
  69. }
  70.  
  71. void addAd(const char* title, const char* description, float price, const char* details, const char* vendor){
  72.   if(ad_count>=MAX_ADS) return;
  73.   Ad &a = ads[ad_count++];
  74.   a.id = next_id++;
  75.   strncpy(a.title, title, sizeof(a.title)); a.title[sizeof(a.title)-1] = '\0';
  76.   strncpy(a.description, description, sizeof(a.description)); a.description[sizeof(a.description)-1] = '\0';
  77.   a.price = price;
  78.   strncpy(a.details, details, sizeof(a.details)); a.details[sizeof(a.details)-1] = '\0';
  79.   strncpy(a.vendor, vendor, sizeof(a.vendor)); a.vendor[sizeof(a.vendor)-1] = '\0';
  80.   a.active = true;
  81. }
  82.  
  83. void deleteAd(uint32_t id){
  84.   int idx = -1;
  85.   for(int i=0;i<ad_count;i++) if(ads[i].id==id){ idx=i; break; }
  86.   if(idx==-1) return;
  87.   for(int i=idx;i<ad_count-1;i++) ads[i] = ads[i+1];
  88.   ad_count--;
  89. }
  90.  
  91. void seedAds(){
  92.   addAd("Summer Sale","Great deals on banners and posters.", 9.99, "Size: 300x600; Materials: Vinyl; Duration: 30 days", "AcmeAds");
  93.   addAd("New Launch","Promote your new product with banner ads.", 19.99, "Exposure: 2 weeks; Target: 18-35", "VendorX");
  94. }
  95.  
  96. // --------- HTTP helpers ---------
  97. String getParamValue(const String& query, const String& key){
  98.   int start = 0;
  99.   while (start < query.length()) {
  100.     int amp = query.indexOf('&', start);
  101.     String part = (amp == -1) ? query.substring(start) : query.substring(start, amp);
  102.     int eq = part.indexOf('=');
  103.     if (eq > 0) {
  104.       String k = part.substring(0, eq);
  105.       String v = part.substring(eq+1);
  106.       if (k == key) return v;
  107.     }
  108.     if (amp == -1) break;
  109.     start = amp + 1;
  110.   }
  111.   return String("");
  112. }
  113.  
  114. // --------- HTML renderers ---------
  115. void printHeader(EthernetClient &client, const char* title){
  116.   client.println("HTTP/1.1 200 OK");
  117.   client.println("Content-Type: text/html");
  118.   client.println("Connection: close");
  119.   client.println();
  120.   client.println("<!DOCTYPE html><html><head>");
  121.   client.print("<title");
  122.   client.print("=");
  123.   client.print('\''');
  124.  client.print(title);
  125.  client.print('\''');
  126.  client.println(">" );
  127.  client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" );
  128.  client.println("<style>body{font-family:Arial, Helvetica, sans-serif;} .card{border:1px solid #ccc; padding:12px; margin:12px;} .btn{display:inline-block; padding:6px 12px; margin:4px; background:#28a745; color:#fff; text-decoration:none; border-radius:4px;} .btn.red{background:#dc3545;} </style>");
  129.  client.println("</head><body>");
  130.  client.print("<h1>Advertisement Manager</h1>");
  131. }
  132.  
  133. void printFooter(EthernetClient &client){
  134.  client.println("</body></html>");
  135. }
  136.  
  137. void sendIndexPage(EthernetClient &client){
  138.  printHeader(client, "Advertisement Manager - View Ads");
  139.  client.println("<div><a class=\"btn\" href=\"/add\">Add Advertisement</a></div>");
  140.  for(int i=0;i<ad_count;i++){
  141.    Ad &a = ads[i];
  142.    client.println("<div class='card'>");
  143.    client.print("<h2>" ); client.print(a.title); client.println("</h2>");
  144.    client.print("<p>" ); client.print(a.description); client.println("</p>");
  145.    char priceBuf[32]; snprintf(priceBuf, sizeof(priceBuf), "$%.2f", a.price); client.print(priceBuf); client.println("<br/>");
  146.    client.print("Vendor: "); client.print(a.vendor); client.println("<br/>");
  147.    client.print("<a class=\"btn\" href=\"/details?id="); client.print(a.id); client.println("\">Details</a>");
  148.    client.print("<a class=\"btn\" href=\"/edit?id="); client.print(a.id); client.println("\">Edit</a>");
  149.    client.print("<a class=\"btn red\" href=\"/delete?id="); client.print(a.id); client.println("\">Delete</a>");
  150.    client.println("</div>");
  151.  }
  152.  printFooter(client);
  153. }
  154.  
  155. void sendAddForm(EthernetClient &client){
  156.  printHeader(client, "Add Advertisement");
  157.  client.println("<h3>Add Advertisement (GET parameters)</h3>");
  158.  client.println("<form action='/add' method='GET'>");
  159.  client.println("Title: <input type='text' name='title' /> <br/>");
  160.  client.println("Description: <input type='text' name='description' /> <br/>");
  161.  client.println("Price: <input type='text' name='price' /> <br/>");
  162.  client.println("Details: <input type='text' name='details' /> <br/>");
  163.  client.println("Vendor: <input type='text' name='vendor' /> <br/>");
  164.  client.println("<input type='submit' value='Add Ad' />");
  165.  client.println("</form>");
  166.  client.println("<p>Or use URL: /add?title=...&description=...&price=...&details=...&vendor=...</p>");
  167.  printFooter(client);
  168. }
  169.  
  170. void sendEditForm(EthernetClient &client, uint32_t id){
  171.  Ad* a = findAdById(id);
  172.  if(!a){ sendIndexPage(client); return; }
  173.  printHeader(client, "Edit Advertisement");
  174.  client.print("<h3>Edit Advertisement</h3>");
  175.  client.print("<form action='/edit' method='GET'>");
  176.  client.print("<input type='hidden' name='id' value='"); client.print(a->id); client.println("' />");
  177.  client.print("Title: <input type='text' name='title' value='"); client.print(a->title); client.println("' /> <br/>");
  178.  client.print("Description: <input type='text' name='description' value='"); client.print(a->description); client.println("' /> <br/>");
  179.  client.print("Price: <input type='text' name='price' value='"); {
  180.    char buf[32]; snprintf(buf, sizeof(buf), "%.2f", a->price); client.print(buf); } client.println("' /> <br/>");
  181.  client.print("Details: <input type='text' name='details' value='"); client.print(a->details); client.println("' /> <br/>");
  182.  client.print("Vendor: <input type='text' name='vendor' value='"); client.print(a->vendor); client.println("' /> <br/>");
  183.  client.println("<input type='submit' value='Update Ad' />");
  184.  client.println("</form>");
  185.  printFooter(client);
  186. }
  187.  
  188. void sendDetailsPage(EthernetClient &client, uint32_t id){
  189.  Ad* a = findAdById(id);
  190.  printHeader(client, "Advertisement Details");
  191.  if(a){
  192.    client.print("<h2>"); client.print(a->title); client.println("</h2>");
  193.    client.print("<p>" ); client.print(a->description); client.println("</p>");
  194.    char priceBuf[32]; snprintf(priceBuf, sizeof(priceBuf), "$%.2f", a->price); client.print(priceBuf); client.println("<br/>");
  195.    client.print("Details: "); client.print(a->details); client.println("<br/>");
  196.    client.print("Vendor: "); client.print(a->vendor); client.println("<br/>");
  197.  } else {
  198.    client.println("<p>Ad not found.</p>");
  199.  }
  200.  client.println("<a class='btn' href='/'>Back to list</a>");
  201.  printFooter(client);
  202. }
  203.  
  204. void ensureServerStarted(){
  205.  // placeholder for potential future checks
  206. }
  207.  
  208. // ******* setup / loop *******
  209. void setup(){
  210.  Serial.begin(115200);
  211.  while(!Serial) { ; }
  212.  Serial.println("Starting Advertisement Manager Web Server");
  213.  
  214.  // Try DHCP first
  215.  if (Ethernet.begin(mac) == 0) {
  216.    Serial.println("DHCP failed, using static IP");
  217.    Ethernet.begin(mac, staticIP, gateway, subnet);
  218.  }
  219.  server.begin();
  220.  Serial.print("Server started at IP: ");
  221.  Serial.println(Ethernet.localIP());
  222.  
  223.  // Seed initial items
  224.  seedAds();
  225. }
  226.  
  227. void loop(){
  228.  EthernetClient client = server.available();
  229.  if (client) {
  230.    boolean currentLineIsBlank = true;
  231.    String req = "";
  232.    // Read HTTP request
  233.    while (client.connected()) {
  234.      if (client.available()) {
  235.        char c = client.read();
  236.        req += c;
  237.        if (req.indexOf("\r\n\r\n") != -1) break; // end of headers
  238.      }
  239.    }
  240.  
  241.    // Parse request line
  242.    String method = "";
  243.    String path = "";
  244.    String query = "";
  245.    int firstLineEnd = req.indexOf('\n');
  246.    if (firstLineEnd != -1) {
  247.      String requestLine = req.substring(0, firstLineEnd);
  248.      requestLine.trim();
  249.      int sp1 = requestLine.indexOf(' ');
  250.      int sp2 = requestLine.indexOf(' ', sp1+1);
  251.      if (sp1>0 && sp2>sp1) {
  252.        method = requestLine.substring(0, sp1);
  253.        String fullPath = requestLine.substring(sp1+1, sp2);
  254.        int qpos = fullPath.indexOf('?');
  255.        if (qpos >= 0) {
  256.          path = fullPath.substring(0, qpos);
  257.          query = fullPath.substring(qpos+1);
  258.        } else {
  259.          path = fullPath;
  260.        }
  261.      }
  262.    }
  263.    if (path == "/") path = "/ads";
  264.  
  265.    // Handle routes
  266.    if (path == "/ads") {
  267.      sendIndexPage(client);
  268.    } else if (path == "/add") {
  269.      if (query.length() > 0) {
  270.        String t = getParamValue(query, "title");
  271.        String d = getParamValue(query, "description");
  272.        String pr = getParamValue(query, "price");
  273.        String det = getParamValue(query, "details");
  274.        String ven = getParamValue(query, "vendor");
  275.        float price = 0.0;
  276.        if (pr.length() > 0) price = pr.toFloat();
  277.        if (t.length() > 0) addAd(t.c_str(), d.c_str(), price, det.c_str(), ven.c_str());
  278.        sendIndexPage(client);
  279.      } else {
  280.        sendAddForm(client);
  281.      }
  282.    } else if (path == "/edit") {
  283.      String idStr = getParamValue(query, "id");
  284.      uint32_t id = idStr.toInt();
  285.      // If fields provided, update
  286.      String t = getParamValue(query, "title");
  287.      if (t.length() > 0 && id > 0) {
  288.        Ad* a = findAdById(id);
  289.        if (a) {
  290.          String d = getParamValue(query, "description");
  291.          String pr = getParamValue(query, "price");
  292.          String det = getParamValue(query, "details");
  293.          String ven = getParamValue(query, "vendor");
  294.          if (d.length() > 0) strncpy(a->description, d.c_str(), sizeof(a->description));
  295.          if (pr.length() > 0) a->price = pr.toFloat();
  296.          if (det.length() > 0) strncpy(a->details, det.c_str(), sizeof(a->details));
  297.          if (ven.length() > 0) strncpy(a->vendor, ven.c_str(), sizeof(a->vendor));
  298.        }
  299.        sendIndexPage(client);
  300.      } else {
  301.        sendEditForm(client, id);
  302.      }
  303.    } else if (path == "/delete") {
  304.      String idStr = getParamValue(query, "id");
  305.      uint32_t id = idStr.toInt();
  306.      if (id > 0) deleteAd(id);
  307.      sendIndexPage(client);
  308.    } else if (path == "/details") {
  309.      String idStr = getParamValue(query, "id");
  310.      uint32_t id = idStr.toInt();
  311.      sendDetailsPage(client, id);
  312.    } else {
  313.      // 404
  314.      client.println("HTTP/1.1 404 Not Found");
  315.      client.println("Content-Type: text/plain");
  316.      client.println("Connection: close");
  317.      client.println();
  318.      client.println("Not Found");
  319.    }
  320.  
  321.    delay(1);
  322.    client.stop();
  323.  }
  324. }
  325.  
  326. /* END CODE */
  327.  
Advertisement
Add Comment
Please, Sign In to add comment