/* test code to store and forward SMS messages recevied over UART*/
//#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);
//SoftwareSerial Serial1(8, 10); // RX, TX
int RxCnt = 0;
int state = 0;
int RxOver = 1;
int in[35];
char msg1[35];
String Smsg1;
char clipMsg1[31];
char msg2[35];
String Smsg2;
char clipMsg2[31];
char msg3[35];
String Smsg3;
char clipMsg3[31];
int action = 0;
int slot1=0;
int slot2=0;
int slot3=0;
unsigned long previousMillis = 0;
unsigned long currentMillis;
const long interval = 2000;
int ready = 1;
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
//display.clearDisplay();
Serial.begin(115200);
while (!Serial) {
;
}
Serial.println("Message server start");
Serial1.begin(38400);
Serial.println("Serial 1 started");
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
Serial.println("display begin");
// display.setTextSize(2);
//display.setTextColor(WHITE);
//display.setCursor(0, 0);
//Serial.println("text sent");
// display.println("Welcome");
//Serial.println("text printed");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Serial.println("welcome screen");
display.println("ARDUINO");
display.println("SMS");
display.println("SERVER");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 51);
display.println("READY");
display.display();
// Serial.println("delay");
delay(1000);
display.clearDisplay();
Serial.println("entering loop");
}
void loop() { // run over and over
if(slot3==1){
Serial.println("slot 3 full");
delay(2000);
}
/*if(slot2==1){
Serial.println("slot 2 full");
delay(2000);
}*/
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("ARDUINO");
display.println("SMS");
display.println("SERVER");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 51);
display.println("READY");
display.display();
}
//Serial.println("loopstart");
//digitalWrite(13,HIGH);
//delay(1000);
//digitalWrite(13,LOW);
//delay(1000);
if (Serial1.available() > 0) {
Serial.println("data incoming");
action=0;
for (byte i = 0; i < 36; i = i + 1) {
in[i] = (Serial1.read());
Serial.print(" in: ");
Serial.print(i);
Serial.print(": ");
Serial.println(in[i]);
delay(10);
}
if (in[3] == 60) {
Serial.println("message was incoming");
ready = 0;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("INCOMING");
display.setCursor(0, 17);
display.println("MESSAGE");
display.display();
Serial.print("Action :");
action = in[4] - 48;
Serial.println(action);
Serial.println((String)"slot1:"+slot1);
Serial.println((String)"slot2:"+slot2);
Serial.println((String)"slot3:"+slot3);
}
if (in[3] == 62) {
Serial.println("message was outgoing");
}
//delay(1000);
if (action == 1) { // If first character is a 1, then the message gets stored in the msg1 array
for (byte i = 0; i < 36; i = i + 1) {
msg1[i] = in[i]; // store the received message in the array so it can be recalled later, furutre messages wont delete the stored message unless the first character is a 1
}
for (byte i = 0; i < 36; i = i + 1) {
Serial.print("MSG1 char:");
Serial.print(i);
Serial.print(" :");
Serial.println(msg1[i]);
}
delay(2000); // needed? to settle down the radio
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG1 RCVD");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 17);
display.println(msg1);
display.display();
currentMillis = millis();
Serial1.println("SMS:msg1 rcvd"); // confirmation of msg1 receipt
Serial.println("slot1 full");
slot1=1;
}
if (action == 2) { // same for message 2
for (byte i = 0; i < 36; i = i + 1) {
msg2[i] = in[i];
}
for (byte i = 0; i < 36; i = i + 1) {
Serial.print("MSG2 char:");
Serial.print(i);
Serial.print(" :");
Serial.println(msg2[i]);
}
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG2 RCVD");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 17);
display.println(msg2);
display.display();
currentMillis = millis();
Serial1.println("SMS:msg2 rcvd");
Serial.println("slot2 full");
slot2=1;
}
if (action == 3) { // same for message 3
Serial.println("action 3 called");
for (byte i = 0; i < 36; i = i + 1) {
msg3[i] = in[i];
}
for (byte i = 0; i < 36; i = i + 1) {
Serial.print("MSG3 char:");
Serial.print(i);
Serial.print(" :");
Serial.println(msg3[i]);
}
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG3 RCVD");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 17);
display.println(msg3);
display.display();
currentMillis = millis();
Serial1.println("SMS:msg3 rcvd");
Serial.println("slot3 full");
slot3=1;
}
if (action == 7) { // recall message 1
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG1");
display.setCursor(0, 17);
display.println("REQUESTED");
display.display();
currentMillis = millis();
for (byte i = 0; i < 32; i = i + 1) {
int y = (i+5);
clipMsg1[i] = msg1[y];
}
Smsg1=clipMsg1;
if(slot1==1){
Serial.println((String) "MSG1 :" + Smsg1);
//Serial.println(Smsg1);
Serial1.println((String) "SMS:" + Smsg1);
}
if(slot1==0){
Serial.println((String) "MSG1 : EMPTY");
//Serial.println(Smsg1);
Serial1.println((String) "SMS:EMPTY");
}
}
if (action == 8) { // recall message 2
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG2");
display.setCursor(0, 17);
display.println("REQUESTED");
display.display();
currentMillis = millis();
for (byte i = 0; i < 32; i = i + 1) {
int y = (i+5);
clipMsg2[i] = msg2[y];
}
Smsg2 = clipMsg2;
if(slot2==1){
Serial.println((String) "MSG2 :" + Smsg2);
Serial1.println((String) "SMS:" + Smsg2);
}
if(slot2==0){
Serial.println((String) "MSG2 :" + Smsg2);
//Serial.println(Smsg1);
Serial1.println((String) "SMS:EMPTY");
}
}
if (action == 9) { // recall message 3
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("MSG3");
display.setCursor(0, 17);
display.println("REQUESTED");
display.display();
currentMillis = millis();
for (byte i = 0; i < 32; i = i + 1) {
int y = (i+5);
clipMsg3[i] = msg3[y];
}
Smsg3 = clipMsg3;
if(slot3==1){
Serial.println((String) "MSG3 :" + Smsg3);
Serial1.println((String) "SMS:" + Smsg3);
}
if(slot3==0){
Serial.println((String) "MSG3 : EMPTY");
//Serial.println(Smsg1);
Serial1.println((String) "SMS:EMPTY");
}
}
//////////////////CLEAR THE ARRAY
for (byte i = 0; i < 36; i = i + 1) { // write 0 to all array locations for the input array, this should avoid confusion about where corrupt data comes from, not possible to be left in the array
in[i] = 0;
Serial.print(" ");
Serial.print(i);
Serial.print(": ");
Serial.println(in[i]);
delay(10);
}
}
}
Hi,
My code is above, I am experiencing a glitch which I cannot track down the cause of.
basically.
slot1, slot2 and slot3 are integers being used to keep track of if there is data in arrays msg1, msg2 and msg3.
when data is added to the arrays, the slot is set to 1, this affects how the code works later, if the slot value is a 1 or 0.
directly before setting the slots to 1, there is a serial.println to indicate that this has been done deliberately in the right part of the code.
There is only one place where each slot is set to 1 in the code, and there is a serial prompt generated as the 1 is stored.
when the code sets slot 1 as 1, randomly slots 2 or 3 will get set to one also, only ever one of them though, I have not seen both.
This is realiable, there is no serial prompt to show that they should have been set to 1, just the serial prompt for slot 1.
and what is breaking my mind, is that if i put
if(slot3==1){
Serial.println("slot 3 full");
delay(2000);
}
/*if(slot2==1){
Serial.println("slot 2 full");
delay(2000);
}*/
at the start of the loop, this unwanted behaviour stops for the slot which is not commented out. If I remove the comment exclusion from both slots, then neither gets set incorrectly.
if i comment both out, then the random setting of one slot by error resumes.
Does anyone have any idea what may cause such behaviour?
perhaps there is a bug in the code, but the fact that just including an if with a serial print, stops it happening, doesnt make sense to me.
Its late where I am , so perhaps ive messed up the code and cant see it. perhaps not.
hopefully someone can help
thanks in advance