70. AT Command System via UART

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

 

Code

#define LED_Pin     4
#define Analog_Pin  A0

char uart_buffer[20] = { 0 };
int index = 0;
uint32_t uptime = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED_Pin, OUTPUT);
}

void loop() {

  if(Serial.available() > 0)
  {
    char received_char = Serial.read();
    if(received_char != '\n')
      uart_buffer[index++] = received_char;
    else
    {
      if(strcmp(uart_buffer, "AT") == 0)
      {
        Serial.println("OK");
      }
      else if(strcmp(uart_buffer, "AT+LED=ON") == 0)
      {
        digitalWrite(LED_Pin, HIGH);
        Serial.println("+LED:\"ON\"");
      }
      else if(strcmp(uart_buffer, "AT+LED=OFF") == 0)
      {
        digitalWrite(LED_Pin, LOW);
        Serial.println("+LED:\"OFF\"");
      }
      else if(strcmp(uart_buffer, "AT+SYS_ON_TIME?") == 0)
      {
        uptime = millis();
        Serial.print("+SYS_ON_TIME:\”");
        Serial.print(uptime/3600000);
        uptime = uptime%3600000;
        Serial.print(":");
        Serial.print(uptime/60000);
        uptime = uptime%60000;
        Serial.print(":");
        Serial.print(uptime/1000);
        Serial.println("\”");
      }
      else if(strcmp(uart_buffer, "AT+LED_STATUS?") == 0)
      {
        int status = digitalRead(LED_Pin);
        if(status == LOW)
          Serial.println("+LED_STATUS:\”OFF\”");
        else if(status == HIGH)
          Serial.println("+LED_STATUS:\”ON\”");
      }
      else if(strcmp(uart_buffer, "AT+ADC_VALUE?") == 0)
      {
        int analog_val = analogRead(Analog_Pin);
        Serial.print("+ADC_VALUE: ");
        Serial.println(analog_val);
      }
      else if(strcmp(uart_buffer, "AT+ADC_VOLTAGE?") == 0)
      {
        float voltage = (analogRead(Analog_Pin) / 1024.0) * 5.0; 
        Serial.print("+ADC_VOLTAGE: ");
        Serial.println(voltage);
      }
      index = 0;
      memset(uart_buffer, 0, sizeof(uart_buffer));
    }
  }
}

 

Output

Video

Add a video of the output (know more)

 

 

 

 

 

Photo of Output

 

 

 

 

 

 

Screenshot of Serial Terminal 


 

Was this helpful?
Upvote
Downvote