Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gprs.h>
- #include <softwareserial.h>
- /*koneksi sim800 dengan Arduino
- SIM800 TX is connected to Arduino D8
- SIM800 RX is connected to Arduino D7
- */
- #define TIMEOUT 5000
- #define RELAY_PIN 0 // relay pin di hubungkan ke D0
- bool relayStatus;
- GPRS gprs;
- void setup() {
- Serial.begin(9600);
- while (!Serial);
- Serial.println("Starting SIM800 SMS Command Processor");
- gprs.preInit();
- delay(1000);
- while (0 != gprs.init()) {
- delay(1000);
- Serial.print("init error\r\n");
- }
- //Set SMS mode ke mode ASCII
- if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
- ERROR("ERROR:CNMI");
- return;
- }
- //mulai baca ada indikasi sms baru
- if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
- ERROR("ERROR:CNMI");
- return;
- }
- Serial.println("Init success");
- }
- //Variable penyimpanan sms
- char currentLine[500] = "";
- int currentLineIndex = 0;
- bool nextLineIsMessage = false;
- void loop() {
- //kontrol status relay di sini
- digitalWrite(RELAY_PIN, relayStatus);
- //jika ada data serial terbaca
- if (gprs.serialSIM800.available()) {
- char lastCharRead = gprs.serialSIM800.read();
- //baca karakter dari serial
- if (lastCharRead == '\r' || lastCharRead == '\n') {
- String lastLine = String(currentLine);
- //jika baris terakhir adalah +CMT, ada indikasi sms baru masuk.
- if (lastLine.startsWith("+CMT:")) {
- Serial.println(lastLine);
- nextLineIsMessage = true;
- } else if (lastLine.length() > 0) {
- if (nextLineIsMessage) {
- Serial.println(lastLine);
- //disini tempat pembacaan sms oleh sim800, kontrol sms ada disini
- if (lastLine.indexOf("RELAY ON") >= 0) {
- relayStatus = 1;
- } else if (lastLine.indexOf("RELAY OFF") >= 0) {
- relayStatus = 0;
- }
- nextLineIsMessage = false;
- }
- }
- //Clear char array for next line of read
- for ( int i = 0; i < sizeof(currentLine); ++i ) {
- currentLine[i] = (char)0;
- }
- currentLineIndex = 0;
- } else {
- currentLine[currentLineIndex++] = lastCharRead;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement