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.

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