You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /**************************************************************
  2. *
  3. * This script tries to auto-detect the baud rate
  4. * and allows direct AT commands access
  5. *
  6. * TinyGSM Getting Started guide:
  7. * http://tiny.cc/tiny-gsm-readme
  8. *
  9. **************************************************************/
  10. // Set serial for debug console (to the Serial Monitor, speed 115200)
  11. #define SerialMon Serial
  12. // Set serial for AT commands (to the module)
  13. // Use Hardware Serial on Mega, Leonardo, Micro
  14. #define SerialAT Serial1
  15. // or Software Serial on Uno, Nano
  16. //#include <SoftwareSerial.h>
  17. //SoftwareSerial SerialAT(2, 3); // RX, TX
  18. #include <TinyGsmClient.h>
  19. TinyGsm modem(SerialAT);
  20. void setup() {
  21. // Set console baud rate
  22. SerialMon.begin(115200);
  23. delay(5000);
  24. }
  25. void loop() {
  26. // Detect module baud rate
  27. uint32_t rate = 0;
  28. uint32_t rates[] = { 115200, 9600, 57600, 19200, 74400, 74880 };
  29. SerialMon.println("Autodetecting baud rate");
  30. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  31. SerialMon.print(String("Trying baud rate ") + rates[i] + "... ");
  32. SerialAT.begin(rates[i]);
  33. delay(10);
  34. if (modem.autoBaud(2000)) {
  35. rate = rates[i];
  36. SerialMon.println(F("OK"));
  37. break;
  38. } else {
  39. SerialMon.println(F("fail"));
  40. }
  41. }
  42. if (!rate) {
  43. SerialMon.println(F("***********************************************************"));
  44. SerialMon.println(F(" Module does not respond!"));
  45. SerialMon.println(F(" Check your Serial wiring"));
  46. SerialMon.println(F(" Check the module is correctly powered and turned on"));
  47. SerialMon.println(F("***********************************************************"));
  48. delay(30000L);
  49. return;
  50. }
  51. // Access AT commands from Serial Monitor
  52. SerialMon.println(F("***********************************************************"));
  53. SerialMon.println(F(" You can now send AT commands"));
  54. SerialMon.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
  55. SerialMon.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
  56. SerialMon.println(F("***********************************************************"));
  57. while(true) {
  58. if (SerialAT.available()) {
  59. SerialMon.write(SerialAT.read());
  60. }
  61. if (SerialMon.available()) {
  62. SerialAT.write(SerialMon.read());
  63. }
  64. delay(0);
  65. }
  66. }