Advertisement
Guest User

Arduino sketch for remote programming by SISTEMAS O.R.P.

a guest
Nov 2nd, 2014
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1.  
  2. // Copyright (C) 2014 SISTEMAS O.R.P.
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. #define SEARCH_AFTER_RESET "ready"
  18. #define INTRO "\r\n"
  19.  
  20. #define AP_NORMAL "SISTEMASORP"
  21. #define PWD_NORMAL "mypassword"
  22. #define HOST_NORMAL "192.168.1.33"
  23. #define PORT_NORMAL "49000"
  24.  
  25. #define AP_BOOTLOADER "SISTEMASORP"
  26. #define PWD_BOOTLOADER "mypassword"
  27. #define HOST_BOOTLOADER "192.168.1.33"
  28. #define PORT_BOOTLOADER "50000"
  29.  
  30.  
  31. boolean ok = false;
  32.  
  33. String send(String command, int timeout, String wait_for1, String wait_for2)
  34. {
  35.   unsigned long time = millis();
  36.   String received = "";
  37.  
  38.   Serial.print(command);
  39.   Serial.print(INTRO);
  40.  
  41.   while(millis() < (time + timeout))
  42.   {
  43.     if(Serial.available() > 0)
  44.     {
  45.       received.concat(char(Serial.read()));
  46.       if(received.indexOf(wait_for1) > -1)
  47.       {
  48.         return wait_for1;
  49.       }
  50.       else if(received.indexOf(wait_for2) > -1)
  51.       {
  52.         return wait_for2;
  53.       }
  54.     }
  55.   }
  56.  
  57.   return "TIMEOUT";
  58. }
  59.  
  60. boolean look_for(String wait_for, int timeout)
  61. {
  62.   unsigned long time = millis();
  63.   String received = "";
  64.  
  65.   while(millis() < (time + timeout))
  66.   {
  67.     if(Serial.available() > 0)
  68.     {
  69.       received.concat(Serial.readString());
  70.       if(received.indexOf(wait_for) > -1)
  71.       {
  72.         return true;
  73.       }
  74.     }
  75.   }
  76.  
  77.   return false;
  78. }
  79.  
  80. boolean send_command(String command, int timeout, String wait_for1, String wait_for2)
  81. {
  82.   String state;
  83.  
  84.   state = send(command, timeout, wait_for1, wait_for2);
  85.   if(state == wait_for1)
  86.   {
  87.     return true;
  88.   }
  89.   else if(state == wait_for2)
  90.   {
  91.     // do something on error
  92.   }
  93.   else
  94.   {
  95.     // do something on timeout
  96.   }
  97.  
  98.   return false;
  99. }
  100.  
  101. boolean init_commands()
  102. {
  103.   if (send_command("AT+RST", 5000, SEARCH_AFTER_RESET, "ERROR"))
  104.   {
  105.     String cwjap = "AT+CWJAP=\"";
  106.     cwjap += AP_NORMAL;
  107.     cwjap += "\",\"";
  108.     cwjap += PWD_NORMAL;
  109.     cwjap += "\"";
  110.     if (send_command(cwjap, 20000, "OK", "FAIL"))
  111.       if (send_command("AT+CIPMUX=0", 2000, "OK", "ERROR"))
  112.         if (send_command("AT+CIPMODE=0", 2000, "OK", "ERROR"))
  113.         {
  114.           String cipstart = "AT+CIPSTART=\"TCP\",\"";
  115.           cipstart += HOST_NORMAL;
  116.           cipstart += "\",";
  117.           cipstart += PORT_NORMAL;
  118.           if (send_command(cipstart, 20000, "OK", "ERROR"))
  119.             return true;
  120.         }
  121.   }
  122.            
  123.   return false;
  124. }
  125.  
  126. boolean boot_commands()
  127. {
  128.  
  129.   if (send_command("AT+RST", 5000, SEARCH_AFTER_RESET, "ERROR"))
  130.   {
  131.     String cwjap = "AT+CWJAP=\"";
  132.     cwjap += AP_BOOTLOADER;
  133.     cwjap += "\",\"";
  134.     cwjap += PWD_BOOTLOADER;
  135.     cwjap += "\"";
  136.     if (send_command(cwjap, 20000, "OK", "FAIL"))
  137.       if (send_command("AT+CIPMUX=0", 2000, "OK", "ERROR"))
  138.         if (send_command("AT+CIPMODE=1", 2000, "OK", "ERROR"))
  139.         {
  140.           String cipstart = "AT+CIPSTART=\"TCP\",\"";
  141.           cipstart += HOST_BOOTLOADER;
  142.           cipstart += "\",";
  143.           cipstart += PORT_BOOTLOADER;
  144.           if (send_command(cipstart, 20000, "OK", "ERROR"))
  145.             if (send_command("AT+CIPSEND", 2000, ">", "ERROR"))
  146.               if (send_command("hello", 2000, "welcome", "error"))
  147.                 if (send_command("Arduino_remote_example.cpp.hex", 2000, "ok", "error"))
  148.                   return true;
  149.         }
  150.   }
  151.            
  152.   return false;
  153. }
  154.  
  155.  
  156. boolean test()
  157. {
  158.  
  159.   if (send_command("AT+CIPSEND=13", 2000, ">", "ERROR"))
  160.     if (send_command("hello world!", 2000, "OK", "ERROR"))
  161.       return true;
  162.  
  163.   return false;
  164. }
  165.  
  166. void setup()
  167. {
  168.   pinMode(13, OUTPUT);
  169.   /*
  170.   Serial.begin(9600);
  171.   send_command("AT+RST", 2000, "OK", "ERROR");
  172.   send_command("AT+CIOBAUD=115200", 2000, "OK", "ERROR");
  173.   */
  174.   Serial.begin(115200);
  175.  
  176.   delay(5000);
  177.   digitalWrite(13, HIGH);
  178.   ok = init_commands();
  179. }
  180.  
  181. void loop()
  182. {
  183.   if(ok)
  184.   {
  185.     digitalWrite(13, HIGH);
  186.     ok = test();
  187.     if(look_for("reboot", 10000))
  188.     {
  189.       if(boot_commands())
  190.       {
  191.         pinMode(12, OUTPUT);
  192.         digitalWrite(12, LOW);
  193.         ok = false;
  194.       }
  195.       else
  196.       {
  197.         ok = false;
  198.       }
  199.     }
  200.   }  
  201.   else
  202.   {
  203.     digitalWrite(13, HIGH);
  204.     delay(300);
  205.     digitalWrite(13, LOW);
  206.     delay(300);
  207.   }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement