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.

96 lines
2.3 KiB

6 years ago
  1. /**************************************************************
  2. *
  3. * This sketch uploads SSL certificates to the SIM8xx
  4. *
  5. * TinyGSM Getting Started guide:
  6. * https://tiny.cc/tinygsm-readme
  7. *
  8. **************************************************************/
  9. // This example is specific to SIM8xx
  10. #define TINY_GSM_MODEM_SIM800
  11. // Select your certificate:
  12. #include "DSTRootCAX3.h"
  13. //#include "DSTRootCAX3.der.h"
  14. //#include "COMODORSACertificationAuthority.h"
  15. // Select the file you want to write into
  16. // (the file is stored on the modem)
  17. #define CERT_FILE "C:\\USER\\CERT.CRT"
  18. #include <TinyGsmClient.h>
  19. // Set serial for debug console (to the Serial Monitor, speed 115200)
  20. #define SerialMon Serial
  21. // Use Hardware Serial for AT commands
  22. #define SerialAT Serial1
  23. // Uncomment this if you want to see all AT commands
  24. // #define DUMP_AT_COMMANDS
  25. #ifdef DUMP_AT_COMMANDS
  26. #include <StreamDebugger.h>
  27. StreamDebugger debugger(SerialAT, SerialMon);
  28. TinyGsm modem(debugger);
  29. #else
  30. TinyGsm modem(SerialAT);
  31. #endif
  32. void setup() {
  33. // Set console baud rate
  34. SerialMon.begin(115200);
  35. delay(10);
  36. // Set GSM module baud rate
  37. SerialAT.begin(115200);
  38. delay(6000);
  39. SerialMon.println(F("Initializing modem..."));
  40. modem.init();
  41. modem.sendAT(GF("+FSCREATE=" CERT_FILE));
  42. if (modem.waitResponse() != 1) return;
  43. const int cert_size = sizeof(cert);
  44. modem.sendAT(GF("+FSWRITE=" CERT_FILE ",0,"), cert_size, GF(",10"));
  45. if (modem.waitResponse(GF(">")) != 1) {
  46. return;
  47. }
  48. for (int i = 0; i < cert_size; i++) {
  49. char c = pgm_read_byte(&cert[i]);
  50. modem.stream.write(c);
  51. }
  52. modem.stream.write(GSM_NL);
  53. modem.stream.flush();
  54. if (modem.waitResponse(2000) != 1) return;
  55. modem.sendAT(GF("+SSLSETCERT=\"" CERT_FILE "\""));
  56. if (modem.waitResponse() != 1) return;
  57. if (modem.waitResponse(5000L, GF(GSM_NL "+SSLSETCERT:")) != 1) return;
  58. const int retCode = modem.stream.readStringUntil('\n').toInt();
  59. SerialMon.println();
  60. SerialMon.println();
  61. SerialMon.println(F("****************************"));
  62. SerialMon.print(F("Setting Certificate: "));
  63. SerialMon.println((0 == retCode) ? "OK" : "FAILED");
  64. SerialMon.println(F("****************************"));
  65. }
  66. void loop() {
  67. if (SerialAT.available()) {
  68. SerialMon.write(SerialAT.read());
  69. }
  70. if (SerialMon.available()) {
  71. SerialAT.write(SerialMon.read());
  72. }
  73. delay(0);
  74. }