A9G with STM32 Blue Pill

Hi People

I am a beginner when it comes to coding, so please forgive my ignorance and stupid questions.

I am trying to create a small immobilizer and tracking unit for my bike. I am using a A9G Pudding module with a STM32 Blue Pill module (f103c6ta). I am using SMS to control the relay and for location. I also have a push button, when pressed to send a location via sms.

Receive SMS "Start" = relay1 active (circuit closed) Start enabled
Receive SMS "Stop" = relay1 inactive (Circuit open) Start Disabled
Receive SMS "Location" = Send SMS with Location

I have the A9G Connected via RX and TX to pin 9&10 of the Blue pill. I have a Push Button Connected to Blue pill pin 6 and Relay to Pin 7. i have a 18650 battery connect to the A9g VBAT input and the 3.3V blue pill input. i have a 12v via a LM7805 voltage regulator to the A9G and Blue pill 5V inputs.

I am using Arduino IDE via STLink for the coding, Arduino via the usb port for the Serial Monitor. The code is bits of different projects I picked up online. I do understand most of it or at least I think I do.

It works as intended but very inconsistently. So I can send the unit a SMS and it will work. A minute or 2 later I can send a sms and it wont. When I use serial monitor I can see it receiving the SMS and displaying the message but will not execute the corresponding functions.

I am not sure if this is a coding or hardware issue? Any assistance will be highly appreciated

#include <TinyGPSPlus.h>

#define Panic PA6

int Relay1 = (PA7);

void setup() {

  Serial1.begin(115200);
  Serial.begin(115200);
  Serial1.println("AT+RST=1\r");
  delay(20000);
  Serial.println("Initializing...");
  Serial1.println("AT+CMGF=1"); // Set GSM Module in Text Mode
  delay(3000);
  Serial1.println("AT+CNMI=2,2,0,0,0"); // New SMS Message Indications
  delay(3000);  
  Serial1.println("AT+GPSMD=2"); // Set GPS + BD Mode
  delay(3000);
  Serial1.println("AT+GPSLP=0"); // Set GPS Power mode to full
  delay(3000);
  Serial1.println("AT+GPS=1"); // Enable GPS
  delay(3000);
  Serial1.println("AT+GPSRD=15"); 
  delay(3000);
  
  Serial1.println("AT+CPMS=SM,SM,SM\r"); 
  delay(5000);
  Serial1.println("AT+CPMS=?"); 
  delay(3000);
  if (Serial1.available() > 0) {
    String incomingMessage1 = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage1);

    Serial1.flush();

  }

  Serial1.println("AT+CMGD=1,4\r"); 
  delay(5000);
  if (Serial1.available() > 0) {
    String incomingMessage1 = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage1);

    Serial1.flush();
  }

  Serial1.println("AT+CMPS?\r"); 
  delay(3000);
  if (Serial1.available() > 0) {
    String incomingMessage1 = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage1);

    Serial1.flush();

  }

  pinMode(Relay1, OUTPUT);  // Connect to s terminal on relay
  digitalWrite(Relay1, LOW);
  pinMode(Panic, INPUT_PULLUP);    // CONNECT TO PANIC BUTTON
  
  
  Serial.println("Initialization complete.");

} // End Void Setup

uint8_t prevState = 1;


void loop() {

    // Begin Relay1 ON
    if (Serial1.available() > 0) {
    String incomingMessage = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage);
    if (incomingMessage.indexOf("Start") != -1) {
      Serial.println("----- Switching Vehicle On -----");
      digitalWrite(Relay1, HIGH);  // Relay ON
      delay(3000);
      Serial.println("### RELAY ON ###");
      Serial1.println("AT+CMGD=1,4\r"); 
      delay(5000);
            
    }

   Serial1.flush();
   
  } //End Relay1 ON 

  // Begine Relay1 OFF
  if (Serial1.available() > 0) {
    String incomingMessage = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage);
    if (incomingMessage.indexOf("Stop") >= 0) {
      Serial.println("----- Switching Vehicle Off -----");
      digitalWrite(Relay1, LOW);  // Relay OFF
      delay(3000);
      Serial.println("### RELAY OFF ###");
      Serial1.println("AT+CMGD=1,4\r"); 
      delay(5000);      
    }

    Serial1.flush();
  } // End Relay1 OFF

  // Begin Location SMS
  if (Serial1.available() > 0) {
    String incomingMessage = Serial1.readString();
    Serial.println("Incoming message: " + incomingMessage);
    if (incomingMessage.indexOf("Location") != -1)  {
      Serial1.println("AT+CMGS=\"+1234567890\"\r"); // Replace +1234567890 with your number
      Serial.println("Sending SMS message...");
      delay(2000);
      
      TinyGPSPlus gps;
      while (gps.location.isValid() == 0) {
        while (Serial1.available()) {
          gps.encode(Serial1.read());
        }
      }
      
      String lat = String(gps.location.lat(), 6);
      String lon = String(gps.location.lng(), 6);
      
      String url = "https://www.google.com/maps/dir/?api=1&destination=" + lat + "," + lon + "&travelmode=driving";
      Serial.println("Google Maps URL: " + url);
      Serial1.println(url); // Send the Google Maps URL with the GPS module's location
      delay(100);
      Serial1.println((char)26); // ASCII code of CTRL+Z
      delay(1000);
      Serial.println("SMS message sent.");
      Serial1.println("AT+CMGD=1,4\r"); 
      delay(5000);
    }
 
    Serial1.flush();
 
  } // End Location SMS

    // Begin Panic Button
    uint8_t state = digitalRead(Panic);
	  if(state != prevState)
	  {
		prevState = state;
		if(state == 0)
			Serial.println("### PANIC ACTIVATED ###");
		  
      Serial1.println("AT+CMGS=\"+27710784606\"\r"); // Replace +1234567890 with your number
      Serial.println("Sending SMS message...");
      delay(2000);
      
      TinyGPSPlus gps;
      while (gps.location.isValid() == 0) {
        while (Serial1.available()) {
          gps.encode(Serial1.read());
        }
      }
      
      String lat = String(gps.location.lat(), 6);
      String lon = String(gps.location.lng(), 6);
      
      String url = "https://www.google.com/maps/dir/?api=1&destination=" + lat + "," + lon + "&travelmode=driving";
      Serial.println("Google Maps URL: " + url);
      Serial1.println("### PANIC ACTIVATED ###");
      Serial1.println(url); // Send the Google Maps URL with the GPS module's location
      delay(100);
      Serial1.println((char)26); // ASCII code of CTRL+Z
      delay(1000);
      Serial.println("SMS message sent.");
      Serial1.println("AT+CMGD=1,4\r"); 
      delay(5000);
	
      Serial1.flush();
      
    }  // End Panic Button



}  // VOID LOOP END


The pins on STM32 boards usually labelled with port - i.e PA6 rather than just 6, or PB10... and so on.
If you only indicate a number, you get uncertainty.

hi buddy

yes in the code they are labeled as

PA6
PA7
PA9
PA10

If you want help, we need more information.
Post the full code, the connection diagram, and specify your board exactly. Also specify which Arduino package for STM32 you are using - there are several.

A summary of my Serial Monitor

Push Button Pressed this function working ok

13:26:35.013 -> Incoming message: +GPSRD:$GNGGA,112633.000,2746.2253,S,03048.5813,E,1,11,1.26,1161.5,M,26.0,M,,*59
13:26:35.013 -> $GPGSA,A,3,14,19,17,02,22,03,,,,,,,1.52,1.26,0.85*06
13:26:35.013 -> $BDGSA,A,3,25,22,23,08,12,,,,,,,,1.52,1.26,0.85*10
13:26:35.013 -> $GPGSV,3,1,12,14,77,337,12,22,69,237,17,17,51,198,30,03,49,113,29*71
13:26:35.013 -> $GPGSV,3,2,12,06,39,281,,19,35,225,16,02,18,132,25,30,12,338,*74
13:26:35.013 -> $GPGSV,3,3,12,21,05,126,,09,04,031,,04,04,060,,11,02,300,*71
13:26:35.013 -> $BDGSV,4,1,14,12,51,161,27,05,45,049,,25,42,267,21,11,37,243,*65
13:26:35.013 -> $BDGSV,4,2,14,24,37,340,,21,33,053,,08,29,145,19,13,25,144,*64
13:26:35.013 -> $BDGSV,4,3,14,02,25,072,,22,24,112,16,10,12,067,,23,09,222,18*69
13:26:35.013 -> $BDGSV,4,4,14,26,02,016,,03,01,086,*60
13:26:35.013 -> $GNRMC,112633.000,A,2746.2253,S,03048.5813,E,0.892,299.87,141024,,,A*54
13:26:35.013 -> $GNVTG,299.87,T,,M,0.892,N,1.653,K,A*2C
13:26:35.013 -> 
13:26:42.186 -> ### PANIC ACTIVATED ###
13:26:42.186 -> Sending SMS message...
13:26:48.932 -> Google Maps URL: https://www.google.com/maps/dir/?api=1&destination=-27.770437,30.809693&travelmode=driving
13:26:50.036 -> SMS message sent.
13:26:56.036 -> Incoming message: 0.809693&travelmode=driving
13:26:56.036 -> 
13:26:56.036 -> +CMGS: 68
13:26:56.036 -> 
13:26:56.036 -> OK
13:26:56.036 -> 
13:26:56.036 -> AT+CMGD=1,4

13:26:56.036 -> Sending SMS message...
13:27:03.959 -> Google Maps URL: https://www.google.com/maps/dir/?api=1&destination=-27.770495,30.809678&travelmode=driving
13:27:05.084 -> SMS message sent.
13:27:11.054 -> Incoming message: &travelmode=driving
13:27:11.054 -> 
13:27:11.054 -> +CMGS: 69
13:27:11.054 -> 
13:27:11.054 -> OK
13:27:11.054 -> 
13:27:11.054 -> AT+CMGD=1,4

13:27:11.054 -> OK
13:27:11.054 -> 
13:27:11.054 -> 
13:27:20.063 -> Incoming message: +GPSRD:$GNGGA,112718.000,2746.2269,S,03048.5808,E,1,11,1.26,1159.9,M,26.1,M,,*54
13:27:20.063 -> $GPGSA,A,3,14,19,17,02,22,03,,,,,,,1.52,1.26,0.85*06
13:27:20.063 -> $BDGSA,A,3,25,22,23,08,12,,,,,,,,1.52,1.26,0.85*10
13:27:20.063 -> $GPGSV,3,1,12,14,76,338,14,22,70,238,19,17,52,197,30,03,49,113,31*75
13:27:20.063 -> $GPGSV,3,2,12,06,39,281,,19,35,225,16,02,18,132,24,30,12,338,*75
13:27:20.063 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,13*70
13:27:20.063 -> $BDGSV,4,1,14,12,51,160,25,05,45,049,,25,42,267,21,11,38,243,*69
13:27:20.063 -> $BDGSV,4,2,14,24,37,340,,21,33,053,,08,29,145,20,13,25,144,*6E
13:27:20.063 -> $BDGSV,4,3,14,02,25,072,,22,24,112,19,10,12,067,,23,09,222,19*67
13:27:20.063 -> $BDGSV,4,4,14,26,02,016,,03,01,086,*60
13:27:20.063 -> $GNRMC,112718.000,A,2746.2269,S,03048.5808,E,1.172,336.13,141024,,,A*50
13:27:20.063 -> $GNVTG,336.13,T,,M,1.172,N,2.172,K,A*24
13:27:20.063 -> 

Location SMS sent to unit not working this time

13:27:35.923 -> Incoming message: +GPSRD:$GNGGA,112733.000,2746.2258,S,03048.5807,E,1,11,0.89,1159.6,M,26.0,M,,*5A
13:27:35.923 -> $GPGSA,A,3,19,17,30,02,22,03,,,,,,,1.18,0.89,0.78*08
13:27:35.923 -> $BDGSA,A,3,25,22,23,08,12,,,,,,,,1.18,0.89,0.78*18
13:27:35.923 -> $GPGSV,3,1,12,14,76,338,,22,70,238,17,17,52,197,29,03,49,113,32*75
13:27:35.923 -> $GPGSV,3,2,12,06,39,281,,19,35,225,19,02,18,132,25,30,12,338,14*7E
13:27:35.923 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:27:35.923 -> $BDGSV,4,1,14,12,51,160,26,05,45,049,,25,42,267,17,11,38,243,*6F
13:27:35.923 -> $BDGSV,4,2,14,24,37,340,,21,33,054,,08,29,145,20,13,25,144,*69
13:27:35.923 -> $BDGSV,4,3,14,02,25,072,,22,24,113,19,10,12,067,,23,10,223,18*6E
13:27:35.923 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:27:35.923 -> $GNRMC,112733.000,A,2746.2258,S,03048.5807,E,0.247,167.78,141024,,,A*5B
13:27:35.923 -> $GNVTG,167.78,T,,M,0.247,N,0.458,K,A*24
13:27:35.923 -> +CIEV: "MESSAGE",1
13:27:35.923 -> +CMT: "+1234567890",,"2024/10/14,13:27:31+02"
13:27:35.923 -> Location
13:27:35.923 -> 
13:27:49.993 -> Incoming message: +GPSRD:$GNGGA,112748.000,2746.2226,S,03048.5782,E,1,11,0.89,1160.5,M,26.0,M,,*54
13:27:49.993 -> $GPGSA,A,3,19,17,30,02,22,03,,,,,,,1.18,0.89,0.78*08
13:27:49.993 -> $BDGSA,A,3,25,22,23,08,12,,,,,,,,1.18,0.89,0.78*18
13:27:49.993 -> $GPGSV,3,1,12,14,76,338,,22,70,238,17,17,52,197,28,03,49,113,32*74
13:27:49.993 -> $GPGSV,3,2,12,06,39,280,,19,35,225,18,02,17,131,26,30,12,338,14*71
13:27:49.993 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:27:49.993 -> $BDGSV,4,1,14,12,51,160,27,05,45,049,,25,42,267,15,11,38,243,*6C
13:27:49.993 -> $BDGSV,4,2,14,24,37,340,,21,33,054,,08,29,145,20,13,25,144,*69
13:27:49.993 -> $BDGSV,4,3,14,02,25,072,,22,24,113,19,10,12,067,,23,10,223,17*61
13:27:49.993 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:27:49.993 -> $GNRMC,112748.000,A,2746.2226,S,03048.5782,E,0.523,291.34,141024,,,A*5B
13:27:49.993 -> $GNVTG,291.34,T,,M,0.523,N,0.969,K,A*2C
13:27:49.993 -> 

Start SMS sent to unit working ok this time

13:28:04.999 -> Incoming message: +CIEV: "MESSAGE",1
13:28:04.999 -> +CMT: "+1234567890",,"2024/10/14,13:27:59+02"
13:28:04.999 -> Start
13:28:04.999 -> +GPSRD:$GNGGA,112803.000,2746.2191,S,03048.5767,E,1,10,0.93,1161.9,M,26.0,M,,*57
13:28:04.999 -> $GPGSA,A,3,19,17,30,02,22,03,,,,,,,1.56,0.93,1.26*03
13:28:04.999 -> $BDGSA,A,3,22,23,08,12,,,,,,,,,1.56,0.93,1.26*14
13:28:04.999 -> $GPGSV,3,1,12,14,76,339,,22,70,239,17,17,52,196,27,03,49,113,31*79
13:28:04.999 -> $GPGSV,3,2,12,06,39,280,,19,36,224,14,02,17,131,24,30,12,338,15*7C
13:28:04.999 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:28:04.999 -> $BDGSV,4,1,14,12,51,160,25,05,45,049,,25,42,267,,11,38,243,*6A
13:28:04.999 -> $BDGSV,4,2,14,24,37,340,,21,33,054,,08,29,145,18,13,25,144,*62
13:28:04.999 -> $BDGSV,4,3,14,02,25,072,,22,24,113,17,10,12,067,,23,10,223,17*6F
13:28:04.999 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:28:04.999 -> $GNRMC,112803.000,A,2746.2191,S,03048.5767,E,0.471,292.38,141024,,,A*56
13:28:04.999 -> $GNVTG,292.38,T,,M,0.471,N,0.872,K,A*2E
13:28:04.999 -> 
13:28:04.999 -> ----- Switching Vehicle On -----
13:28:07.979 -> ### RELAY ON ###
13:28:14.691 -> Incoming message: AT+CMGD=1,4

13:28:14.691 -> OK
13:28:14.691 -> 
13:28:14.691 -> 
13:28:19.956 -> Incoming message: +GPSRD:$GNGGA,112818.000,2746.2226,S,03048.5787,E,1,10,0.93,1164.1,M,26.0,M,,*51
13:28:19.956 -> $GPGSA,A,3,19,17,30,02,22,03,,,,,,,1.56,0.93,1.26*03
13:28:19.956 -> $BDGSA,A,3,22,23,08,12,,,,,,,,,1.56,0.93,1.26*14
13:28:19.956 -> $GPGSV,3,1,12,14,76,339,,22,70,239,16,17,52,196,30,03,49,113,31*7E
13:28:19.956 -> $GPGSV,3,2,12,06,39,280,,19,36,224,12,02,17,131,24,30,12,339,15*7B
13:28:19.956 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:28:19.956 -> $BDGSV,4,1,14,12,51,160,27,05,45,049,,25,42,267,,11,38,243,*68
13:28:20.005 -> $BDGSV,4,2,14,24,37,340,,21,33,054,,08,29,145,18,13,25,144,*62
13:28:20.005 -> $BDGSV,4,3,14,02,25,072,,22,24,113,16,10,12,067,,23,10,223,17*6E
13:28:20.005 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:28:20.005 -> $GNRMC,112818.000,A,2746.2226,S,03048.5787,E,1.046,37.84,141024,,,A*66
13:28:20.005 -> $GNVTG,37.84,T,,M,1.046,N,1.939,K,A*1A
13:28:20.005 -> 

Stop SMS sent to unit not working this time

13:28:34.973 -> Incoming message: +GPSRD:$GNGGA,112833.000,2746.2211,S,03048.5801,E,1,10,0.92,1164.6,M,26.0,M,,*5B
13:28:34.973 -> $GPGSA,A,3,19,17,30,02,22,03,,,,,,,1.21,0.92,0.78*08
13:28:34.973 -> $BDGSA,A,3,22,23,08,12,,,,,,,,,1.21,0.92,0.78*1F
13:28:34.973 -> $GPGSV,3,1,12,14,76,339,,22,70,239,17,17,52,196,30,03,49,113,31*7F
13:28:34.973 -> $GPGSV,3,2,12,06,39,280,,19,36,224,17,02,17,131,25,30,12,339,13*79
13:28:35.020 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:28:35.020 -> $BDGSV,4,1,14,12,51,160,25,05,45,049,,25,42,267,,11,38,242,*6B
13:28:35.020 -> $BDGSV,4,2,14,24,37,340,,21,33,054,,08,28,145,18,13,25,144,*63
13:28:35.020 -> $BDGSV,4,3,14,02,25,072,,22,24,113,18,10,12,067,,23,10,223,19*6E
13:28:35.020 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:28:35.020 -> $GNRMC,112833.000,A,2746.2211,S,03048.5801,E,0.629,6.51,141024,,,A*5E
13:28:35.020 -> $GNVTG,6.51,T,,M,0.629,N,1.166,K,A*2C
13:28:35.020 -> 
13:28:36.195 -> Incoming message: +CIEV: "MESSAGE",1
13:28:36.195 -> +CMT: "+1234567890",,"2024/10/14,13:28:31+02"
13:28:36.195 -> Stop
13:28:36.195 -> 
13:28:50.015 -> Incoming message: +GPSRD:$GNGGA,112848.000,2746.2245,S,03048.5807,E,1,11,0.89,1166.3,M,26.0,M,,*5C
13:28:50.015 -> $GPGSA,A,3,14,19,17,30,02,22,03,,,,,,1.16,0.89,0.74*0F
13:28:50.015 -> $BDGSA,A,3,22,23,08,12,,,,,,,,,1.16,0.89,0.74*1D
13:28:50.015 -> $GPGSV,3,1,12,14,76,339,14,22,70,239,16,17,52,196,31,03,49,113,32*79
13:28:50.015 -> $GPGSV,3,2,12,06,39,280,,19,36,224,18,02,17,131,24,30,12,339,12*76
13:28:50.015 -> $GPGSV,3,3,12,21,04,125,,09,04,031,,04,04,060,,11,02,299,*72
13:28:50.015 -> $BDGSV,4,1,14,12,51,160,23,05,45,049,,25,42,268,,11,38,242,*62
13:28:50.015 -> $BDGSV,4,2,14,24,36,341,,21,34,054,,08,28,145,16,13,25,144,*6A
13:28:50.015 -> $BDGSV,4,3,14,02,25,072,,22,24,113,15,10,12,067,,23,10,223,20*69
13:28:50.015 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:28:50.015 -> $GNRMC,112848.000,A,2746.2245,S,03048.5807,E,0.987,193.36,141024,,,A*52
13:28:50.015 -> $GNVTG,193.36,T,,M,0.987,N,1.829,K,A*29
13:28:50.015 -> 
13:29:04.999 -> Incoming message: +GPSRD:$GNGGA,112903.000,2746.2215,S,03048.5803,E,1,12,0.85,1167.1,M,26.0,M,,*5F
13:29:04.999 -> $GPGSA,A,3,14,19,17,30,02,22,03,,,,,,1.39,0.85,1.10*0D
13:29:04.999 -> $BDGSA,A,3,25,22,23,08,12,,,,,,,,1.39,0.85,1.10*18
13:29:04.999 -> $GPGSV,3,1,12,14,76,339,15,22,70,239,16,17,52,196,31,03,49,114,29*75
13:29:04.999 -> $GPGSV,3,2,12,06,39,280,,19,36,224,18,02,17,131,24,30,12,339,15*71
13:29:04.999 -> $GPGSV,3,3,12,04,05,061,,21,04,125,,09,04,031,,11,02,299,*72
13:29:04.999 -> $BDGSV,4,1,14,12,51,160,27,05,45,049,,25,42,268,22,11,38,242,*66
13:29:04.999 -> $BDGSV,4,2,14,24,36,341,,21,34,054,,08,28,145,15,13,25,144,*69
13:29:05.052 -> $BDGSV,4,3,14,02,25,072,,22,24,113,16,10,12,067,,23,10,223,24*6E
13:29:05.052 -> $BDGSV,4,4,14,03,01,086,,26,01,016,*63
13:29:05.052 -> $GNRMC,112903.000,A,2746.2215,S,03048.5803,E,1.218,314.03,141024,,,A*5A
13:29:05.052 -> $GNVTG,314.03,T,,M,1.218,N,2.257,K,A*2E
13:29:05.052 -> 

The code posted is the complete code

I have attached the schematic

Board model is STM32F103C6T6

1.pdf (39.5 KB)

Please show Arduino IDE compiling message

How do you upload the code to the board? By Serial or via St-Link or... ?

STLink using arduino IDE

Any Help Guys?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.