lonas
1
Hi,
I want to use Serial1 communication port in library but it is not working.
Same code put into main sketch work.
These are simple commands to send sms via Serial1.
Any idea why Serial1 is not working from class ?
code below:
MyGSM header:
#ifndef _h
#define MyGSM_h
#include <Arduino.h>
#include <MQTTClient.h>
const byte POWER_ON_PIN = 9;
class MyGSM
{
public:
void init();
void handle();
void sendSMS(String message);
private:
MQTTClient *mqttClient;
void powerOn();
};
#endif
MyGsm.cpp
#include "MyGSM.h"
//MyGSM::MyGSM(MQTTClient *client): mqttClient(client) {}
void MyGSM::init()
{
powerOn();
Serial1.print("AT+CMGF=1\r");
delay(100);
Serial1.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
sendSMS("Arduino Init");
}
void MyGSM::powerOn()
{
digitalWrite(POWER_ON_PIN, HIGH);
delay(1000);
digitalWrite(POWER_ON_PIN, LOW);
delay(7000);
}
void MyGSM::sendSMS(String message)
{
Serial.println("Sending");
Serial1.print("AT+CMGS=\"+48508426980\"\r");
Serial1.print("messsege!\r");
Serial1.write(0x1A);
}
main sketch
#include <MyGSM.h>
#include <MQTTClient.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Bridge.h>
#include <Arduino.h>
MyGSM gsm = MyGSM();
void setup() {
Serial1.begin(19200);
Serial.begin(19200);
gsm.init();
}
void loop() {
}
system
2
#ifndef _h
That is a poor inclusion guard.
#define MyGSM_h
It does not match what you then define.
MyGSM gsm = MyGSM();
Why are you calling the constructor directly? That is not correct.
MyGSM gsm;
is correct.
There is no reason why Serial1 can not be used in a class. What happens when you run that code?
lonas
3
Regarding header it has been copied with mistake here.
I have that as expected "#ifndef MyGSM_h"
Ok I have found what was the root cause.
Adding delay between commads solved an issue and message has been sent
Serial1.print("AT+CMGS=\"+48727004396\"\r");
delay(1000);
Serial1.print("messse33333ge!\r");
delay(1000);
Serial1.write(0x1A);
thanks
Your powerOn() function turns the power off. Do you understand that things happen sequentially in a computer?
lonas
5
yes I know what sequence is.. but try this on your side first, OK ?
Using code as in my example GSM is not being reload during arduino restart (green led is not being disable during restart and GSM keep GSM connection)
beside same example is in this manual :
Does a LOW on POWER_ON_PIN turn the GSM module on, or off?
lonas
7
when You set just HIGH - it's set module ON - but it starts as disabled when restarting arduino
when sequence is as in my example GSM is not reloading connection while arduino restart (GSM is not being down)