Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned long nMillis, oMillis;
- char buf[100];
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial3.begin(4800);
- pinMode(39, OUTPUT);
- rstModem();
- openConnect();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- nMillis = millis();
- if ( nMillis - oMillis >= 3000){
- pushData();
- oMillis = nMillis;
- }
- }
- String waitRespon(){
- String txt;
- while (!Serial3.available()){}
- while (Serial3.available()) {
- txt = Serial3.readString();
- }
- return txt;
- }
- bool replayOK(){
- String txt; int len;
- bool i;
- while(!Serial3.available()){}
- while (Serial3.available()){
- txt = Serial3.readString();
- }
- len = txt.length();
- //Serial.print("len: ");
- //Serial.println(len);
- txt.toCharArray(buf, len);
- txt = "";
- txt = String(buf[len-4]) + buf[len-3];
- if ( txt == "OK" ){
- i = true;
- }else{
- i = false;
- }
- return i;
- }
- void rstModem(){
- int i;
- char dt[8];
- digitalWrite(39, HIGH);
- delay(1000);
- digitalWrite(39, LOW);
- delay(1000);
- digitalWrite(39, HIGH);
- while (!Serial3.available()){}
- nMillis = millis();
- oMillis = nMillis;
- while ( nMillis - oMillis < 15000){
- nMillis = millis();
- while(Serial3.available()){
- Serial.println(Serial3.readString());
- }
- }
- Serial.println(F("Restart Sukses"));
- oMillis = nMillis;
- }
- bool reg(){
- bool sts;
- String txt; int len;
- int dt, i;
- Serial3.println("AT+CFUN?");
- txt = waitRespon();
- len = txt.length();
- txt.toCharArray(buf,len);
- for ( i = 0; i < len; i++){
- //Serial.print(i);
- //Serial.println(buf[i]);
- if( buf[i] == ':'){
- break;
- }
- }
- txt = "";
- txt = String(buf[i+2]);
- dt = txt.toInt();
- if ( dt != 1){
- Serial3.println("AT+CFUN=1");
- if (!replayOK){
- delay(5000);
- return;
- }
- }
- if ( dt == 1 ){
- sts = true;
- }else{
- sts = false;
- }
- return sts;
- }
- void inSSL(){
- String txt; bool i;
- Serial3.println("AT+CGPSSSL=1");
- wRespon(2000);
- Serial.println("SSL");
- delay(1000);
- Serial3.println("AT+CCERTLIST");
- wRespon(2000);
- }
- void openConnect(){
- delay(500);
- Serial3.println("AT+CFUN?");
- wRespon(1000);
- delay(500);
- Serial3.println("AT+CREG?");
- wRespon(1000);
- delay(500);
- Serial3.println("AT+CGATT=1");
- wRespon(1000);
- delay(500);
- Serial3.println("AT+CGSOCKCONT=1,\"IP\",\"Internet\"");
- wRespon(1000);
- delay(1000);
- Serial3.println("AT+CPSI?");
- wRespon(5000);
- }
- void pushData(){
- int len; String txt;
- String url = "/pln/i.php?id=01&tgl=2019/3/29&jam=5:17:10";
- url += "&van=223&vbn=112&vcn=111&vab=111&vac=0";
- url += "&vbc=111&ca=0&cb=0&cc=0&cn=0&lu=0&kwh=0";
- url += "&f=50&t=29&h=69&r1=1&s1=1&t1=1&r2=1";
- url += "&s2=1&t2=1&r3=1&s3=1&t3=1&r4=1&s4=1&t4=1\r";
- len = url.length();
- Serial3.println("AT+CHTTPACT=\"example.com\",80");
- wRespon(5000);
- Serial3.print("GET http://example.com" + url + " HTTP/1.0\r\n");
- Serial3.print("Host: example.com\r\n");
- Serial3.print("Connection: close\r\n\r\n");
- Serial3.print(char(0x1A));
- wRespon(15000);
- //Serial3.print(F("POST http://example.com/pln/i.php? HTTP/1.1\r\n"));
- //Serial3.print(F("Host: example.com\r\n"));
- //Serial3.print(F("User-Agent: MY WEB AGENT\r\n"));
- //Serial3.print(F("Accept: */*\r\n"));
- //Serial3.print(F("Content-Type: application/x-www-form-urlencoded\r\n"));
- //Serial3.print(F("Cache-Control: no-cache\r\n"));
- //Serial3.print(F("Accept-Charset: utf-8, us-ascii\r\n"));
- //Serial3.print(F("Pragma: no-cache\r\n"));
- //Serial3.print(String("Content-Length: ") + len + "\r\n");
- //Serial3.println(url);
- //Serial3.print(char(0x1A));
- wRespon(15000);
- }
- void wRespon(unsigned long waktu){
- nMillis = millis();
- oMillis = nMillis;
- while ( nMillis - oMillis < waktu){
- nMillis = millis();
- while(Serial3.available()){
- Serial.println(Serial3.readString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement