Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- #define RXD2 26
- #define TXD2 27
- #define PKEY 12
- #define RST 14
- */
- #define RXD2 26
- #define TXD2 27
- #define PKEY 14
- #define RST 12
- #define SerialMon Serial
- #define SerialAT Serial2
- int ledState = LOW;
- const int ledPin = 13;
- int counter = 0;
- unsigned long cur_time, old_time;
- unsigned long cur_time_led, old_time_led;
- unsigned long cur_time_push, old_time_push;
- unsigned long cur_time_rst, old_time_rst;
- const int time_push = 1; //push per detik
- bool hold = 0;
- //pilih jenis modem
- //#define TINY_GSM_MODEM_SIM7600
- #define TINY_GSM_MODEM_SIM800
- #if !defined(TINY_GSM_RX_BUFFER)
- #define TINY_GSM_RX_BUFFER 650
- #endif
- #define TINY_GSM_DEBUG SerialMon
- // set GSM PIN, if any
- #define GSM_PIN ""
- // Your GPRS credentials, if any
- const char apn[] = "Internet";
- const char gprsUser[] = "";
- const char gprsPass[] = "";
- const char server[] = "dweet.io";
- const char resource[] = "/dweet/for/";
- const int port = 80;
- #include <TinyGsmClient.h>
- #include <ArduinoHttpClient.h>
- #ifdef DUMP_AT_COMMANDS
- #include <StreamDebugger.h>
- StreamDebugger debugger(SerialAT, SerialMon);
- TinyGsm modem(debugger);
- #else
- TinyGsm modem(SerialAT);
- #endif
- TinyGsmClient client(modem);
- HttpClient http(client, server, port);
- #include <ArduinoJson.h>
- StaticJsonDocument<450> doc;
- String buf_message_push = "";
- void setup() {
- // put your setup code here, to run once:
- SerialMon.begin(9600);
- SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
- delay(250);
- SerialMon.println("test at mulai");
- pinMode(ledPin, OUTPUT);
- pinMode(RST, OUTPUT);
- pinMode(PKEY, OUTPUT);
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- delay(1000);
- digitalWrite(PKEY, HIGH);
- digitalWrite(RST, HIGH);
- delay(1000);
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- delay(1000);
- SerialMon.println("Initializing modem...");
- modem.restart();
- String modemInfo = modem.getModemInfo();
- SerialMon.print("Modem Info: ");
- SerialMon.println(modemInfo);
- delay(1000);
- byte _retry = 0;
- if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
- SerialMon.println(" fail");
- delay(5000);
- _retry ++;
- Serial.print("retry: ");
- Serial.println(_retry);
- return;
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- cur_time_led = millis();
- if (cur_time_led - old_time_led >= 1000) {
- if (ledState == LOW) {
- ledState = HIGH;
- } else {
- ledState = LOW;
- }
- digitalWrite(ledPin, ledState);
- old_time_led = cur_time_led;
- }
- cur_time_push = millis() / 1000;
- if (cur_time_push - old_time_push >= time_push) {
- push_server();
- old_time_push = millis() / 1000;
- }
- cur_time_rst = millis()/1000;
- if (cur_time_rst - old_time_rst >= 43200) { // restart setiap 12 jam sekali
- ESP.restart();
- old_time_rst = millis() / 1000;
- }
- }
- void send_at(char *_command) {
- SerialAT.println(_command);
- wRespon(1000);
- }
- void wRespon(long waktu) {
- cur_time = millis();
- old_time = cur_time;
- while (cur_time - old_time < waktu ) {
- cur_time = millis();
- while (SerialAT.available() > 0) {
- SerialMon.print(SerialAT.readString());
- }
- }
- }
- void get_sensor() {
- int sensorX = 0;
- int sensorY = 0;
- String message_push = "";
- String id_truck = "AB001";
- sensorX = random(10, 100);
- sensorY = random(10, 100);
- doc["id"] = id_truck;
- doc["X"] = sensorX;
- doc["Y"] = sensorX;
- serializeJson(doc, message_push);
- Serial.print("payload: ");
- Serial.println(message_push);
- buf_message_push = message_push;
- doc.clear();
- //
- Serial.print("payload: ");
- Serial.println(buf_message_push);
- }
- void push_server() {
- get_sensor();
- Serial.println("push to server");
- byte _retry = 0;
- if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
- SerialMon.println(" fail");
- delay(10000);
- return;
- }
- if (!client.connect(server, port)) {
- SerialMon.println(" fail");
- delay(10000);
- return;
- }
- client.println("POST /dweet/for/ HTTP/1.1");
- client.println("Host: dweet.io");
- client.println("Content-Type: application/json");
- client.print("Content-Length: ");
- client.println(buf_message_push.length());
- client.println();
- client.println(buf_message_push);
- client.println();
- delay(100);
- uint32_t timeout = millis();
- while (client.connected() && millis() - timeout < 10000L) {
- // Print available data
- while (client.available()) {
- char c = client.read();
- SerialMon.print(c);
- // hapus comment ini jika ingin lebih cepat respon nya
- if (c == 'O') {
- timeout = 10000L;
- break;
- }
- timeout = millis();
- }
- }
- SerialMon.println();
- client.stop();
- SerialMon.println(F("Server disconnected"));
- modem.gprsDisconnect();
- }
- void reset_sim() {
- digitalWrite(RST, HIGH);
- }
- void wakeup_sim() {
- SerialMon.println("wakeup sim7600");
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- delay(1000);
- digitalWrite(PKEY, HIGH);
- digitalWrite(RST, HIGH);
- delay(1000);
- digitalWrite(PKEY, LOW);
- digitalWrite(RST, LOW);
- delay(1000);
- wRespon(15000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement