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.4 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
  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 SerialMonitor 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 <SoftwareSerialMonitor.h>
  17. //SoftwareSerial SerialAT(2, 3); // RX, TX
  18. #include <TinyGsmClient.h>
  19. TinyGsmClient gsm(SerialAT);
  20. void setup() {
  21. // Set console baud rate
  22. SerialMonitor.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. SerialMonitor.println("Autodetecting baud rate");
  30. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  31. SerialMonitor.print(String("Trying baud rate ") + rates[i] + "... ");
  32. SerialAT.begin(rates[i]);
  33. delay(10);
  34. if (gsm.autoBaud(3000)) {
  35. rate = rates[i];
  36. SerialMonitor.println(F("OK"));
  37. break;
  38. } else {
  39. SerialMonitor.println(F("fail"));
  40. }
  41. }
  42. if (!rate) {
  43. SerialMonitor.println(F("***********************************************************"));
  44. SerialMonitor.println(F(" Module does not respond!"));
  45. SerialMonitor.println(F(" Check your Serial wiring"));
  46. SerialMonitor.println(F(" Check the module is correctly powered and turned on"));
  47. SerialMonitor.println(F("***********************************************************"));
  48. delay(30000L);
  49. return;
  50. }
  51. // Access AT commands from Serial Monitor
  52. SerialMonitor.println(F("***********************************************************"));
  53. SerialMonitor.println(F(" You can now send AT commands"));
  54. SerialMonitor.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
  55. SerialMonitor.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
  56. SerialMonitor.println(F("***********************************************************"));
  57. while(true) {
  58. if (SerialAT.available()) {
  59. SerialMonitor.write(SerialAT.read());
  60. }
  61. if (SerialMonitor.available()) {
  62. SerialAT.write(SerialMonitor.read());
  63. }
  64. delay(0);
  65. }
  66. }