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.

92 lines
2.7 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
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. // Select your modem:
  11. #define TINY_GSM_MODEM_SIM800
  12. // #define TINY_GSM_MODEM_SIM900
  13. // #define TINY_GSM_MODEM_A6
  14. // #define TINY_GSM_MODEM_A7
  15. // #define TINY_GSM_MODEM_M590
  16. // #define TINY_GSM_MODEM_ESP8266
  17. // #define TINY_GSM_MODEM_XBEE
  18. #include <TinyGsmClient.h>
  19. // Set serial for debug console (to the Serial Monitor, speed 115200)
  20. #define SerialMon Serial
  21. // Set serial for AT commands (to the module)
  22. // Use Hardware Serial on Mega, Leonardo, Micro
  23. #define SerialAT Serial1
  24. // or Software Serial on Uno, Nano
  25. //#include <SoftwareSerial.h>
  26. //SoftwareSerial SerialAT(2, 3); // RX, TX
  27. TinyGsm modem(SerialAT);
  28. // Module baud rate
  29. uint32_t rate = 0; // Set to 0 for Auto-Detect
  30. void setup() {
  31. // Set console baud rate
  32. SerialMon.begin(115200);
  33. delay(5000);
  34. }
  35. void loop() {
  36. if (!rate) {
  37. static uint32_t rates[] = { 115200, 9600, 57600, 19200, 38400, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 };
  38. SerialMon.println("Autodetecting baud rate");
  39. for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) {
  40. SerialMon.print(String("Trying baud rate ") + rates[i] + "... ");
  41. SerialAT.begin(rates[i]);
  42. delay(10);
  43. if (modem.autoBaud(1000)) {
  44. rate = rates[i];
  45. SerialMon.println(F("OK"));
  46. break;
  47. } else {
  48. SerialMon.println(F("fail"));
  49. }
  50. }
  51. }
  52. if (!rate) {
  53. SerialMon.println(F("***********************************************************"));
  54. SerialMon.println(F(" Module does not respond!"));
  55. SerialMon.println(F(" Check your Serial wiring"));
  56. SerialMon.println(F(" Check the module is correctly powered and turned on"));
  57. SerialMon.println(F("***********************************************************"));
  58. delay(30000L);
  59. return;
  60. }
  61. SerialAT.begin(rate);
  62. // Access AT commands from Serial Monitor
  63. SerialMon.println(F("***********************************************************"));
  64. SerialMon.println(F(" You can now send AT commands"));
  65. SerialMon.println(F(" Enter \"AT\" (without quotes), and you should see \"OK\""));
  66. SerialMon.println(F(" If it doesn't work, select \"Both NL & CR\" in Serial Monitor"));
  67. SerialMon.println(F("***********************************************************"));
  68. while(true) {
  69. if (SerialAT.available()) {
  70. SerialMon.write(SerialAT.read());
  71. }
  72. if (SerialMon.available()) {
  73. SerialAT.write(SerialMon.read());
  74. }
  75. delay(0);
  76. }
  77. }