How to send SMS with Nordic characters on Arduino MKR GSM 1400

I bough the Arduine MKR GSM 1400 to send som SMSes in Norwegian language and this did not work well with the example provided. Spent quite some hours experimenting with this gadget and had many attempts before finally solving it. I'm would like to share the rewritten example code here for others to enjoy. Right now it is set up for handling Norwegian characters, but you should be able to fix it for swedish, german, etc. if you experiment a bit. Good luck!

/*
 SMS sender

 This sketch, for the MKR GSM 1400 board,sends an SMS message
 you enter in the serial monitor. Connect your Arduino with the
 GSM shield and SIM card, open the serial monitor, and wait for
 the "READY" message to appear in the monitor. Next, type a
 message to send and press "return". Make sure the serial
 monitor is set to send a newline when you press return.

 Circuit:
 * MKR GSM 1400 board
 * Antenna
 * SIM card that can send SMS

 created 25 Feb 2012
 by Tom Igoe

 upgraded 05 Jun 2021
 by Eirik Øra, eirik.ora@gmail.com
*/

// Include the GSM library
#include <MKRGSM.h>

#include "arduino_secrets.h" 
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// initialize the library instance
GSM gsmAccess;
GSM_SMS sms;

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Sender");

  // connection state
  bool connected = false;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (!connected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");

  // Configure GSM Modem to send in northern Europe character set
  MODEM.send("AT+CSCS=\"8859-1\""); // Setting message format to ISO8859-1 used in the Nordics
  if (MODEM.waitForResponse(100) == 2) {
    connected = false;
    Serial.println("Problem setting message format to ISO8859-1");
  }
}

void loop() {

  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println(); 
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if ((inChar != '\r') && (int(inChar) != 195)) { // \r is carriage return and is ignored. 195 is first byte of nordic chars and ignored too
        result[i] = fixNordic(inChar);
        i++;
      }
    }
  }
}

/*
 * Fix encoding in single characters. Only Norwegian characters mapped right now.
 */
char fixNordic(char inChar){
  char result = inChar;
  if (int(result) == 166) { result = char(230); } //'æ'
  if (int(result) == 184) { result = char(248); } //'ø'
  if (int(result) == 165) { result = char(229); } //'å'
  
  if (int(result) == 134) { result = char(198); } //'Æ'
  if (int(result) == 152) { result = char(216); } //'Ø'
  if (int(result) == 133) { result = char(197); } //'Å'
  return result;
}

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