Hey,
i have a problem with my mkr 1400 gsm. I will use this modul to send and receive telegram messages.
Therefore i use the following libs: UniversalTelegramBot.h, MKRGSM.h.
The problem is, that there are no problems, but i cant receive or send messages to my telegram bot.
And i can also ping google.com, so the gprs works.
The most tutorials that i have found works with the ESP8266WiFi modul (universal-arduino-telegram-demo/implementation.ino at master · witnessmenow/universal-arduino-telegram-demo · GitHub).
But i will use the modul in the field to set up some ralais over telegram, therefore it is better to use a gsm modul.
I have checked my chatid and my token very often, there are right.
Or cant work the gsmclient/gsmsslclient with the universaltelegram bot?
here is my code:
#include <MKRGSM.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#define relayPin1 6
#define relayPin2 7
#define BotToken "1756531022:AAFylkXd_tp0bjhqis395bXzQmnugYJDvJc"
//Gprs
const char apn[] = "pinternet.interkom.de"; //APN
const char apn_u[] = ""; //APN-Username
const char apn_p[] = ""; //APN-Password
const char pin[] = "7087"; //APN-Pin
GSMSSLClient client;
GPRS gprs;
GSM gsmAccess;
String hostName = "www.google.com";
int pingResult;
//telegram
UniversalTelegramBot bot(BotToken,client);
long checkTelegramDueTime;
int checkTelegramDelay = 1000; // 1000 (1 second)
String defaultChatId = "850790486";
void setup() {
pinMode(relayPin1, OUTPUT);// defind pin as output
pinMode(relayPin2, OUTPUT);// defind pin as output
Serial.begin(9600);// initialize serial monitor with 9600 baud.
digitalWrite(relayPin1, LOW);//relai ist in diesem Zustand immer geschlossen für einen manuellen start
digitalWrite(relayPin2, LOW);//relai ist in diesem zustand immer offen, damit das aggregat nicht gleich startet
Serial.println("Starting Arduino GPRS ping.");
// connection state
bool connected = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (!connected) {
if ((gsmAccess.begin(pin) == GSM_READY) && (gprs.attachGPRS(apn, apn_u, apn_p) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
}
void loop() {
//ping();
long now = millis();
if(now >= checkTelegramDueTime) {
Serial.println("---- Checking Telegram -----");
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
Serial.write(numNewMessages);
while(numNewMessages) {
Serial.println("Bot recieved a message");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
checkTelegramDueTime = now + checkTelegramDelay;
//Serial.write(checkTelegramDueTime);
}
now = millis();
}
void ping(){
Serial.print("Pinging ");
Serial.print(hostName);
Serial.print(": ");
pingResult = gprs.ping(hostName);
if (pingResult >= 0) {
Serial.print("SUCCESS! RTT = ");
Serial.print(pingResult);
Serial.println(" ms");
} else {
Serial.print("FAILED! Error code: ");
Serial.println(pingResult);
}
delay(5000);
}
void motor_start() {
digitalWrite(relayPin1, LOW);//vorgluehen auf aus stellen
digitalWrite(relayPin2, LOW);//anlasser auf aus stellen
digitalWrite(relayPin1, HIGH);//vorgluehen starten (unterbricht kurz die Verbindung,damit der Vorgluehprozess gestartet werden kann)
bot.sendMessage(defaultChatId, "Motor wird vorgeglueht");
delay(10000); //vorgluezeit
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);//anlasser starten
bot.sendMessage(defaultChatId, "Motor wird gestartet");
delay(10000); //anlasszeit
digitalWrite(relayPin2, LOW);//anlasser ausschalten
bot.sendMessage(defaultChatId, "Motor laeuft");
}
void motor_aus() {
digitalWrite(relayPin1, LOW);//unterbrechung motorsteuergerät
delay(1000);
digitalWrite(relayPin1, HIGH);
delay(1000);
digitalWrite(relayPin1, LOW);
bot.sendMessage(defaultChatId, "Motor ist aus");
}
void handleNewMessages(int numNewMessages) {
Serial.write("test");
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/Motor_start") {
motor_start();
bot.sendMessage(chat_id, "Motor wird gestartet...");
}
if (text == "/Motor_aus") {
motor_aus();
bot.sendMessage(chat_id, "Motor wird abgeschaltet...");
}
if (text == "/options") {
String keyboardJson = "[[\"/Motor_start\", \"/Motor_aus\"]]";
bot.sendMessageWithReplyKeyboard(chat_id, "Waehlen sie eine Aggregat Option:", "", keyboardJson, true);
}
if (text == "/start" || text == "/help") {
String welcome = "Wilkommen zur Aggregatsteuerung, " + from_name + ".\n";
welcome += "Folgende Optionen sind verfügbar.\n\n";
welcome += "/Motor_start: Der Motor wird nachdem vorgluehen gestartet\n";
welcome += "/Motor_aus : Der Motor wird abgeschaltet\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}