OP's code:
///////////////////////////CREDENTIALS///////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
#define station_id "****"
#define username "****"
#define password "****"
#define owner_id "****"
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
const char* apn = "internet";
//Vodafone: internet OR webonly.vodafone.gr OR internet.vodafone.gr
//Cosmote: internet
//Wind: gnet.b-online.gr OR gint.b-online.gr
//Q-Telecom: myq
//Taza Mobile: internet.vodafone.gr
#define TINY_GSM_MODEM_SIM800
#define RFM95_CS 10
#define RFM95_RST 11
#define RFM95_INT 2
#define RF95_FREQ 434.0
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <RH_RF95.h>
#include <SoftwareSerial.h>
RH_RF95 rf95(RFM95_CS, RFM95_INT);
unsigned long time = 0;
uint8_t buf[150];
uint8_t len = sizeof(buf);
char module_id[25];
char radiopacket[20];
unsigned long lastPost = 10800000;
unsigned long postInterval = 10800000;
short int tries = 2;
SoftwareSerial SerialAT(8, 9);
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
PubSubClient mqtt(client);
void reconnect() {
if (!modem.waitForNetwork() || !modem.gprsConnect(apn, "", "")) {
initialize_modem();
}
if (mqtt.connect(station_id, username, password))
{
mqtt.subscribe(station_id, (uint8_t)0);
}
}
void initialize_radio_module() {
if (!rf95.init()) {
initialize_radio_module();
} else {
rf95.setFrequency(RF95_FREQ);
rf95.setTxPower(23, false);
}
}
void initialize_modem() {
modem.restart();
if (!modem.waitForNetwork()) {
initialize_modem();
}
if (!modem.gprsConnect(apn, "", "")) {
initialize_modem();
}
}
void initialize_mqtt() {
mqtt.setServer("****", 1883);
mqtt.setCallback(mqttCallback);
mqtt.connect(station_id, username, password);
mqtt.subscribe(station_id, (uint8_t)0);
}
void send_sms(String& phone, String& txt) {
modem.sendAT(GF("+CMGF=1"));
modem.waitResponse();
modem.sendAT(GF("+CSCS=\"UCS2\""));
modem.waitResponse();
modem.sendAT(GF("+CSMP=17,167,0,8"));
modem.waitResponse();
modem.sendAT(GF("+CMGS=\""), phone, GF("\""));
if (modem.waitResponse(GF(">")) != 1) {
}
modem.stream.print(txt);
modem.stream.write(0x1A);
modem.stream.flush();
delay(1000);
mqtt.publish(owner_id, "sms");
}
void getMeasurements(String &mod, int n) {
memset(buf, 0, 150);
if (n > 0) {
sendMessage(mod);
delay(250);
if (rf95.waitAvailableTimeout((n * 4300))) {
len = sizeof(buf);
if (rf95.recv(buf, &len)) {
mqtt.publish(owner_id, (char *)buf);
return;
} else {
delay(400);
sendMessage(module_id);
getMeasurements(mod, --n);
}
} else {
delay(400);
sendMessage(module_id);
getMeasurements(mod, --n);
}
} else {
mod += ":mnf"; //module_id + :mnf , module not found
mod.toCharArray(module_id, mod.length() + 1);
mqtt.publish(owner_id, module_id);
}
}
void sendMessage(String Message) {
Message.toCharArray(radiopacket, 20);
rf95.send((uint8_t *)radiopacket, strlen(radiopacket));
rf95.waitPacketSent();
}
void mqttCallback(char *topic, byte *payload, unsigned int len) {
/*
f : irrigation off
n: irrigation on
h: station ping
s: station sms
r{module_id}: refresh measurements for module
*/
char* cmd = (byte*)malloc(len);
memcpy(cmd, payload, len);
switch (cmd[0]) {
case 'h': //responds to user's ping
mqtt.publish(owner_id, "h");
free(cmd);
break;
case 'r': { //refreshes measurements from a module
memset(module_id, 0, 25);
String x = "";
for (int i = 1; i < len; i++) {
x += cmd[i];
}
getMeasurements(x, tries);
free(cmd);
break;
}
case 's': { //sends sms
String phone, msg = "";
for (int i = 1; i < 41; i++)
{
phone += cmd[i];
}
for (int i = 41; i < len; i++)
{
msg += cmd[i];
}
send_sms(phone, msg);
free(cmd);
break;
}
}
}
void setup() {
delay(5000);
SerialAT.begin(4800);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
initialize_radio_module();
initialize_modem();
initialize_mqtt();
}
void loop()
{
time = millis();
if (!mqtt.connected()) {
reconnect();
} else {
if (time - lastPost >= postInterval) {
lastPost = time;
mqtt.publish(owner_id, "tm");
}
mqtt.loop();
}
}