Hi all! My hardware is Arduino Uno R3 and GSM shield based on SIM900 by keyestudio.
My goal is minimising payment for internet connection on GSM shield side. What I am trying to do in that case is: turn ON and OFF internet connection by SMS. (So, I send SMS - ON and after that blynk connect to server and I can manage it from the app. When SMS is OFF - shield disconnect blynk (or GPRS connection at all) )
So, the question is:
- Blynk.disconnect() - this method minimise gprs traffic or I have to turn off internet by AT-command ( AT+SAPBR=0,1)?
- How rightly turn back on blynk after receiving proper SMS?
Maybe there is any simple way for achieving this goal?
Thanks!
This is my code:
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM900
#include <GPRSk_Shield_Arduino.h>
#define pinBOOT 9 // нога BOOT или K на модеме M590, на SIM900 это 9
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <DHT.h>
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(3,2); // RX, TX
SoftwareSerial mySerial(3,2);
GPRSk gprs(mySerial);
TinyGsm modem(SerialAT);
// Ваш токен
char auth[] = "0d0e0b22eee549**********************";
char apn[] = "internet";
char user[] = "";
char pass[] = "";
//Method for DHT11
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, h);
Blynk.virtualWrite(V4, t);
}
void setup()
{
///////NOW CONFIGURING BLYNK AND GSM MODULE
delay(3000);
gprs.powerOn();
Serial.begin(9600);
delay(10);
// Set GSM module baud rate
SerialAT.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
delay(5000);
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
Blynk.begin(auth, modem, apn, user, pass);
dht.begin();
timer.setInterval(1000L, sendSensor);
////////////////////NOW CONFIGURING RECEIVE/RESPONSE SMS MESSAGES
mySerial.print("AT+CMGF=1\r");
delay(300);
mySerial.print("AT+IFC=1, 1\r");
delay(300);
mySerial.print("AT+CPBS=\"SM\"\r");
delay(300);
mySerial.print("AT+CNMI=1,2,2,1,0\r");
delay(500);
}
void loop()
{
//////This code handle SMS
if (!gprsSerial.available())
return;
char currSymb = gprsSerial.read();
if ('\r' == currSymb) {
if (isStringMessage) {
////If we have sms like "InternetOn", I want to run blynk
if (!currStr.compareTo("InternetOn")) {
///WILL THIS WORK AFTER RECEIVING SMS?
Blynk.run();
timer.run();
Blynk.connect();
////////////////////////////////////////
delay(5000);
mySerial.println("AT + CMGS = \"+7967-------\"");
delay(1000);
gprsSerial.print("Internet turned on!");
delay(100);
gprsSerial.println((char)26);
delay(1000);
gprsSerial.println("\r");
delay(100);
}
////If we have sms like "InternetOff", I want to stop internet connection (or stop blynk)
else if (!currStr.compareTo("InternetOff")) {
Blynk.disconnect()
/////////WILL IT WORK???
///////All of this is for minimising payment for internet
///maybe it is better to disconnect GPRS at all using this command?
////AT+SAPBR=0,1
//
}else {
if (currStr.startsWith("+CMT")) {
//если текущая строка начинается с "+CMT",
//то следующая строка является сообщением
isStringMessage = true;
}
}
currStr = "";
} else if ('\n' != currSymb) {
currStr += String(currSymb);
}
}