Advertisement
mikroavr

http_post_json_sim800_sim7600

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