Quickly briefing about my project.
Project is about controlling servo motor with sms.
Has 4 components - (sim800l v2 + arduino uno r3 + 5v relay + servo motor)
along woth the software to control servo.
When i test the software for different start stop parameters by connecting three components sim800l v2 + arduino + relay then all the test cases are pass. I am able to see this in serial monitor.
BUT
the moment i connect servo motor, the OFF parameter of the software and STATUS do not work. Sms light blinks on gsm module, sms is recoeved but does not the required work.
I am completely confused as to the same software passes all the parameters without connecting servo but the moment i connect servo ON and Start parameters goes haywire.
Only ON parameter works.
// Include Software Serial library to communicate with GSM
#include <SoftwareSerial.h>
#include <Servo.h>
// Configure software serial port
SoftwareSerial SIM900(10, 11);
// Variable to store text message
String textMessage;
// Create a variable to store Motor state
String motorState = "INITIALLY OFF";
// Relay connected to pin 12
const int relay = 2;
// Servo related parameters
int angle = 150;
int servoPin = 5;
Servo servo;
void setup() {
// Set relay as OUTPUT
pinMode(relay, OUTPUT);
// By default the relay is off
digitalWrite(relay, HIGH);
// Initializing serial commmunication
Serial.begin(19200);
SIM900.begin(19200);
// Attaching servopin
servo.attach(servoPin);
// Give time to your GSM shield log on to network
delay(5000);
Serial.print("SIM900 ready...");
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop(){
if(SIM900.available()>0)
{
textMessage = SIM900.readString();
Serial.print(textMessage);
Serial.println("Relay set to onetime");
delay(10);
}
if(textMessage.indexOf("ON")>=0)
{
// Turn on relay and save current state
digitalWrite(relay, LOW);
motorState = "on";
Serial.println("Relay set to ON");
// 58 is the deep angle press
for(angle = 86; angle < 180; angle++)
{
servo.write(angle);
delay(20);
}
delay (3000);
// 58 is the deep angle press
for(angle = 180; angle > 86; angle--)
{
servo.write(angle);
delay(20);
}
}
if(textMessage.indexOf("OFF")>=0)
{
// Turn off relay and save current state
digitalWrite(relay, HIGH);
motorState = "off";
Serial.println("Relay set to OFF");
textMessage = "";
}
if(textMessage.indexOf("STATE")>=0){
String message = "Motor is " + motorState;
Serial.println("Motor state resquest");
SIM900.println("AT+CMGF=1");
delay(1000);
SIM900.println("AT+CMGS= \"xxxxxxxxx\"\r");
delay(1000);
SIM900.println("Motor is "+ motorState);
delay(100);
SIM900.println((char)26);
}
}
I have posted my code. Please suggest. If I test in serial window by sending sms (without attaching servo motor, it works fine, By all parameters On, OFF and STATE ) But the moment I attach servo only ON parameter works. After getting in ON state nothing works further. I cannot OFF it by sending OFF sms
true, but in his scenario if using relay is required on what motor is he using, it may be helpfull to read that article because relay is not so easy to work with, especially if you are new in arduino
SoftwareSerial makes heavy use of interrupts, which will interfere with the interrupts needed by the Servo library, and the extensive use of delay() in your code is going to risk overflowing the SoftwareSerial receive buffer unless the GSM messages are limited to 63 characters or less.
You can either drive the servo through a PWM output, or use a board with a 2nd hardware serial port (you could also use Serial for the GSM communications, if you don't need to send to/from the computer, but this also requires disconnecting the GSM during code uploads).
< edit >
On an UNO, I would also recommend not using String variables, use a char array instead.
Thats a great info, you have give..thanks..
However
One more thing, could I use the PWMservo.h instead of servo.h to solve this interrupt related details ??
Or may you can share some link on how to use PWMservo.h ??
So, I tried replacing the Servo.h with PWMServo.h. But still the output is same. Once inside the start block, as soon as servo starts working, GSM is not accepting the OFF or STATE commands.
Any other suggestions ?
What messages from the GSM are you seeing in the Serial monitor after the servo is operating?
How long is the longest message you expect to receive from the GSM? If it exceeds 63 characters (including any newline or carriage returns), then you need to get rid of all the delay() statements and use millis() for timing. You need to guarantee the entire loop will execute in less than 33mS (the time it takes to receive 63 characters), otherwise you will overflow the receive buffer.
It would help if you substantially increased the baud rate for Serial, using the same baud rate as
SoftwareSerial risks overflowing the SoftwareSerial receive buffer if you print enough to completely fill the Serial transmit buffer.
Can you post a diagram of how everything is connected, particularly how you are powering the servo?
If i write this in ON and STATE block... logic works fine. Motor will only run once and exit.
However I want that motor should run until OFF msg comes as sms.