Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // my whatsapp: +6282165062273
- //tutorial get chat id group: https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
- #define TINY_GSM_MODEM_SIM7600
- #if !defined(TINY_GSM_RX_BUFFER)
- #define TINY_GSM_RX_BUFFER 650
- #endif
- #define TINY_GSM_DEBUG SerialMon
- #define GSM_PIN ""
- #define SerialMon Serial
- #define SerialAT Serial2
- #include <TinyGsmClient.h>
- TinyGsm modem(SerialAT);
- #define RXD2 26
- #define TXD2 27
- #define PKEY 14
- #define RST 12
- byte pin_led = 13;
- byte pin_wdt = 19;
- bool ledState = 0;
- char apn[] = "Internet";
- char gprsUser[] = "";
- char gprsPass[] = "";
- unsigned long cur_time_at, old_time_at ;
- String str_token = "6959418433:BBES5hhzLL-OVX53DKBYBRbvHjl9OEGXKeU"; // token bot telegram
- String str_chat_id = "-4056201879"; // chat id group telegram
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
- pinMode(pin_wdt, OUTPUT);
- pinMode(pin_led, OUTPUT);
- pinMode(RST, OUTPUT);
- pinMode(PKEY, OUTPUT);
- delay(1000);
- Serial.println("reset sim");
- rst_sim();
- Serial.println("list cert");
- send_at("AT+CCERTLIST");
- }
- void loop() {
- ledState = !ledState;
- digitalWrite(pin_led, ledState);
- digitalWrite(pin_wdt, ledState);
- push_telegram(str_token, str_chat_id, "dariSim7600");
- delay(3000);
- }
- void push_telegram(String _token, String _chat_id, String _txt_message) {
- if (koneksi()) {
- Serial.println("gsm ok");
- String _url = "GET /bot" + _token + "/sendMessage?";
- _url += "chat_id=" + _chat_id + "&text=" + _txt_message + " HTTP/1.1";
- _url += "\r\n";
- _url += "Host: api.telegram.org\r\n";
- _url += "Content-Type: text/plain\r\n";
- _url += "\r\n";
- Serial.print("url: ");
- Serial.println(_url);
- res_cmd("AT+CSSLCFG=\"sslversion\",0,4", "OK", 1000); Serial.println("1");
- res_cmd("AT+CSSLCFG=\"authmode\",0,1", "OK", 1000); Serial.println("2");
- res_cmd("AT+CSSLCFG=\"cacert\",0,\"ca_cert.pem\"", "OK", 1000); Serial.println("3");
- res_cmd("AT+CCHSET=1", "OK", 1000); Serial.println("4");
- res_cmd("AT+CCHSTART", "OK", 1000); Serial.println("5");
- res_cmd("AT+CCHSSLCFG=0,0", "OK", 1000); Serial.println("6");
- res_cmd("AT+CCHOPEN=0,\"api.telegram.org\",443,2", "+CCHOPEN: 0,0", 30000);
- char buf_at[40];
- String str_at = "";
- str_at = "AT+CCHSEND=0," + String(_url.length());
- str_at.toCharArray(buf_at, str_at.length() + 1);
- send_at(buf_at);
- SerialAT.println(_url);
- wRespon(5000);
- res_cmd("AT+CCHCLOSE=0", "OK", 1000);
- res_cmd("AT+CCHSTOP", "OK", 1000);
- }
- else {
- Serial.println("gsm not ok");
- }
- }
- bool koneksi() {
- bool sts_koneksi;
- SerialMon.print(F("Waiting for network..."));
- int i = 0;
- while (!modem.waitForNetwork()) {
- SerialMon.println(" fail");
- delay(1000); i++;
- if (i == 1)break;
- }
- i = 0;
- SerialMon.println(F(" success"));
- while (!modem.isNetworkConnected()) {
- SerialMon.println(F("Network fail"));
- delay(1000); i++;
- if (i == 1)break;
- }
- SerialMon.println(F("Network connected"));
- i = 0;
- // GPRS connection parameters are usually set after network registration
- SerialMon.print(F("Connecting to "));
- SerialMon.print(apn);
- while (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
- SerialMon.println(" fail");
- delay(1000); i++;
- if (i == 1)break;
- }
- i = 0;
- SerialMon.println(F(" success"));
- if (modem.isGprsConnected()) {
- SerialMon.println(F("GPRS connected"));
- sts_koneksi = true;
- } else {
- sts_koneksi = false;
- }
- Serial.print("status koneksi: ");
- Serial.println(sts_koneksi);
- return sts_koneksi;
- }
- void send_at(char *_command) {
- SerialAT.println(_command);
- wRespon(500);
- }
- void wRespon(long waktu) {
- cur_time_at = millis();
- old_time_at = cur_time_at;
- while (cur_time_at - old_time_at < waktu ) {
- cur_time_at = millis();
- while (SerialAT.available() > 0) {
- Serial.print(SerialAT.readString());
- }
- }
- }
- void rst_sim() {
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- delay(1000);
- digitalWrite(PKEY, HIGH);
- digitalWrite(RST, HIGH);
- delay(1000);
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- Serial.println("Initializing modem...");
- modem.restart();
- String modemInfo = modem.getModemInfo();
- Serial.print("Modem Info: ");
- Serial.println(modemInfo);
- }
- int8_t res_cmd(char* ATcommand, char* expected_answer, unsigned int timeout) {
- uint8_t x = 0, answer = 0;
- char response[100];
- unsigned long prevMillis;
- memset(response, '\0', 100);
- delay(100);
- while (SerialAT.available() > 0)SerialAT.read();
- SerialAT.println(ATcommand);
- prevMillis = millis();
- do {
- if (SerialAT.available() != 0) {
- response[x] = SerialAT.read();
- x++;
- if (strstr(response, expected_answer) != NULL) {
- answer = 1;
- }
- }
- } while ((answer == 0) && ((millis() - prevMillis) < timeout));
- return answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement