logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Arduino/ENC28J60 - Zmiana adresu IP i maski przez przeglądarkę

Thorgus 04 Sty 2017 01:00 3111 2
REKLAMA
  • #1 16171473
    Thorgus
    Poziom 12  
    Posty: 578
    Pomógł: 4
    Ocena: 53
    Witajcie,

    Wraz z grupą studentów tworzymy projekt domu inteligentnego w wersji open srouce & open hardware

    Jest to prosty sterownik do oświetlenia wykorzystujący arduino nano i kontroler ethernet ENC28J60 (właściwie moduł ethernet)

    Chcielibyśmy z racji łatwiej konfiguracji urządzenia docelowego które będzie płytką PCB mieć możliwość zmiany adresu IP oraz maski wraz z resetem do ustawień fabrycznych także przez przeglądarkę.

    Problem 1:
    Po kliknięciu na "change settings" lub "show settings" zawsze wyświetla się ta sama storna w przeglądarce.

    Problemu 2:
    Nie wiemy jak zmieniać adres ip i maskę oraz w jaki sposób przekazywać te dane z poziomu użytkowania do np. tablicy.

    Problem 3:
    Nie wiemy jak wywołać funkcję po naciśnięciu guziku reset settings

    Za wszelką pomoc bardzo serdecznie dziękujemy.


    Kod
    Kod: C / C++
    Zaloguj się, aby zobaczyć kod


    Arduino/ENC28J60 - Zmiana adresu IP i maski przez przeglądarkę
  • REKLAMA
  • #2 16171545
    Slawek K.
    Poziom 35  
    Posty: 3015
    Pomógł: 259
    Ocena: 1299
    myip, netmask i gwip masz zadeklarowane jako stałe (const) zatem nie możesz dynamicznie zmieniać ich wartości w programie. Usuń static const sprzed deklaracji i spróbuj.

    Pozdr
  • #3 16176012
    Thorgus
    Poziom 12  
    Posty: 578
    Pomógł: 4
    Ocena: 53
    Witaj,

    Niestety to nie jest problemem.

    Taka zmiana niewiele pomorze, głównym problemem jest pobieranie od użytkownika danych wpisanych na stronie WWW
    i przekazywanie ich do tablicy w C.

    Kod bez static inta nic nie daje.

    #include <enc28j60.h>
    #include <EtherCard.h>
    #include <net.h>
    
    /*
    ---------------------------------------------------------
    -             Web control relay with Arduino            - 
    ---------------------------------------------------------
    Ethernet module "ENC28J60" is connected to the Arduino :
    VCC - 3.3V   Arduino
    GND - GND    Arduino
    SCK - Pin 13 Arduino
    SO  - Pin 12 Arduino
    SI  - Pin 11 Arduino
    CS  - Pin 10 Arduino
    
    The relay is connected to the Arduino so:
    VCC - 5V     Arduino
    In1 - Pin 2  Arduino
    In2 - Pin 3  Arduino
    ...
    In7 - Pin 8  Arduino
    In8 - Pin 9  Arduino
    GND - GND    Arduino
    ---------------------------------------------------------
    */
    
    #include <EtherCard.h>
    byte mymac[6] = {0x74,0x69,0x69,0x2D,0x30,0x31};
    // ethernet interface mac address, must be unique on the LAN
    // remote website ip address and port
    //static byte hisip[] = { 74,125,79,99 };
    // remote website name
    // Declare PinStatus and LedPins before BufferFiller declaration
    
    
    uint8_t myip[4] = {192,168,1,3};
    uint8_t netmask[4] = {255,255,255,0};
    uint8_t gwip[4] = {192,168,1,1};
    uint8_t dnsip[4] = {8,8,8,8};
    
    boolean PinStatus[2];
    
    int LedPins[2];
    
    // buffer[500] for 4 relays
    // increase size buffer for more relays
    byte Ethernet::buffer[1200];
    BufferFiller bfill;
    
    const char http_OK[] PROGMEM =
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n\r\n";
    
    const char http_Found[] PROGMEM =
    "HTTP/1.0 302 Found\r\n"
    "Location: /\r\n\r\n";
    
    const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";
    
    //---------------------------------------------------------
    // Web page
    void homePage()
    {
        bfill.emit_p(PSTR("$F"
        "<HTML>"
        "<title>ArduinoPIN Webserver</title>"
        "<p align=center><font size=10>"
        ),
        http_OK);
      
       for(int i = 0; i <= 1; i++)
        {
            bfill.emit_p(PSTR(
            "R$D: <a href='?P$D=$F' style=color:$F>$F</a><br />"
            ),
            i+1,
            i+1,
            PinStatus[i]?PSTR("off"):PSTR("on"),    
            PinStatus[i]?PSTR("green"):PSTR("red"),
            PinStatus[i]?PSTR("ON"):PSTR("OFF"));
        }
    
    
        bfill.emit_p(PSTR("<a href=\"50000/show_settings/\">Show settings</a></br>"));
        bfill.emit_p(PSTR("<a href=\"50000/change_settings/\">Change settings</a></br>"));
        bfill.emit_p(PSTR("<a href=\"50000/reset_to_deflaut/\">Reset to deflaut</a></br>"));
        
        bfill.emit_p(PSTR(
        "</font></p>"
        "</HTML>" 
        )); 
        
    }
    
    
        
        void reset_to_default()
        {
          bfill.emit_p(PSTR("$F"
        "<HTML>"
        "<title>ArduinoPIN Webserver</title>"
        "<p align=center><font size=5>"
        ),
        http_OK);
          
           bfill.emit_p(PSTR(
        "<button type="
        "button"
        ">Save settings</button></br>"
        ));
          
          bfill.emit_p(PSTR(
        "</font></p>"
        "</HTML>" 
        )); 
        }
    
        void change_settings()
        {
        bfill.emit_p(PSTR("$F"
        "<HTML>"
        "<title>ArduinoPIN Webserver</title>"
        "<p align=center><font size=4>"),http_OK);
        bfill.emit_p(PSTR("<body>"));
        bfill.emit_p(PSTR("<form>"));
        bfill.emit_p(PSTR("<input type=\"radio\" name=\"ip\" value=\"DHCP\" checked> DHCP<br>"));
        bfill.emit_p(PSTR("<input type=\"radio\" name=\"ip\" value=\"Static_IP\"> Static IP<br>"));
        bfill.emit_p(PSTR("</form>"));
          bfill.emit_p(PSTR("IP: "));
          bfill.emit_p(PSTR("<input value=\"$D\">."),myip[0]); 
          bfill.emit_p(PSTR("<input value=\"$D\">."),myip[1]);
          bfill.emit_p(PSTR("<input value=\"$D\">."),myip[2]);
          bfill.emit_p(PSTR("<input value=\"$D\"></br>"),myip[3]);
    
          bfill.emit_p(PSTR("MASK: "));
          bfill.emit_p(PSTR("<input value=\"$D\">."),netmask[0]); 
          bfill.emit_p(PSTR("<input value=\"$D\">."),netmask[1]);
          bfill.emit_p(PSTR("<input value=\"$D\">."),netmask[2]);
          bfill.emit_p(PSTR("<input value=\"$D\"></br>"),netmask[3]);
    
          bfill.emit_p(PSTR("GW: "));
          bfill.emit_p(PSTR("<input value=\"$D\">."),gwip[0]); 
          bfill.emit_p(PSTR("<input value=\"$D\">."),gwip[1]);
          bfill.emit_p(PSTR("<input value=\"$D\">."),gwip[2]);
          bfill.emit_p(PSTR("<input value=\"$D\"></br>"),gwip[3]);
    
          bfill.emit_p(PSTR("MAC: "));
          bfill.emit_p(PSTR("<input value=\"$H\">:"),mymac[0]); 
          bfill.emit_p(PSTR("<input value=\"$H\">:"),mymac[1]);
          bfill.emit_p(PSTR("<input value=\"$H\">:"),mymac[2]);
          bfill.emit_p(PSTR("<input value=\"$H\">:"),mymac[3]);
          bfill.emit_p(PSTR("<input value=\"$H\">:"),mymac[4]);
          bfill.emit_p(PSTR("<input value=\"$H\"></br> "),mymac[5]);
    
          bfill.emit_p(PSTR("DNS: "));
          bfill.emit_p(PSTR("<input value=\"$D\">."),dnsip[0]); 
          bfill.emit_p(PSTR("<input value=\"$D\">."),dnsip[1]);
          bfill.emit_p(PSTR("<input value=\"$D\">."),dnsip[2]);
          bfill.emit_p(PSTR("<input value=\"$D\"></br>"),dnsip[3]);
    
          bfill.emit_p(PSTR("<a href=\"http://$D.$D.$D.$D/\">Save settings</a></br>"));
    
          bfill.emit_p(PSTR("<a href=\"http://$D.$D.$D.$D/\">Back to control page</a></br>"),myip[0],myip[1],myip[2],myip[3]);
      /*   
          bfill.emit_p(PSTR("IP: "));
          bfill.emit_p(PSTR("<input type=\"number\" maxlength=\"3\" min=\"10\" max=\"255\" size=\"0\" value=\"$D\">."),myip[0]); 
          bfill.emit_p(PSTR("<input type=\"number\" maxlength=\"3\" min=\"10\" max=\"255\" size=\"0\" value=\"$D\">."),myip[1]);
          bfill.emit_p(PSTR("<input type=\"number\" maxlength=\"3\" min=\"10\" max=\"255\" size=\"0\" value=\"$D\">."),myip[2]);
          bfill.emit_p(PSTR("<input type=\"number\" maxlength=\"3\" min=\"10\" max=\"255\" size=\"0\" value=\"$D\"></br>"),myip[3]);
       */  
        bfill.emit_p(PSTR(
        "</body>"
        "</font></p>"
        "</HTML>" 
        ));
        }
        
        void show_settings()
        {
        bfill.emit_p(PSTR("$F"
        "<HTML>"
        "<title>ArduinoPIN Webserver</title>"
        "<p align=center><font size=4>"
        ),
        http_OK);  
        bfill.emit_p( PSTR("IP Address: $D.$D.$D.$D\n</br>"), myip[0],myip[1],myip[2],myip[3] );
        bfill.emit_p( PSTR("Mask: $D.$D.$D.$D\n</br>"), netmask[0],netmask[1],netmask[2],netmask[3] );
        bfill.emit_p( PSTR("GW Address: $D.$D.$D.$D\n</br>"), gwip[0],gwip[1],gwip[2],gwip[3] );
        bfill.emit_p( PSTR("MAC: $H:$H:$H:$H:$H:$H\n</br>"), mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5] );
        bfill.emit_p(PSTR("<a href=\"http://$D.$D.$D.$D/\">Back to control page</a>"),myip[0],myip[1],myip[2],myip[3]);
        bfill.emit_p(PSTR(
        "</font></p>"
        "</HTML>" 
        )); 
        }
        
    //---------------------------------------------------------
    
    
    void setup()
    {
      
        Serial.begin(9600);
        Serial.print("Setup ");
    
        // Pins on Arduino D2 and D3
        LedPins[0] = 2;
        LedPins[1] = 3;
    
        // if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
        // and change it (CS-pin) to pin 10.
        Serial.println(F("\nConnecting..."));
        if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0);
            Serial.println(F("Failed to access Ethernet controller"));
        if (!ether.dhcpSetup()); 
            Serial.println(F("DHCP failed"));
        Serial.print("Connected\n");
    
        ether.printIp("My Router IP: ", ether.myip); 
    
        //ether.staticSetup(myip);
    
        ether.printIp("My SET IP: ", ether.myip); 
    
        // Debug
        Serial.print("\n");
        Serial.print("|-------------------------------------------|\n");
        Serial.print("| Actions | LedPin       /        PinStatus |\n");
        Serial.print("|-------------------------------------------|\n");
        Serial.print("| Init    | ");
    
        for(int i = 0; i <= 1; i++)
        {
            // Initialization of pin mode and pin status
            pinMode(LedPins[i],OUTPUT); 
            digitalWrite (LedPins[i],HIGH);
            PinStatus[i]=false;
            // Debug
            Serial.print(LedPins[i]);
            Serial.print("/");
            Serial.print(PinStatus[i]);
            Serial.print(" ");
        }
        Serial.print("\n");
    }
    
    //---------------------------------------------------------
    void loop()
    {
    
        delay(1);
    // bool a=0; if(digitalRead(tenbutton){if(a==0){a=1; delay(500);} else {a=0; delay(500);)}}
        word len = ether.packetReceive();   // check for ethernet packet
        word pos = ether.packetLoop(len);   // check for tcp packet
    
        if (pos) {
            bfill = ether.tcpOffset();
            char *data = (char *) Ethernet::buffer + pos;
            if (strncmp("GET /",data, 5) != 0) {
                bfill.emit_p(http_Unauthorized);
            }
            else {
                data += 5;
                int relay=data[2] - 48; // ASCII code for the character '0' is decimal 48
                if (data[0] == ' ') {
                    // If change on home page, execute function.
                    homePage();
                    //Debug
                    for (int i = 0; i <= 1; i++)digitalWrite(LedPins[i],!PinStatus[i]);
                        Serial.print("| F5      | ");
                } else if( relay>0 && relay<=2 ) {
                    data += 4;
                    if (strncmp("on ", data, 3) == 0) {
                        PinStatus[relay-1] = true;        
                        bfill.emit_p(http_Found);
                        // Debug
                        Serial.print("| R");
                        Serial.print(relay-1);
                        Serial.print(" ON   | ");
                    } else if (strncmp("off ", data, 4) == 0) {
                        PinStatus[relay-1] = false;
                        bfill.emit_p(http_Found);
                        // Debug
                        Serial.print("| R");
                        Serial.print(relay-1);
                        Serial.print(" OFF  | ");
                    }
                }
    
                 else if(strncmp("/50000/show_settings/",data, 21) != 0)
                {
                      change_settings();
                }
    
                else if(strncmp("/50000/change_settings/",data, 23) != 0)
                {
                   show_settings();   
                }
                
               
                else if(strncmp("/50000/reset_to_deflaut/",data, 24) != 0)
                {
                    reset_to_default();
                }
               
    
                else {
                    // Page not found
                    bfill.emit_p(http_Unauthorized);
                }
    
    
                
                
                // Debug
                for(int i = 0; i <= 1; i++)
                {
                    Serial.print(LedPins[i]);
                    Serial.print("/");
                    Serial.print(PinStatus[i]);
                    Serial.print(" ");
                }
                Serial.print("\n");
            }
            ether.httpServerReply(bfill.position());    // send http response
        }
    }
    
    


    Dodano po 4 [godziny] 12 [minuty]:

    Jeżeli to pomoże w jakikolwiek sposób znalazłem projekt z inputem danych oparty o arduino i moduł ENC28J60
    http://blog.thiseldo.co.uk/?p=336
REKLAMA