Advertisement
mikroavr

telegram_bot_sim7600

Dec 2nd, 2023 (edited)
1,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // my whatsapp: +6282165062273
  2. //tutorial get chat id group: https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
  3.  
  4.  
  5. #define TINY_GSM_MODEM_SIM7600
  6.  
  7. #if !defined(TINY_GSM_RX_BUFFER)
  8. #define TINY_GSM_RX_BUFFER 650
  9. #endif
  10. #define TINY_GSM_DEBUG SerialMon
  11. #define GSM_PIN ""
  12. #define SerialMon Serial
  13. #define SerialAT Serial2
  14.  
  15. #include <TinyGsmClient.h>
  16. TinyGsm modem(SerialAT);
  17.  
  18. #define RXD2 26
  19. #define TXD2 27
  20.  
  21. #define PKEY 14
  22. #define RST 12
  23. byte pin_led = 13;
  24. byte pin_wdt = 19;
  25. bool ledState = 0;
  26.  
  27. char apn[] = "Internet";
  28. char gprsUser[] = "";
  29. char gprsPass[] = "";
  30.  
  31. unsigned long cur_time_at, old_time_at ;
  32.  
  33. String str_token = "6959418433:BBES5hhzLL-OVX53DKBYBRbvHjl9OEGXKeU"; // token bot telegram
  34. String str_chat_id = "-4056201879"; // chat id group telegram
  35.  
  36. void setup() {
  37.   // put your setup code here, to run once:
  38.   Serial.begin(115200);
  39.   SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
  40.   pinMode(pin_wdt, OUTPUT);
  41.   pinMode(pin_led, OUTPUT);
  42.   pinMode(RST, OUTPUT);
  43.   pinMode(PKEY, OUTPUT);
  44.   delay(1000);
  45.  
  46.   Serial.println("reset sim");
  47.   rst_sim();
  48.   Serial.println("list cert");
  49.   send_at("AT+CCERTLIST");
  50.  
  51. }
  52.  
  53. void loop() {
  54.   ledState = !ledState;
  55.   digitalWrite(pin_led, ledState);
  56.   digitalWrite(pin_wdt, ledState);
  57.   push_telegram(str_token, str_chat_id, "dariSim7600");
  58.   delay(3000);
  59. }
  60.  
  61. void push_telegram(String _token, String _chat_id, String _txt_message) {
  62.   if (koneksi()) {
  63.     Serial.println("gsm ok");
  64.     String _url = "GET /bot" + _token + "/sendMessage?";
  65.     _url += "chat_id=" + _chat_id + "&text=" + _txt_message + " HTTP/1.1";
  66.     _url += "\r\n";
  67.     _url += "Host: api.telegram.org\r\n";
  68.     _url += "Content-Type: text/plain\r\n";
  69.     _url += "\r\n";
  70.     Serial.print("url: ");
  71.     Serial.println(_url);
  72.  
  73.     res_cmd("AT+CSSLCFG=\"sslversion\",0,4", "OK", 1000); Serial.println("1");
  74.     res_cmd("AT+CSSLCFG=\"authmode\",0,1", "OK", 1000); Serial.println("2");
  75.     res_cmd("AT+CSSLCFG=\"cacert\",0,\"ca_cert.pem\"", "OK", 1000); Serial.println("3");
  76.     res_cmd("AT+CCHSET=1", "OK", 1000); Serial.println("4");
  77.     res_cmd("AT+CCHSTART", "OK", 1000); Serial.println("5");
  78.     res_cmd("AT+CCHSSLCFG=0,0", "OK", 1000); Serial.println("6");
  79.     res_cmd("AT+CCHOPEN=0,\"api.telegram.org\",443,2", "+CCHOPEN: 0,0", 30000);
  80.     char buf_at[40];
  81.     String str_at = "";
  82.     str_at = "AT+CCHSEND=0," + String(_url.length());
  83.     str_at.toCharArray(buf_at, str_at.length() + 1);
  84.     send_at(buf_at);
  85.     SerialAT.println(_url);
  86.     wRespon(5000);
  87.     res_cmd("AT+CCHCLOSE=0", "OK", 1000);
  88.     res_cmd("AT+CCHSTOP", "OK", 1000);
  89.   }
  90.   else {
  91.     Serial.println("gsm not ok");
  92.   }
  93. }
  94.  
  95.  
  96.  
  97. bool koneksi() {
  98.   bool sts_koneksi;
  99.   SerialMon.print(F("Waiting for network..."));
  100.   int i = 0;
  101.   while (!modem.waitForNetwork()) {
  102.     SerialMon.println(" fail");
  103.     delay(1000); i++;
  104.     if (i == 1)break;
  105.   }
  106.   i = 0;
  107.   SerialMon.println(F(" success"));
  108.   while (!modem.isNetworkConnected()) {
  109.     SerialMon.println(F("Network fail"));
  110.     delay(1000); i++;
  111.     if (i == 1)break;
  112.   }
  113.   SerialMon.println(F("Network connected"));
  114.  
  115.   i = 0;
  116.   // GPRS connection parameters are usually set after network registration
  117.  
  118.   SerialMon.print(F("Connecting to "));
  119.   SerialMon.print(apn);
  120.   while (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  121.     SerialMon.println(" fail");
  122.     delay(1000); i++;
  123.     if (i == 1)break;
  124.   }
  125.  
  126.   i = 0;
  127.   SerialMon.println(F(" success"));
  128.   if (modem.isGprsConnected()) {
  129.     SerialMon.println(F("GPRS connected"));
  130.     sts_koneksi = true;
  131.   } else {
  132.     sts_koneksi = false;
  133.   }
  134.   Serial.print("status koneksi: ");
  135.   Serial.println(sts_koneksi);
  136.   return sts_koneksi;
  137. }
  138.  
  139. void send_at(char *_command) {
  140.   SerialAT.println(_command);
  141.   wRespon(500);
  142. }
  143.  
  144. void wRespon(long waktu) {
  145.   cur_time_at = millis();
  146.   old_time_at = cur_time_at;
  147.   while (cur_time_at - old_time_at < waktu ) {
  148.     cur_time_at = millis();
  149.     while (SerialAT.available() > 0) {
  150.       Serial.print(SerialAT.readString());
  151.     }
  152.   }
  153. }
  154.  
  155. void rst_sim() {
  156.   digitalWrite(PKEY, LOW);
  157.   digitalWrite(RST, LOW);
  158.   delay(1000);
  159.   digitalWrite(PKEY, HIGH);
  160.   digitalWrite(RST, HIGH);
  161.   delay(1000);
  162.   digitalWrite(PKEY, LOW);
  163.   digitalWrite(RST, LOW);
  164.   Serial.println("Initializing modem...");
  165.   modem.restart();
  166.   String modemInfo = modem.getModemInfo();
  167.   Serial.print("Modem Info: ");
  168.   Serial.println(modemInfo);
  169. }
  170.  
  171. int8_t res_cmd(char* ATcommand, char* expected_answer, unsigned int timeout) {
  172.   uint8_t x = 0, answer = 0;
  173.   char response[100];
  174.   unsigned long prevMillis;
  175.   memset(response, '\0', 100);
  176.   delay(100);
  177.  
  178.   while (SerialAT.available() > 0)SerialAT.read();
  179.   SerialAT.println(ATcommand);
  180.  
  181.   prevMillis = millis();
  182.  
  183.   do {
  184.     if (SerialAT.available() != 0) {
  185.       response[x] = SerialAT.read();
  186.       x++;
  187.       if (strstr(response, expected_answer) != NULL) {
  188.         answer = 1;
  189.       }
  190.     }
  191.   } while ((answer == 0) && ((millis() - prevMillis) < timeout));
  192.   return answer;
  193. }
  194.  
  195.  
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement