Skip to content

Instantly share code, notes, and snippets.

@benstr
Last active October 26, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benstr/f0c89d718328628fa2b3a0f29b517faf to your computer and use it in GitHub Desktop.
Save benstr/f0c89d718328628fa2b3a0f29b517faf to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
int _MODEMSTATE = 1; // set state as available
int RESET_PIN = 10 //SIM800 reset connected to pin D10
int TX_PIN = 7;
int RX_PIN = 8;
String _MESSAGEBUFFER = "";
String _SERIALBUFFER = "";
SoftwareSerial serialHologram(TX_PIN, RX_PIN);
void setup()
{
pinMode(RESET_PIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
delay(10);
digitalWrite(RESET_PIN, LOW);
delay(100);
digitalWrite(RESET_PIN, HIGH);
delay(10000); // wait for modem to startup
if(!Serial) { // if user did not begin Serial then we will
Serial.begin(19200);
while(!Serial); // wait for Serial to be ready
}
if(!serialHologram) { // if user did not begin Serial then we will
serialHologram.begin(19200);
while(!serialHologram); // wait for Serial to be ready
}
Serial.println(F("Setup complete"));
}
void loop()
{
modem_debug();
delay(1000);
}
void read_serial() {
// IMPORTANT: I want to tightly control reading from serialHologram,
// this is the only function allowed to do it
_SERIALBUFFER = "";
if (serialHologram.available() > 0) {
delay(20); // allow for buffer to build
while ( serialHologram.available() > 0 ) { // move serial buffer into global String
char r = serialHologram.read();
_SERIALBUFFER += r;
if (_SERIALBUFFER.endsWith("\n" )) { // we read the serial one line at a time
_SERIALBUFFER.replace("\r","");
_SERIALBUFFER.replace("\n","");
// If buffer has a message then break, but if the length
// is 0 after removing \r\n the serial needs to be read again
if(_SERIALBUFFER.length() > 0) {
break;
}
}
}
Serial.print(F("DEBUG: Modem Serial Buffer = "));
Serial.println(_SERIALBUFFER);
if(_MESSAGEBUFFER.length() > 0) {
Serial.print(F("DEBUG: Message Buffer = "));
Serial.println(_MESSAGEBUFFER);
}
}
}
void write_serial(const char* string) {
// Note: this expects you to check state before calling
// IMPORTANT: I want to tightly control writing to serialHologram,
// this is the only function allowed to do it
Serial.print(F("DEBUG: Write Modem Serial = "));
Serial.println(string);
serialHologram.write(string); // send command
}
void modem_debug() {
// keep track of anything written into the MCU serial
// collect it and write it to the modem serial
if (Serial.available() > 0) {
_SERIALBUFFER = "";
delay(100); // allow for buffer to build
while(Serial.available() > 0) {
char r = Serial.read();
_SERIALBUFFER += r;
}
char write_string[_SERIALBUFFER.length()];
_SERIALBUFFER.toCharArray(write_string, sizeof(write_string));
write_serial(write_string);
}
// normally we get debug messages when another function runs a modem command
// but if there is not another function using the modem then we need to listen
// MAKE SURE Arduino serial monitor is sending both NL & CR!!
if(_MODEMSTATE == 1 && serialHologram.available() > 0) {
_MODEMSTATE = 0;
while(serialHologram.available() > 0) {
read_serial();
}
_MODEMSTATE = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment