Advertisement
mikroavr

http_post_json_sim7600

Aug 7th, 2023
1,068
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   #define RXD2 26
  3.   #define TXD2 27
  4.  
  5.   #define PKEY 12
  6.   #define RST 14
  7. */
  8.  
  9. #define RXD2 26
  10. #define TXD2 27
  11.  
  12. #define PKEY 13
  13. #define RST 14
  14.  
  15. #define SerialMon Serial
  16. #define SerialAT Serial2
  17.  
  18. int ledState = LOW;
  19. const int ledPin =  12;
  20. int counter = 0;
  21.  
  22. unsigned long cur_time, old_time;
  23. unsigned long cur_time_led, old_time_led;
  24. unsigned long cur_time_push, old_time_push;
  25. unsigned long cur_time_rst, old_time_rst;
  26.  
  27. const int time_push = 1; //push per detik
  28.  
  29. bool hold = 0;
  30.  
  31. #define TINY_GSM_MODEM_SIM7600
  32. #if !defined(TINY_GSM_RX_BUFFER)
  33. #define TINY_GSM_RX_BUFFER 650
  34. #endif
  35. #define TINY_GSM_DEBUG SerialMon
  36. // set GSM PIN, if any
  37. #define GSM_PIN ""
  38.  
  39. // Your GPRS credentials, if any
  40. const char apn[]      = "Internet";
  41. const char gprsUser[] = "";
  42. const char gprsPass[] = "";
  43.  
  44. const char server[]   = "dweet.io";
  45. const char resource[] = "/dweet/for/";
  46. const int  port       = 80;
  47.  
  48. #include <TinyGsmClient.h>
  49. #include <ArduinoHttpClient.h>
  50.  
  51. #ifdef DUMP_AT_COMMANDS
  52. #include <StreamDebugger.h>
  53. StreamDebugger debugger(SerialAT, SerialMon);
  54. TinyGsm        modem(debugger);
  55. #else
  56. TinyGsm        modem(SerialAT);
  57. #endif
  58.  
  59. TinyGsmClient client(modem);
  60. HttpClient    http(client, server, port);
  61.  
  62. #include <ArduinoJson.h>
  63. StaticJsonDocument<450> doc;
  64. String buf_message_push = "";
  65.  
  66. void setup() {
  67.   // put your setup code here, to run once:
  68.   SerialMon.begin(9600);
  69.   SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
  70.   delay(250);
  71.   SerialMon.println("test at mulai");
  72.   pinMode(ledPin, OUTPUT);
  73.   pinMode(RST, OUTPUT);
  74.   pinMode(PKEY, OUTPUT);
  75.  
  76.   digitalWrite(PKEY, LOW);
  77.   digitalWrite(RST, LOW);
  78.   delay(1000);
  79.   digitalWrite(PKEY, HIGH);
  80.   digitalWrite(RST, HIGH);
  81.   delay(1000);
  82.   digitalWrite(PKEY, LOW);
  83.   digitalWrite(RST, LOW);
  84.   delay(1000);
  85.   SerialMon.println("Initializing modem...");
  86.   modem.restart();
  87.   String modemInfo = modem.getModemInfo();
  88.   SerialMon.print("Modem Info: ");
  89.   SerialMon.println(modemInfo);
  90.   delay(1000);
  91.  
  92.   byte _retry = 0;
  93.   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  94.     SerialMon.println(" fail");
  95.     delay(5000);
  96.     _retry ++;
  97.     Serial.print("retry: ");
  98.     Serial.println(_retry);
  99.     return;
  100.   }
  101. }
  102.  
  103. void loop() {
  104.   // put your main code here, to run repeatedly:
  105.  
  106.   cur_time_led = millis();
  107.   if (cur_time_led - old_time_led >= 1000) {
  108.  
  109.     if (ledState == LOW) {
  110.       ledState = HIGH;
  111.     } else {
  112.       ledState = LOW;
  113.     }
  114.  
  115.     digitalWrite(ledPin, ledState);
  116.     old_time_led = cur_time_led;
  117.   }
  118.  
  119.   cur_time_push = millis() / 1000;
  120.   if (cur_time_push - old_time_push >= time_push) {
  121.     push_server();
  122.  
  123.     old_time_push = millis() / 1000;
  124.   }
  125.  
  126.   cur_time_rst = millis()/1000;
  127.   if (cur_time_rst - old_time_rst >= 43200) { // restart setiap 12 jam sekali
  128.     ESP.restart();
  129.     old_time_rst = millis() / 1000;
  130.   }
  131.  
  132. }
  133.  
  134. void send_at(char *_command) {
  135.   SerialAT.println(_command);
  136.   wRespon(1000);
  137. }
  138.  
  139. void wRespon(long waktu) {
  140.   cur_time = millis();
  141.   old_time = cur_time;
  142.   while (cur_time - old_time < waktu ) {
  143.     cur_time = millis();
  144.     while (SerialAT.available() > 0) {
  145.       SerialMon.print(SerialAT.readString());
  146.     }
  147.   }
  148. }
  149.  
  150. void get_sensor() {
  151.   int sensorX = 0;
  152.   int sensorY = 0;
  153.   String message_push = "";
  154.   String id_truck = "AB001";
  155.  
  156.   sensorX = random(10, 100);
  157.   sensorY = random(10, 100);
  158.  
  159.   doc["id"] = id_truck;
  160.   doc["X"] = sensorX;
  161.   doc["Y"] = sensorX;
  162.   serializeJson(doc, message_push);
  163.   Serial.print("payload: ");
  164.   Serial.println(message_push);
  165.   buf_message_push = message_push;
  166.   doc.clear();
  167.  
  168.   //
  169.  
  170.   Serial.print("payload: ");
  171.   Serial.println(buf_message_push);
  172.  
  173. }
  174.  
  175. void push_server() {
  176.   get_sensor();
  177.   Serial.println("push to server");
  178.  
  179.   byte _retry = 0;
  180.   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  181.     SerialMon.println(" fail");
  182.     delay(10000);
  183.     return;
  184.   }
  185.  
  186.   if (!client.connect(server, port)) {
  187.     SerialMon.println(" fail");
  188.     delay(10000);
  189.     return;
  190.   }
  191.  
  192.   client.println("POST /dweet/for/ HTTP/1.1");
  193.   client.println("Host: dweet.io");
  194.   client.println("Content-Type: application/json");
  195.   client.print("Content-Length: ");
  196.   client.println(buf_message_push.length());
  197.   client.println();
  198.   client.println(buf_message_push);
  199.   client.println();
  200.   delay(100);
  201.  
  202.   uint32_t timeout = millis();
  203.   while (client.connected() && millis() - timeout < 10000L) {
  204.     // Print available data
  205.     while (client.available()) {
  206.       char c = client.read();
  207.       SerialMon.print(c);
  208.  
  209.       // hapus comment ini jika ingin lebih cepat respon nya
  210.       if (c == 'O') {
  211.         timeout = 10000L;
  212.         break;
  213.       }
  214.  
  215.  
  216.       timeout = millis();
  217.     }
  218.   }
  219.   SerialMon.println();
  220.   client.stop();
  221.   SerialMon.println(F("Server disconnected"));
  222.   //modem.gprsDisconnect();
  223.  
  224. }
  225.  
  226.  
  227. void reset_sim() {
  228.   digitalWrite(RST, HIGH);
  229. }
  230.  
  231. void wakeup_sim() {
  232.   SerialMon.println("wakeup sim7600");
  233.   digitalWrite(PKEY, LOW);
  234.   digitalWrite(RST, LOW);
  235.   delay(1000);
  236.   digitalWrite(PKEY, HIGH);
  237.   digitalWrite(RST, HIGH);
  238.   delay(1000);
  239.   digitalWrite(PKEY, LOW);
  240.   digitalWrite(RST, LOW);
  241.   delay(1000);
  242.   wRespon(15000);
  243. }
  244.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement