/**
* @file main.cpp
* @brief rhomb.io SIM868 demo
* @author rhomb.io Software Team
*
* Version 1.0
*
* @copyright Copyright (c) 2019
*/
#include <Arduino.h>
// Include rhio-pinmap and enable one MCU
// #define S100_DUINO_UNO_v1_0
// #define S100_DUINO_LEONARDO_v1_0
// #define S200_DUINO_UNO_PRO_v1_0
#define S200_DUINO_ZERO_v1_0
// #define S100_DUINO_MEGA_v2_0
// #define S100_ESP32_WROOM_32_v1_0
// #define S100_ESP_WROOM_02_v1_0
#include "rhio-pinmap.h"
// SIM868 pins on Socket 1
#define GSM_PWREN IO0
#define GSM_PWRKEY IO1
#define GPS_EN IO2
#define GSM_STATUS IO3
// SIM868 pins on Socket 2 (only Duino Mega)
// #define GSM_PWREN IO8
// #define GSM_PWRKEY IO9
// #define GPS_EN IO10
// #define GSM_STATUS IO11
// SIM868 apn parameters
#define RH_apn "m2m.orange.es"
#define RH_apnUser ""
#define RH_apnPass ""
#define RH_remoteServer "45.76.37.219"
#define RH_remotePort "3136"
// Monitor serial (debug) and modem At commands
#ifdef S200_DUINO_ZERO_v1_0
#define SerialMon SerialUSB
#define SerialAt Serial1 // Serial1 = UART-A
#endif
#ifdef S100_DUINO_UNO_v1_0
#include SoftwareSerial
SoftwareSerial SoftSerial(IO6, IO7); // also may be SDA,SCL, MISO,MOSI and others
#define SerialMon SoftSerial
#define SerialAt Serial // Serial1 = UART-A
#endif
#ifdef S100_DUINO_MEGA_v2_0
#define SerialMon Serial1 // UART-B
#define SerialAt Serial // UART-A
#endif
// At commands responses will be allocated here
u_int8_t signal_quality;
char buffer[30];
void configureIos () {
pinMode(GSM_PWRKEY, OUTPUT);
pinMode(GSM_PWREN, OUTPUT);
pinMode(GPS_EN, OUTPUT);
pinMode(GSM_STATUS, INPUT);
}
void configureSerial () {
SerialMon.begin(9600);
while (!SerialMon) {}
SerialAt.begin(9600);
while (!SerialAt) {}
}
/**
* @brief Turn on the modem
*
* To be able to use the SIM868 module we have to activate the PWREN line and
* make a pulse of 1.5 seconds in the PWRKEY line. The modem will have been
* turned on when the STATUS line goes to high level.
*
* This allows the MCU to control the power of the device, necessary for low
* power applications. By default the modem will be turned off and will only
* turn on if we perform the turn on routine as seen in this method.
*
* @return true if the modem has been turned on
* @return false
*/
bool powerOn () {
uint8_t gsm_status;
digitalWrite(GSM_PWREN, HIGH);
for (char i=0; i<5; i++) {
gsm_status = digitalRead(GSM_STATUS);
if (gsm_status == HIGH){
SerialMon.println(F("GSM HIGH!!"));
break;
} else {
SerialMon.println(F("GSM LOW!"));
digitalWrite(GSM_PWRKEY, HIGH);
delay(1500);
digitalWrite(GSM_PWRKEY, LOW);
delay(1500);
}
}
if (!gsm_status) {
return false;
}
// Turn on the GPS. It is not necessary for modem operation but this demo
// will make use of GPS.
digitalWrite(GPS_EN, HIGH);
SerialAt.println("AT+CGNSPWR=1");
delay(3000);
// We obviate the result of enabling GPS
return true;
}
void sendAt(const char * command){
strcpy(buffer,"");
SerialAt.println(command);
uint32_t init=millis();
int count=0;
while (SerialAt.available() || (millis() - init) < 3000)
{
buffer[count] = SerialAt.read();
count++;
delay(10);
}
buffer[count]='\n';
SerialMon.print("La respuesta es: ");
SerialMon.println(buffer);
}
void setup () {
configureIos();
configureSerial();
if (!powerOn()) {
SerialMon.println("Fail to power on the modem. Is the SIM868 connected to the Socket 1?");
for (;;) {}
}
sendAt("AT"); // test modem, should return "OK"
if(strcmp(buffer,"OK")){
sendAt("AT+SHUT"); //sets down any previous gprs conexions, should return "SHUT OK"
sendAt("AT+CIPMUX=0"); //sets single conexion mode, should return "OK"
sendAt("AT+CGATT=1"); //attaches GPRS Service, should return "OK"
sendAt("AT+CSTT=RH_apn,RH_apnUser,RH_apnPass");
sendAt("AT+CIICR"); //brings up the GPRS connexions, sometimes it takes a bit to respond, but should return "OK"
sendAt("AT+CIFSR"); //gives us our IP Address
sendAt("AT+CIPSTART=TCP,RH_remoteServer,RH_remotePort");
}
}
bool checkCreg () {
for (auto i = 0; i < 20; i++) {
sendAt("AT+CREG?");
if (strstr(buffer, ",1")) {
// tenemos CREG!!!
return true;
}
delay(1000);
}
return false;
}
void loop () {
// Get GPS. It will be necessary a few minutes before obtaining data.
// The important thing for this demo is not to get GPS data, but to check
// that we can send a command to the modem and get a response.
// SerialAt.println("AT+CGNSINF"); (Read GPS location data)
// delay(1000);
sendAt("AT");
if (strcmp(buffer, "OK")) {
SerialMon.println("El modem funciona!!");
} else {
Serial.println("Huston tenemos un problema");
}
if (checkCreg()) {
SerialMon.println("Tenemos conexión a red!!");
}
//sendAt("AT+CSQ");
// wait for loop
delay(5000);
}
//AT+CSQ (Comando AT que indica la calidad de la señal o la cobertura)