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