unique reference number

Hello All, I would like some assistance please with generating a unique/random number in a text message which is sent as an alarm

my current code looks like this

#include <MKRGSM.h> // Include the GSM library
#include "arduino_secrets.h" // Please enter your sensitive data in the Secret tab or arduino_secrets.h
#include<UltraDistSensor.h>

const char PINNUMBER[] = SECRET_PINNUMBER;// PIN Number
GSM gsmAccess;// initialize the library instance
GSM_SMS sms;
UltraDistSensor mysensor;
float reading;

void setup() {
Serial.begin(9600);// initialize serial communications and wait for port to open:
mysensor.attach(12,13);//Trigger pin , Echo pin
Serial.println("SMS Messages Sender");
bool connected = false;// connection state
while (!connected) {

if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;

} else {

Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");

sms.beginSMS("+31**********");//enter your number here
sms.print("Alarm Has Restarted Following A Power Outage");//this sends an SMS once only on powerup
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);
sms.beginSMS("+31-------------");//enter a 2nd number here is required
sms.print("Alarm Has Restarted Following A Power Outage");//this sends an SMS once only on powerup
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);

}

void loop() {
reading=mysensor.distanceInCm();
Serial.print("Sensor Reading :");Serial.print(reading);Serial.println(" CM");//measuring distances every 3seconds (3* delays of 1000ms)
delay(1000);

if(reading < 117)//read the distance in the serial monitor of the area you want to cover, in this case <117 is any object moves within 117cm send the alerts
{
sms.beginSMS("+31***********");//enter your number here
sms.print("Proximity Alert");//alarm to be sent
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);
sms.beginSMS("+31-------------");//enter a 2nd number here is required
sms.print("Proximity Alert");//alarm to be sent
sms.endSMS();
Serial.println("SMS Sent");
delay(60000); //delay 15min, no measurements made or alerts sent during this period

}else{

delay (1000);
}
}

this alarm uses an ultrasonic sensor and works perfectly but I have found I may be getting duplicate SM's some with significant delays so I need a reference number to rule that out

so if I create a random number with this

randomSeed(analogRead(0));
}

void loop() {
// print a random number from 0 to 299
randNumber = random(300);
Serial.println(randNumber);

// print a random number from 10 to 19
randNumber = random(10, 20);
Serial.println(randNumber);

delay(50);
}

can you please advise how to incorporate that into the SMS, is it something like this?

sms.beginSMS("+31**********");//enter your number here
sms.print("Alarm Has Restarted Following A Power Outage", randNumber);//this sends an SMS once only on powerup
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);

thank you for taking time to respond

Dan

sms.beginSMS("+31**********");//enter your number here
sms.print("Alarm Has Restarted Following A Power Outage");//this sends an SMS once only on powerup
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);
sms.beginSMS("+31-------------");//enter a 2nd number here is required
sms.print("Alarm Has Restarted Following A Power Outage");//this sends an SMS once only on powerup
sms.endSMS();
Serial.println("SMS Sent");
delay(1000);

Creating and calling a function beats copying and pasting code (several times).
Creating and calling a function beats copying and pasting code (several times).

Serial.print("Sensor Reading :");Serial.print(reading);Serial.println(" CM");//measuring distances every 3seconds (3* delays of 1000ms)

That comment is horse crap. That is NOT what that code does.

is it something like this?

No. sms.print() does not take two arguments (at least, not those two). There is no reason that you can't call sms.print() more than once (or a dozen times).

so I need a reference number to rule that out

It sounds like you want to concatenate a random number with the text message.
Something like this maybe

char convertedInt[10];

void setup()
{
  Serial.begin(115200);
  randomSeed(analogRead(A0));
  itoa(random(10, 20), convertedInt, 10);
}

void loop()
{
  char textMessage[200] = "A test text message : ";
  strcat(textMessage, convertedInt);
  Serial.println(textMessage);
  delay(500);
}

UKHeliBob I believe that is exactly what I am trying to do, concatenate is what I was looking for, thanks for that I will give it a try and post my results

@PaulS your reply is as expected, I was dreading posting on here as I knew your response would be negative (as they all are (no really they are, go back and read all of your responses to any post from anyone as I have done...)) the response is even more confusing than my original problem NB I may not be an electronics or programming expert like you but my project works fine

Dan

I believe that is exactly what I am trying to do

Bear in mind that the technique will not work if you need to know which device the text came from and there is also a chance that two senders will produce the same random number. You could, of course, prevent this by using a different random number range on each sender but if you are going to do that then you might just as well hard code the sender number.

Looking back in this thread I have assumed that there will be more than one device sending SMS messages. If that is not the case then what is the point of the ID number concatenated with the message ? If you want to avoid sending duplicate messages within a time frame then there are simple ways to do it.

What exactly is the requirement ?

its just one device at the moment, the issue I have is the alarm has sent two SMS to the same number (both numbers)within a few seconds and we received one at 5am on Saturday morning, now it could have been a mouse or spider, probably was not a person but I also believe that the network I am using could be sending duplicate SMS even with a delay of many hours (I have seen this before) so with a unique ID I can identify that and rule out issues with the code timing etc,

I will stick a camera and SD card on the device soon

in your response below you are concatenating two char values, does it have to be this way or can the one char be concatenated to "something in quotes"?

there is quite a bit of information on this but its somewhat confusing for me

thanks

Dan

in your response below you are concatenating two char values, does it have to be this way or can the one char be concatenated to "something in quotes"?

As long as the destination string (the first parameter) is large enough to accommodate its original contents plus the string to be concatenated with it (either a variable or an explicit string) then the second parameter can be any length within reason, including a single character, but when calculating the required size of the destination array don't forget the terminating '\0' that will be present

My projects do exactly what you’re wanting...
I take the time-of-day from the cellular network as my local time (no RTC needed), then send messages with a ‘sent at:’ tag - so the recipient knows when the message was created - regardless of network latency.
If you’re trying to identify the sender as well, simply extract the sender’s caller-id from the incoming message and validate it against your list of known senders.

It may also be worth implementing a message queue...
My controllers can generate multiple messages to multiple recipients... faster than the mobile network will accept those messages, but the monitoring process can’t stop. So a non-blocking queue is needed.
Push each outbound message onto a FIFO queue, then pull them down as each ‘send’ is acknowledged by the network until the stack is empty.

lastchancename:
My projects do exactly what you’re wanting...
I take the time-of-day from the cellular network as my local time (no RTC needed), then send messages with a ‘sent at:’ tag - so the recipient knows when the message was created - regardless of network latency.
If you’re trying to identify the sender as well, simply extract the sender’s caller-id from the incoming message and validate it against your list of known senders.

It may also be worth implementing a message queue...
My controllers can generate multiple messages to multiple recipients... faster than the mobile network will accept those messages, but the monitoring process can’t stop. So a non-blocking queue is needed.
Push each outbound message onto a FIFO queue, then pull them down as each ‘send’ is acknowledged by the network until the stack is empty.

lastchancename thats impressive, I may try that later

Dan0:
@PaulS your reply is as expected, I was dreading posting on here as I knew your response would be negative (as they all are (no really they are, go back and read all of your responses to any post from anyone as I have done...)) the response is even more confusing than my original problem NB I may not be an electronics or programming expert like you but my project works fine

Think of it as "tough love." It's almost always good advice, and following it will make you a better programmer.

dnwheeler:
Think of it as "tough love." It's almost always good advice, and following it will make you a better programmer.

or I will avoid the forum unless really stuck...

nose != face

working well now, I did try to add the measurement but couldn't convert a float to a char which I believe is required to concatenate it into the SMS

I was also going to add a while function so it didn't alert until it returned to a normal state but again I couldn't get me head around it just yet

so now it sends out messages with unique IDs to two numbers (same ID per message)

thanks

#include <MKRGSM.h> // Include the GSM library
#include "arduino_secrets.h" // Please enter your sensitive data in the Secret tab or arduino_secrets.h
#include<UltraDistSensor.h>
char convertedInt[10];

const char PINNUMBER[] = SECRET_PINNUMBER;// PIN Number
GSM gsmAccess;// initialize the library instance
GSM_SMS sms;
UltraDistSensor mysensor;
float reading;

void setup() {
Serial.begin(9600);// initialize serial communications and wait for port to open:

mysensor.attach(12,13);//Trigger pin , Echo pin
Serial.println("SMS Messages Sender");
bool connected = false;// connection state
while (!connected) {

if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
connected = true;

} else {

Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
delay(5000);
sms.beginSMS("+31********");
randomSeed(analogRead(0));
itoa(random(1000, 2000), convertedInt, 10);
char powerMessage[200] = "Alarm Has Restarted Following A Power Outage : ";
strcat(powerMessage, convertedInt);
sms.print(powerMessage);
sms.endSMS();
Serial.println("SMS Sent to +31************");
delay(500); 

sms.beginSMS("+31***********");
sms.print(powerMessage);
sms.endSMS();
Serial.println("SMS Sent to +31***********");
delay(500);
 
}

void loop() {
reading=mysensor.distanceInCm();
Serial.print("Sensor Reading :");Serial.print(reading);Serial.println(" CM");
delay(1000);

if(reading < 117){
sms.beginSMS("+31*********");
randomSeed(analogRead(0));
itoa(random(2000, 3000), convertedInt, 10);
char alarmMessage[200] = "Proximity Alert : ";
strcat(alarmMessage, convertedInt);
sms.print(alarmMessage);
sms.endSMS();
Serial.println("SMS Sent to +31**********");
delay(500); 

sms.beginSMS("+31********");
sms.print(alarmMessage);
sms.endSMS();
Serial.println("SMS Sent to +31**********");
delay(30000); 
 
}else{

delay (1000);
}
}

but couldn't convert a float to a char which I believe is required to concatenate it into the SMS

Converting a float to a char is not possible. Converting a float to a string (a NULL terminated array of chars) is trivial - dtostrf().

PaulS:
Converting a float to a char is not possible. Converting a float to a string (a NULL terminated array of chars) is trivial - dtostrf().

but can that be concatenated into the SMS?

strcat(powerMessage, reading, convertedInt);

this is what I could not get right after some research

Dan

but can that be concatenated into the SMS?

Of course. dtostrf() creates a string variable. strcat() concatenates strings. Review what I said in reply #6

yes.
Post your latest code attempt - in </> code tags...

lastchancename do you have any anomalous readings with your setup? I got another alert this morning, it could be interference or some issue with the sensor/arduino. I was thinking of taking a second or third reading upon triggering just to make sure it is a detection. What do you recommend?

Dan

RESPONSE...
No anomalous readings... why would they?
It’s a pretty linear process.

Tight loop to poll inputs, evaluate trigger conditions.
When an exception or event is detected, compose a message.
push an sms to all nominated users.
Return to the tight monitoring loop.
(background pops messages until they’re all sent)

Also in the ‘background’, the cpu is listening for incoming modem/text messages, pushbuttons, flashing the LEDs, driving the LCD, driving output relays and other stuff.

I have had several alarms which are so far unexplained, this was the reason for adding the unique id, so its either a mouse, some kind of ultrasonic interference or an error on the arduino, or the program needs tweeking to solve any timing issues