Want more features on Pastebin? Sign Up, it's FREE!
Guest

arduino_code

By: a guest on Feb 28th, 2013  |  syntax: C  |  size: 9.20 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Library
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <EEPROM.h>
  5.  
  6. // Watchdog -------------------
  7. #include <avr/io.h>
  8. #include <avr/interrupt.h>
  9. #include <avr/wdt.h>
  10. // ----------------------------
  11.  
  12. #define WDTSet 8000 // Solo i seguenti valori in ms: 16 32 64 125 250 500 1000 2000 4000 8000
  13. #define DEBUG true
  14.  
  15. int chkhttp      = 10000;       // Valore in ms controllo chkhttp
  16. int vtchk        = 5;           // Valore in s loop check controller
  17.  
  18. unsigned long int TWTD;  // Timer Watchdog
  19. unsigned long int TCHK;  // Timer loop check controller
  20.  
  21. // Temporizzazioni
  22. unsigned long int VTIME;
  23.  
  24. // Impostazione scheda ethernet
  25. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x97, 0xFA };     // mac address 16
  26. //byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x97, 0xFD };     // mac address 17
  27. byte ip[] = { 192, 168, 2, 16 };                         // ip arduino
  28. byte gateway[] = { 192, 168, 2, 1 };                     // Router
  29. byte subnet[] = { 255, 255, 255, 0 };                    // Subnet mask
  30. EthernetServer server(8016);                             // Server port
  31.  
  32. byte ws_ip[]  = { 192, 168, 2, 8 };                      // IP Web server
  33. byte cpu_10[] = { 192, 168, 2, 10 };                     // IP Controller 10
  34. byte cpu_11[] = { 192, 168, 2, 11 };                     // IP Controller 11
  35. byte cpu_12[] = { 192, 168, 2, 12 };                     // IP Controller 12
  36. byte cpu_13[] = { 192, 168, 2, 13 };                     // IP Controller 13
  37. byte cpu_14[] = { 192, 168, 2, 14 };                     // IP Controller 14
  38. byte cpu_15[] = { 192, 168, 2, 15 };                     // IP Controller 15
  39. byte cpu_16[] = { 192, 168, 2, 16 };                     // IP Controller 16
  40. byte cpu_17[] = { 192, 168, 2, 17 };                     // IP Controller 16
  41.  
  42. String ip_web_server="192.168.2.8";                      // Web server
  43. String web_server="http://192.168.2.240";                // Web server
  44. String web_page;                                         // Pagine http
  45. String send_data;                                        // Pagine http
  46. String send_data_cdz;                                    // Pagine http
  47. String send_data_servo;                                  // Pagine http
  48. String send_loop;                                        // Pagine http
  49. String readString;
  50. String str_light;                                        // Stringa led
  51.  
  52. String chk_page;                                         // Check Page string
  53.  
  54.  
  55. void setup()
  56. {
  57.  
  58.   // Inizializzazione Watchdog
  59.   WDT_off();
  60.   WDT_Init();
  61.  
  62.   Ethernet.begin(mac, ip, gateway, subnet);
  63.   server.begin();
  64.  
  65.   if ( DEBUG == true )
  66.   {
  67.     Serial.begin(9600);
  68.     Serial.println("Reset controllore 16");
  69.   }
  70.  
  71.  
  72.   EthernetClient client;
  73.   if (client.connect( ws_ip, 80))
  74.   {
  75.     send_data="";
  76.     send_data += "GET /security.php?TYP=SYS&STS=START_Microcontrollore_Test...";
  77.     client.println(send_data);
  78.     client.stop();
  79.   }
  80.  
  81.   TWTD = WDTSet;
  82. }
  83.  
  84. void loop()
  85. {
  86.  
  87.   // --------------------------------------------------------------
  88.   // Read millis() controller
  89.   // --------------------------------------------------------------
  90.   VTIME=millis();
  91.  
  92.  
  93.   // --------------------------------------------------------------
  94.   // Reset or overflow millis()
  95.   // --------------------------------------------------------------
  96.   if ( VTIME == 1000 )
  97.   {
  98.     TWTD = millis() + chkhttp;
  99.   }
  100.    
  101.   if ( DEBUG == 1 )
  102.   {
  103.     Serial.println(millis());
  104.   }
  105.  
  106.   http_control();
  107.  
  108.   // ---------------------------------------------------------------------------------
  109.   // Controllo ricezione dati http altrimenti restart CPU
  110.   // ---------------------------------------------------------------------------------
  111.   if ( VTIME >= TWTD )
  112.   {
  113.     delay(10000);
  114.   }
  115.  
  116.   // ---------------------------------------------------------------------------------
  117.   // Loop chk Ethernet Shield controllori
  118.   // ---------------------------------------------------------------------------------
  119.   if ( VTIME >= TCHK )
  120.   {
  121.     //Chk_controller( cpu_10,8010 );
  122.     //Chk_controller( cpu_11,8011 );
  123.     //Chk_controller( cpu_12,8012 );
  124.     //Chk_controller( cpu_13,8013 );
  125.     //Chk_controller( cpu_14,8014 );
  126.     //Chk_controller( cpu_15,8015 );
  127.     //Chk_controller( cpu_16,8016 );
  128.     Chk_controller( cpu_17,8017 );
  129.     TCHK = VTIME + ( vtchk * 1000 );
  130.   }
  131.  
  132.  
  133.   //  ---------------------------------------------------------
  134.       ResetWatchdog(); // Resetto il watchdog file del loop
  135.   //  ---------------------------------------------------------
  136. }
  137.  
  138.  
  139. // --------------------------------------------------------------
  140. // HTTP client
  141. // --------------------------------------------------------------
  142. void http_control()
  143. {
  144.   EthernetClient client = server.available();
  145. //  EthernetClient client;
  146.    
  147.   if (client)
  148.   {
  149.       boolean currentLineIsBlank = true;
  150.    
  151.       while (client.connected())
  152.       {
  153.         if (client.available())
  154.         {
  155.           char c = client.read();
  156.           readString.concat(c);
  157.    
  158.          if (c == '\n' && currentLineIsBlank)
  159.          {
  160.             if(readString.indexOf("status=1") > 0)
  161.             {
  162.               web_page = "";
  163.               web_page += "Descrizione : \t\tRegolazione CDZ - Power control \n";
  164.  
  165.               web_page += "IP Controller : \t";
  166.               web_page += ip[0]; web_page += "."; web_page += ip[1]; web_page += "."; web_page += ip[2]; web_page += "."; web_page += ip[3]; web_page += "\n";
  167.  
  168.               web_page += "Gateway : \t\t";
  169.               web_page += gateway[0]; web_page += "."; web_page += gateway[1]; web_page += "."; web_page += gateway[2]; web_page += "."; web_page += gateway[3]; web_page += "\n";
  170.  
  171.               web_page += "Subnet : \t\t";
  172.               web_page += subnet[0]; web_page += "."; web_page += subnet[1]; web_page += "."; web_page += subnet[2]; web_page += "."; web_page += subnet[3]; web_page += "\n";
  173.  
  174.               web_page += "Millis : \t\t";
  175.               web_page += millis(); web_page += "\n";
  176.               client.print(web_page);
  177.             }
  178.  
  179.             if(readString.indexOf("wait=1") > 0)
  180.             {
  181.               web_page = "";
  182.               web_page += "Descrizione : \t\tRegolazione CDZ - Power control \n\n\n\n\n";
  183.               web_page += "Forzatura attesa 6 sec. per riavvio ..... \t";
  184.               client.print(web_page);
  185.               delay(10000);
  186.             }
  187.  
  188.             if(readString.indexOf("chk=1") > 0)
  189.             {
  190.               TWTD = VTIME + chkhttp;
  191.               web_page = "";
  192.               web_page += "IP Controller : \t";
  193.               web_page += ip[0]; web_page += "."; web_page += ip[1]; web_page += "."; web_page += ip[2]; web_page += "."; web_page += ip[3]; web_page += "\n";
  194.               client.print(web_page);
  195.             }
  196.  
  197.             readString="";
  198.             client.stop();
  199.         }
  200.       }
  201.     }
  202.   }
  203. }
  204.  
  205.  
  206.  
  207.  
  208. void WDT_Init(void)
  209. {
  210.   cli();                                    // Disable interrupts
  211.   wdt_reset();                              // Reset watchdog
  212.   WDTCSR = (1<<WDCE)|(1<<WDE);              // Set up WDT interrupt
  213.  
  214.   // Set timing
  215.   if ( WDTSet ==   16 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(0<<WDP1)|(0<<WDP0); }   // Start watchdog timer with  16 ms prescaller
  216.   if ( WDTSet ==   32 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0); }   // Start watchdog timer with  32 ms prescaller
  217.   if ( WDTSet ==   64 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(1<<WDP1)|(0<<WDP0); }   // Start watchdog timer with  64 ms prescaller
  218.   if ( WDTSet ==  125 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(1<<WDP1)|(1<<WDP0); }   // Start watchdog timer with 125 ms prescaller
  219.   if ( WDTSet ==  250 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(0<<WDP1)|(0<<WDP0); }   // Start watchdog timer with 250 ms prescaller
  220.   if ( WDTSet ==  500 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(0<<WDP1)|(1<<WDP0); }   // Start watchdog timer with 500 ms prescaller
  221.   if ( WDTSet == 1000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(1<<WDP1)|(0<<WDP0); }   // Start watchdog timer with   1  s prescaller
  222.   if ( WDTSet == 2000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0); }   // Start watchdog timer with   2  s prescaller
  223.   if ( WDTSet == 4000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(0<<WDP0); }   // Start watchdog timer with   4  s prescaller
  224.   if ( WDTSet == 8000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0); }   // Start watchdog timer with   8  s prescaller
  225.  
  226.   sei();                                    // Enable global interrupts
  227. }
  228.  
  229. void WDT_off(void)
  230. {
  231.   MCUSR &= ~(1<<WDRF);             // Clear WDRF in MCUSR - Disable interrupt - Watchdog Reset
  232.   WDTCSR |= (1<<WDCE) | (1<<WDE);  // Write logical one to WDCE and WDE - Keep old prescaler setting to prevent unintentional time-out
  233.   WDTCSR = 0x00;                   // Off WDT
  234. }
  235.  
  236. void ResetWatchdog()
  237. {
  238.     wdt_reset(); //ResetEthernetShield();
  239. }
  240.  
  241.  
  242. void Chk_controller(byte cp_ip[], int prt)
  243. {
  244.   EthernetClient client;
  245.   if ( client.connect( cp_ip, prt))
  246.   {
  247.     send_data="";
  248.     send_data += "GET /?chk=1";
  249.     client.println(send_data);
  250.     client.stop();
  251.   }
  252. }
clone this paste RAW Paste Data