Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (C) 2014 SISTEMAS O.R.P.
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- #define SEARCH_AFTER_RESET "ready"
- #define INTRO "\r\n"
- #define AP_NORMAL "SISTEMASORP"
- #define PWD_NORMAL "mypassword"
- #define HOST_NORMAL "192.168.1.33"
- #define PORT_NORMAL "49000"
- #define AP_BOOTLOADER "SISTEMASORP"
- #define PWD_BOOTLOADER "mypassword"
- #define HOST_BOOTLOADER "192.168.1.33"
- #define PORT_BOOTLOADER "50000"
- boolean ok = false;
- String send(String command, int timeout, String wait_for1, String wait_for2)
- {
- unsigned long time = millis();
- String received = "";
- Serial.print(command);
- Serial.print(INTRO);
- while(millis() < (time + timeout))
- {
- if(Serial.available() > 0)
- {
- received.concat(char(Serial.read()));
- if(received.indexOf(wait_for1) > -1)
- {
- return wait_for1;
- }
- else if(received.indexOf(wait_for2) > -1)
- {
- return wait_for2;
- }
- }
- }
- return "TIMEOUT";
- }
- boolean look_for(String wait_for, int timeout)
- {
- unsigned long time = millis();
- String received = "";
- while(millis() < (time + timeout))
- {
- if(Serial.available() > 0)
- {
- received.concat(Serial.readString());
- if(received.indexOf(wait_for) > -1)
- {
- return true;
- }
- }
- }
- return false;
- }
- boolean send_command(String command, int timeout, String wait_for1, String wait_for2)
- {
- String state;
- state = send(command, timeout, wait_for1, wait_for2);
- if(state == wait_for1)
- {
- return true;
- }
- else if(state == wait_for2)
- {
- // do something on error
- }
- else
- {
- // do something on timeout
- }
- return false;
- }
- boolean init_commands()
- {
- if (send_command("AT+RST", 5000, SEARCH_AFTER_RESET, "ERROR"))
- {
- String cwjap = "AT+CWJAP=\"";
- cwjap += AP_NORMAL;
- cwjap += "\",\"";
- cwjap += PWD_NORMAL;
- cwjap += "\"";
- if (send_command(cwjap, 20000, "OK", "FAIL"))
- if (send_command("AT+CIPMUX=0", 2000, "OK", "ERROR"))
- if (send_command("AT+CIPMODE=0", 2000, "OK", "ERROR"))
- {
- String cipstart = "AT+CIPSTART=\"TCP\",\"";
- cipstart += HOST_NORMAL;
- cipstart += "\",";
- cipstart += PORT_NORMAL;
- if (send_command(cipstart, 20000, "OK", "ERROR"))
- return true;
- }
- }
- return false;
- }
- boolean boot_commands()
- {
- if (send_command("AT+RST", 5000, SEARCH_AFTER_RESET, "ERROR"))
- {
- String cwjap = "AT+CWJAP=\"";
- cwjap += AP_BOOTLOADER;
- cwjap += "\",\"";
- cwjap += PWD_BOOTLOADER;
- cwjap += "\"";
- if (send_command(cwjap, 20000, "OK", "FAIL"))
- if (send_command("AT+CIPMUX=0", 2000, "OK", "ERROR"))
- if (send_command("AT+CIPMODE=1", 2000, "OK", "ERROR"))
- {
- String cipstart = "AT+CIPSTART=\"TCP\",\"";
- cipstart += HOST_BOOTLOADER;
- cipstart += "\",";
- cipstart += PORT_BOOTLOADER;
- if (send_command(cipstart, 20000, "OK", "ERROR"))
- if (send_command("AT+CIPSEND", 2000, ">", "ERROR"))
- if (send_command("hello", 2000, "welcome", "error"))
- if (send_command("Arduino_remote_example.cpp.hex", 2000, "ok", "error"))
- return true;
- }
- }
- return false;
- }
- boolean test()
- {
- if (send_command("AT+CIPSEND=13", 2000, ">", "ERROR"))
- if (send_command("hello world!", 2000, "OK", "ERROR"))
- return true;
- return false;
- }
- void setup()
- {
- pinMode(13, OUTPUT);
- /*
- Serial.begin(9600);
- send_command("AT+RST", 2000, "OK", "ERROR");
- send_command("AT+CIOBAUD=115200", 2000, "OK", "ERROR");
- */
- Serial.begin(115200);
- delay(5000);
- digitalWrite(13, HIGH);
- ok = init_commands();
- }
- void loop()
- {
- if(ok)
- {
- digitalWrite(13, HIGH);
- ok = test();
- if(look_for("reboot", 10000))
- {
- if(boot_commands())
- {
- pinMode(12, OUTPUT);
- digitalWrite(12, LOW);
- ok = false;
- }
- else
- {
- ok = false;
- }
- }
- }
- else
- {
- digitalWrite(13, HIGH);
- delay(300);
- digitalWrite(13, LOW);
- delay(300);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement