Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Ads Manager
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-09-09 13:57:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* generate a website for advertisement managerment */
- /* where users can view and vendors can add edit and */
- /* delete. where more details of the items are placed */
- /* in the view page */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- #include <SPI.h>
- #include <Ethernet.h>
- /****** Advertisement Management Web Server for Arduino Mega with Ethernet Shield ******/
- /****** NOTE: This sketch serves a simple web interface to view ads and lets vendors add, edit, and delete ads.******/
- // Hardware: Arduino Mega with W5100 Ethernet Shield or compatible
- // MAC address for Ethernet
- const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- // Public IP configuration (used if DHCP fails or you want a static IP)
- IPAddress staticIP(192, 168, 1, 177);
- IPAddress gateway(192, 168, 1, 1);
- IPAddress subnet(255, 255, 255, 0);
- EthernetServer server(80);
- /****** Data Model ******/
- #define MAX_ADS 8
- struct Ad {
- uint32_t id;
- char title[64];
- char description[128];
- float price;
- char details[256];
- char vendor[32];
- bool active;
- };
- Ad ads[MAX_ADS];
- int ad_count = 0;
- uint32_t next_id = 1;
- // --------- Utility functions for ads ---------
- Ad* findAdById(uint32_t id){
- for(int i=0;i<ad_count;i++){
- if(ads[i].id==id) return &ads[i];
- }
- return NULL;
- }
- void addAd(const char* title, const char* description, float price, const char* details, const char* vendor){
- if(ad_count>=MAX_ADS) return;
- Ad &a = ads[ad_count++];
- a.id = next_id++;
- strncpy(a.title, title, sizeof(a.title)); a.title[sizeof(a.title)-1] = '\0';
- strncpy(a.description, description, sizeof(a.description)); a.description[sizeof(a.description)-1] = '\0';
- a.price = price;
- strncpy(a.details, details, sizeof(a.details)); a.details[sizeof(a.details)-1] = '\0';
- strncpy(a.vendor, vendor, sizeof(a.vendor)); a.vendor[sizeof(a.vendor)-1] = '\0';
- a.active = true;
- }
- void deleteAd(uint32_t id){
- int idx = -1;
- for(int i=0;i<ad_count;i++) if(ads[i].id==id){ idx=i; break; }
- if(idx==-1) return;
- for(int i=idx;i<ad_count-1;i++) ads[i] = ads[i+1];
- ad_count--;
- }
- void seedAds(){
- addAd("Summer Sale","Great deals on banners and posters.", 9.99, "Size: 300x600; Materials: Vinyl; Duration: 30 days", "AcmeAds");
- addAd("New Launch","Promote your new product with banner ads.", 19.99, "Exposure: 2 weeks; Target: 18-35", "VendorX");
- }
- // --------- HTTP helpers ---------
- String getParamValue(const String& query, const String& key){
- int start = 0;
- while (start < query.length()) {
- int amp = query.indexOf('&', start);
- String part = (amp == -1) ? query.substring(start) : query.substring(start, amp);
- int eq = part.indexOf('=');
- if (eq > 0) {
- String k = part.substring(0, eq);
- String v = part.substring(eq+1);
- if (k == key) return v;
- }
- if (amp == -1) break;
- start = amp + 1;
- }
- return String("");
- }
- // --------- HTML renderers ---------
- void printHeader(EthernetClient &client, const char* title){
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close");
- client.println();
- client.println("<!DOCTYPE html><html><head>");
- client.print("<title");
- client.print("=");
- client.print('\''');
- client.print(title);
- client.print('\''');
- client.println(">" );
- client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" );
- 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>");
- client.println("</head><body>");
- client.print("<h1>Advertisement Manager</h1>");
- }
- void printFooter(EthernetClient &client){
- client.println("</body></html>");
- }
- void sendIndexPage(EthernetClient &client){
- printHeader(client, "Advertisement Manager - View Ads");
- client.println("<div><a class=\"btn\" href=\"/add\">Add Advertisement</a></div>");
- for(int i=0;i<ad_count;i++){
- Ad &a = ads[i];
- client.println("<div class='card'>");
- client.print("<h2>" ); client.print(a.title); client.println("</h2>");
- client.print("<p>" ); client.print(a.description); client.println("</p>");
- char priceBuf[32]; snprintf(priceBuf, sizeof(priceBuf), "$%.2f", a.price); client.print(priceBuf); client.println("<br/>");
- client.print("Vendor: "); client.print(a.vendor); client.println("<br/>");
- client.print("<a class=\"btn\" href=\"/details?id="); client.print(a.id); client.println("\">Details</a>");
- client.print("<a class=\"btn\" href=\"/edit?id="); client.print(a.id); client.println("\">Edit</a>");
- client.print("<a class=\"btn red\" href=\"/delete?id="); client.print(a.id); client.println("\">Delete</a>");
- client.println("</div>");
- }
- printFooter(client);
- }
- void sendAddForm(EthernetClient &client){
- printHeader(client, "Add Advertisement");
- client.println("<h3>Add Advertisement (GET parameters)</h3>");
- client.println("<form action='/add' method='GET'>");
- client.println("Title: <input type='text' name='title' /> <br/>");
- client.println("Description: <input type='text' name='description' /> <br/>");
- client.println("Price: <input type='text' name='price' /> <br/>");
- client.println("Details: <input type='text' name='details' /> <br/>");
- client.println("Vendor: <input type='text' name='vendor' /> <br/>");
- client.println("<input type='submit' value='Add Ad' />");
- client.println("</form>");
- client.println("<p>Or use URL: /add?title=...&description=...&price=...&details=...&vendor=...</p>");
- printFooter(client);
- }
- void sendEditForm(EthernetClient &client, uint32_t id){
- Ad* a = findAdById(id);
- if(!a){ sendIndexPage(client); return; }
- printHeader(client, "Edit Advertisement");
- client.print("<h3>Edit Advertisement</h3>");
- client.print("<form action='/edit' method='GET'>");
- client.print("<input type='hidden' name='id' value='"); client.print(a->id); client.println("' />");
- client.print("Title: <input type='text' name='title' value='"); client.print(a->title); client.println("' /> <br/>");
- client.print("Description: <input type='text' name='description' value='"); client.print(a->description); client.println("' /> <br/>");
- client.print("Price: <input type='text' name='price' value='"); {
- char buf[32]; snprintf(buf, sizeof(buf), "%.2f", a->price); client.print(buf); } client.println("' /> <br/>");
- client.print("Details: <input type='text' name='details' value='"); client.print(a->details); client.println("' /> <br/>");
- client.print("Vendor: <input type='text' name='vendor' value='"); client.print(a->vendor); client.println("' /> <br/>");
- client.println("<input type='submit' value='Update Ad' />");
- client.println("</form>");
- printFooter(client);
- }
- void sendDetailsPage(EthernetClient &client, uint32_t id){
- Ad* a = findAdById(id);
- printHeader(client, "Advertisement Details");
- if(a){
- client.print("<h2>"); client.print(a->title); client.println("</h2>");
- client.print("<p>" ); client.print(a->description); client.println("</p>");
- char priceBuf[32]; snprintf(priceBuf, sizeof(priceBuf), "$%.2f", a->price); client.print(priceBuf); client.println("<br/>");
- client.print("Details: "); client.print(a->details); client.println("<br/>");
- client.print("Vendor: "); client.print(a->vendor); client.println("<br/>");
- } else {
- client.println("<p>Ad not found.</p>");
- }
- client.println("<a class='btn' href='/'>Back to list</a>");
- printFooter(client);
- }
- void ensureServerStarted(){
- // placeholder for potential future checks
- }
- // ******* setup / loop *******
- void setup(){
- Serial.begin(115200);
- while(!Serial) { ; }
- Serial.println("Starting Advertisement Manager Web Server");
- // Try DHCP first
- if (Ethernet.begin(mac) == 0) {
- Serial.println("DHCP failed, using static IP");
- Ethernet.begin(mac, staticIP, gateway, subnet);
- }
- server.begin();
- Serial.print("Server started at IP: ");
- Serial.println(Ethernet.localIP());
- // Seed initial items
- seedAds();
- }
- void loop(){
- EthernetClient client = server.available();
- if (client) {
- boolean currentLineIsBlank = true;
- String req = "";
- // Read HTTP request
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- req += c;
- if (req.indexOf("\r\n\r\n") != -1) break; // end of headers
- }
- }
- // Parse request line
- String method = "";
- String path = "";
- String query = "";
- int firstLineEnd = req.indexOf('\n');
- if (firstLineEnd != -1) {
- String requestLine = req.substring(0, firstLineEnd);
- requestLine.trim();
- int sp1 = requestLine.indexOf(' ');
- int sp2 = requestLine.indexOf(' ', sp1+1);
- if (sp1>0 && sp2>sp1) {
- method = requestLine.substring(0, sp1);
- String fullPath = requestLine.substring(sp1+1, sp2);
- int qpos = fullPath.indexOf('?');
- if (qpos >= 0) {
- path = fullPath.substring(0, qpos);
- query = fullPath.substring(qpos+1);
- } else {
- path = fullPath;
- }
- }
- }
- if (path == "/") path = "/ads";
- // Handle routes
- if (path == "/ads") {
- sendIndexPage(client);
- } else if (path == "/add") {
- if (query.length() > 0) {
- String t = getParamValue(query, "title");
- String d = getParamValue(query, "description");
- String pr = getParamValue(query, "price");
- String det = getParamValue(query, "details");
- String ven = getParamValue(query, "vendor");
- float price = 0.0;
- if (pr.length() > 0) price = pr.toFloat();
- if (t.length() > 0) addAd(t.c_str(), d.c_str(), price, det.c_str(), ven.c_str());
- sendIndexPage(client);
- } else {
- sendAddForm(client);
- }
- } else if (path == "/edit") {
- String idStr = getParamValue(query, "id");
- uint32_t id = idStr.toInt();
- // If fields provided, update
- String t = getParamValue(query, "title");
- if (t.length() > 0 && id > 0) {
- Ad* a = findAdById(id);
- if (a) {
- String d = getParamValue(query, "description");
- String pr = getParamValue(query, "price");
- String det = getParamValue(query, "details");
- String ven = getParamValue(query, "vendor");
- if (d.length() > 0) strncpy(a->description, d.c_str(), sizeof(a->description));
- if (pr.length() > 0) a->price = pr.toFloat();
- if (det.length() > 0) strncpy(a->details, det.c_str(), sizeof(a->details));
- if (ven.length() > 0) strncpy(a->vendor, ven.c_str(), sizeof(a->vendor));
- }
- sendIndexPage(client);
- } else {
- sendEditForm(client, id);
- }
- } else if (path == "/delete") {
- String idStr = getParamValue(query, "id");
- uint32_t id = idStr.toInt();
- if (id > 0) deleteAd(id);
- sendIndexPage(client);
- } else if (path == "/details") {
- String idStr = getParamValue(query, "id");
- uint32_t id = idStr.toInt();
- sendDetailsPage(client, id);
- } else {
- // 404
- client.println("HTTP/1.1 404 Not Found");
- client.println("Content-Type: text/plain");
- client.println("Connection: close");
- client.println();
- client.println("Not Found");
- }
- delay(1);
- client.stop();
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment