Advertisement
mikroavr

test_at_atmega2560

Aug 12th, 2022
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #define PKEY 7
  3. #define RST 6
  4.  
  5. #define SerialMon Serial
  6. #define SerialAT Serial3
  7.  
  8. int ledState = LOW;
  9. const int ledPin =  12;
  10. int counter = 0;
  11.  
  12. unsigned long cur_time_led, old_time_led;
  13. unsigned long cur_time, old_time;
  14. bool hold = 0;
  15.  
  16. #define TINY_GSM_MODEM_SIM7600
  17. #if !defined(TINY_GSM_RX_BUFFER)
  18. #define TINY_GSM_RX_BUFFER 650
  19. #endif
  20. #define TINY_GSM_DEBUG SerialMon
  21. // set GSM PIN, if any
  22. #define GSM_PIN ""
  23.  
  24. // Your GPRS credentials, if any
  25. const char apn[]      = "Internet";
  26. const char gprsUser[] = "";
  27. const char gprsPass[] = "";
  28.  
  29. const char server[]   = "vsh.pp.ua";
  30. const char resource[] = "/TinyGSM/logo.txt";
  31. const int  port       = 80;
  32.  
  33. #include <TinyGsmClient.h>
  34. #include <ArduinoHttpClient.h>
  35.  
  36. #ifdef DUMP_AT_COMMANDS
  37. #include <StreamDebugger.h>
  38. StreamDebugger debugger(SerialAT, SerialMon);
  39. TinyGsm        modem(debugger);
  40. #else
  41. TinyGsm        modem(SerialAT);
  42. #endif
  43.  
  44. TinyGsmClient client(modem);
  45. HttpClient    http(client, server, port);
  46.  
  47.  
  48. void setup() {
  49.   // put your setup code here, to run once:
  50.   SerialMon.begin(115200);
  51.   SerialAT.begin(115200);
  52.   delay(250);
  53.   SerialMon.println("test at mulai");
  54.   pinMode(ledPin, OUTPUT);
  55.   pinMode(RST, OUTPUT);
  56.   pinMode(PKEY, OUTPUT);
  57.  
  58.   digitalWrite(PKEY, LOW);
  59.   digitalWrite(RST, LOW);
  60.   delay(1000);
  61.   digitalWrite(PKEY, HIGH);
  62.   digitalWrite(RST, HIGH);
  63.   delay(1000);
  64.   digitalWrite(PKEY, LOW);
  65.   digitalWrite(RST, LOW);
  66.   delay(1000);
  67.   SerialMon.println("Initializing modem...");
  68.   modem.restart();
  69.   String modemInfo = modem.getModemInfo();
  70.   SerialMon.print("Modem Info: ");
  71.   SerialMon.println(modemInfo);
  72.   delay(1000);
  73.   /*
  74.   send_at("AT+CMGF=1");
  75.   send_at("AT+IPREX=9600");
  76.   SerialAT.begin(9600, SERIAL_8N1, RXD2, TXD2);
  77.   send_at("AT&W");
  78.   send_at("AT&V");
  79.   */
  80. }
  81.  
  82. void loop() {
  83.   // put your main code here, to run repeatedly:
  84.  
  85.   cur_time_led = millis();
  86.   if (cur_time_led - old_time_led >= 1000) {
  87.     SerialMon.println("SEND AT");
  88.     counter++;
  89.     /*bool pin_sts =   res_cmd("AT+CPIN?", "+CPIN: READY", 1000);
  90.     Serial.print("sts pin: ");
  91.     Serial.println(pin_sts);
  92.     if(pin_sts == 0){
  93.       reset_sim();
  94.       wakeup_sim();
  95.     }
  96.     */
  97.     switch (counter) {
  98.       case 1:
  99.         send_at("AT+CPIN?");
  100.         break;
  101.       case 2:
  102.         send_at("AT+CSQ");
  103.         break;
  104.       case 3:
  105.         send_at("AT+CPSI?");
  106.         break;
  107.       case 4:
  108.         send_at("AT+IPR?");
  109.         counter = 0;
  110.         break;
  111.     }
  112.  
  113.     if (ledState == LOW) {
  114.       ledState = HIGH;
  115.     } else {
  116.       ledState = LOW;
  117.     }
  118.  
  119.     digitalWrite(ledPin, ledState);
  120.     old_time_led = cur_time_led;
  121.   }
  122. }
  123.  
  124. void send_at(char *_command) {
  125.   SerialAT.println(_command);
  126.   wRespon(1000);
  127. }
  128.  
  129. void wRespon(long waktu) {
  130.   cur_time = millis();
  131.   old_time = cur_time;
  132.   while (cur_time - old_time < waktu ) {
  133.     cur_time = millis();
  134.     while (SerialAT.available() > 0) {
  135.       SerialMon.print(SerialAT.readString());
  136.     }
  137.   }
  138. }
  139.  
  140.  
  141. void reset_sim() {
  142.   digitalWrite(RST, HIGH);
  143. }
  144.  
  145. void wakeup_sim() {
  146.   SerialMon.println("wakeup sim7600");
  147.   digitalWrite(PKEY, LOW);
  148.   digitalWrite(RST, LOW);
  149.   delay(1000);
  150.   digitalWrite(PKEY, HIGH);
  151.   digitalWrite(RST, HIGH);
  152.   delay(1000);
  153.   digitalWrite(PKEY, LOW);
  154.   digitalWrite(RST, LOW);
  155.   delay(1000);
  156.   wRespon(15000);
  157. }
  158.  
  159. int8_t res_cmd(char* ATcommand, char* expected_answer, unsigned int timeout) {
  160.   uint8_t x = 0, answer = 0;
  161.   char response[100];
  162.   unsigned long prevMillis;
  163.   memset(response, '\0', 100);
  164.   delay(100);
  165.  
  166.   while (SerialAT.available() > 0)SerialAT.read();
  167.   SerialAT.println(ATcommand);
  168.  
  169.   prevMillis = millis();
  170.  
  171.   do {
  172.     if (SerialAT.available() != 0) {
  173.       response[x] = SerialAT.read();
  174.       x++;
  175.       if (strstr(response, expected_answer) != NULL) {
  176.         answer = 1;
  177.       }
  178.     }
  179.   } while ((answer == 0) && ((millis() - prevMillis) < timeout));
  180.   return answer;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement