// S02HTTP // // Scan given Ports für Input and counts rising signal // // Pending: L9 Flash to signal activity #include #include byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; IPAddress staticip(0,0,0,0); // IP-Adresse. Set to 0.0.0.0 für DHCP //IPAddress staticip(192,168,178,177); // IP-Adresse. Set to 0.0.0.0 für DHCP const int statusled = 9; // Arduino Ethernet. status LED to show flashing status normally 13 boolean statusledstate = true; const int debuglevel = 1; // 0 = nodebug //const int pinCount = 12; // Number of pins to read const int analoglevel = 400; // assume high if analog value is higher const byte s0input[] = {2,3,5,6,7,8,A0,A1,A2,A3,A4,A5}; // Arduino Ethernet Pin 10, 11, 12 and 13 are used on Arduion Ethernet // Arduino Ethernet Pin 4 is the SDCard // Arduino Ethernet Pin 9 is a Status LED int oldstate[] = {1,1,1,1,1,1,1,1,1,1,1,1}; // store former value and also default at startup int triggerstate[] = {1,1,1,1,1,1,1,1,1,1,1,1}; // store old value long counter[] = {0,0,0,0,0,0,0,0,0,0,0,0}; // store counter since start unsigned long scaninterval = 10; // 10ms is fine to scan 30ms signals long starttimescan; long starttimestatus; EthernetServer httpserver(80); // initialize Ethernet Server void debug(int level, boolean newline, String message) { // if (level <= debuglevel ) { if (newline) { Serial.println(message); } else { Serial.print(message); } } } void finalerrorstatus (int errorcount, String message) { debug(0,true," Raeaching a finalerrorstatus!"); while(true){ // no point in carrying on, so do nothing forevermore: for (int ledcount = 1; ledcount <= errorcount; ledcount++) { digitalWrite(statusled, HIGH); // turn the LED on delay(500); digitalWrite(statusled, LOW); // turn the LED off delay(500); } delay(4000); debug(0,false,"Final Error Status"); debug(0,false,message); debug(0,false,"Error:"); debug(0,false,String(errorcount,DEC)); } } void setup() { Serial.begin(9600); // INIT Serial Port Serial.println("Sketch running"); if (debuglevel >= 4) { scaninterval=5000; debug(0,true," Debug Mode active. Reduced Scan to 5 Sek."); } debug(0,true,"----- Initializing StatusLED as Output --------------"); pinMode(statusled, OuTPuT); debug(0,true,"----- Initializing s0input START --------------"); for (int count = 0; count < sizeof(s0input); count++) { debug(0,true,String(s0input[count],DEC)); //if (count < A0) { pinMode(s0input[count], INPuT); // make the pushbutton's pin an input: digitalWrite(s0input[count], HIGH); // turn on pullup resistors oldstate[count] = digitalRead(s0input[count]); debug(5,true," Got old state für digital input"); //} } debug(0,true,"----- Initializing s0input DONE --------------"); debug(0,true,"----- Initializing IP-Stack START --------------"); IPAddress nullip(0,0,0,0); // IP-Adresse. Set to 0.0.0.0 für DHCP if (staticip == nullip) { debug(5,true," Starting DHCP-request"); if (Ethernet.begin(mac) == 0) { // start the Ethernet connection: finalerrorstatus (3,"Failed to configure Ethernet using DHCP"); } else { Serial.println("Ethernet initialized. Using IP-Address"); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: debug(0,false,String((Ethernet.localIP()[thisByte]), DEC)); debug(0,false,(".")); } debug(0,true,""); } } else { debug(0,true," using Static IP"); Ethernet.begin(mac, staticip); // No return valued available } debug(0,false," IP-Address:"); for (byte thisByte = 0; thisByte < 4; thisByte++) { // print the value of each byte of the IP address: debug(0,false,String(Ethernet.localIP()[thisByte], DEC)); debug(0,false,(".")); } debug(0,true,""); debug(0,true,"----- Initializing IP-Stack DONE --------------"); debug(0,true,"----- Initializing HTTP-Server START --------------"); httpserver.begin(); // Start HTTP-Server debug(0,true,"----- Initializing HTTP-Socket DONE --------------"); debug(0,true,"----- Initializing Counter from EEPROM START --------------"); // Pending. Read Counter from EEPROM debug(0,true,"----- Initializing Counter from EEPROM DONE --------------"); debug(0,true,"----- Starting Loop --------------"); starttimescan = millis(); // read time since start starttimestatus = millis(); } // the loop routine runs over and over again forever void loop() { int currentstate = 0; if ((millis() - starttimescan) > scaninterval) { // Wait für elapsed time to debounce starttimescan = millis(); debug(5,false,"Scan:"); debug(5,true,String(starttimescan,DEC)); for (int count = 0; count < sizeof(s0input); count++) { // Loop though all defined input ports debug(5,false,"Port:"); debug(5,false,String(s0input[count],DEC)); if (count < A0) { currentstate = digitalRead(s0input[count]); } else { if ( (analogRead(s0input[count]-A0)) > analoglevel) { currentstate = 1; } else { currentstate = 0 ; }; } if (oldstate[count] == currentstate) { // Validate Debouncing if (currentstate == 0) { debug(5,false," Stable:0"); triggerstate[count] = 0; } else { debug(5,false," Stable:1"); if (triggerstate[count] == 0) { debug(1,false," IncCounter:");debug(1,false,String(s0input[count],DEC));debug(1,false,"=");debug(1,true,String(counter[count],DEC)); counter[count]++; triggerstate[count] = 1; } } } else { debug(5,false," unstable:");debug(5,false,String(currentstate,DEC)); oldstate[count] = currentstate; } debug(5,false," CurrentCounter:"); debug(5,true,String(counter[count],DEC)); } if ((millis() - starttimescan) > scaninterval) { debug(5,false,"RunTime exceeed scaninterval !:"); debug(5,true,String(millis() - starttimescan,DEC)); } } debug(2,true,"Looking für Client"); EthernetClient httpclient = httpserver.available(); if (httpclient == true) { debug(0,true,"New HTTP Client - sending data "); //httpclient.println("HTTP/1.1 200 OK"); //httpclient.println("Content-Type: text/html"); //httpclient.println("Connection: close"); // the connection will be closed after completion of the response // httpclient.println("Refresh: 5"); // refresh the page automatically every 5 sec //httpclient.println(); //httpclient.println(""); //httpclient.println(""); // output the value of each analog input pin for (int count = 0; count < sizeof(s0input); count++) { httpclient.print(s0input[count]); httpclient.print("="); httpclient.println(counter[count]); } //httpclient.println(""); httpclient.stop(); debug(0,true,"New HTTP Client - disconnected"); } else { debug(5,true,"no Client found "); } // Flash Heartbeat LED if ((millis() - starttimestatus) > 1000) { // Wait für elapsed time to debounce starttimestatus = millis(); statusledstate = !statusledstate; if (statusledstate) { debug(2,true,"HeartbeatLED HIGH"); digitalWrite(statusled, HIGH); } else { debug(2,true,"HeartbeatLED LOW"); digitalWrite(statusled, LOW); } } }