Problem connecting Sodaq sara N211 aff with orange SIM-card to allthingstalk [SOLVED]

Hi,

Has anyone succefully sent information from a sodaq (with orange SIM-card) to allthingstalk?
We were able to verify our ability to send udp by sending things to and receiving from an echo-server with the code below (from arduino IDE).
The Sodaq example code used to connect to allthingstalk doesn’t work with the parameters we got from allthingstalk.
Can anyone provide us with code that works for an orange SIM-card or with a way to send sensor data to an external server where the data is received or send to other devices using another protocol (ex. mqtt)?


#include "Sodaq_nbIOT.h"
#include "Sodaq_wdt.h"


#define VODAFONE_NL

/* SODAQ SARA */
#define DEBUG_STREAM SerialUSB
#define MODEM_STREAM Serial1
#define powerPin SARA_ENABLE
#define enablePin SARA_TX_ENABLE


#define DEBUG_STREAM SerialUSB
#define DEBUG_STREAM_BAUD 115200

#define STARTUP_DELAY 5000


const char* apn = "nbiot.iot"; // att.iot?
const char* cdp = "";
uint8_t cid = 1;
const uint8_t band = 20;
const char* forceOperator = "20610"; // optional - depends on SIM / network


Sodaq_nbIOT nbiot;

void sendMessageThroughUDP()
{
    DEBUG_STREAM.println();
    DEBUG_STREAM.println("Sending message through UDP");

    int localPort = 16666;
    int socketID = nbiot.createSocket(localPort);

    if (socketID >= 7 || socketID < 0) {
        DEBUG_STREAM.println("Failed to create socket");
        return;
    }

    DEBUG_STREAM.println("Created socket!");

    const char* strBuffer = "test";
    size_t size = strlen(strBuffer);

    int lengthSent = nbiot.socketSend(socketID, "195.34.89.241", 7, strBuffer); // "195.34.89.241" : 7 is the ublox echo service

    DEBUG_STREAM.print("String length vs sent: ");
    DEBUG_STREAM.print(size);
    DEBUG_STREAM.print(" vs ");
    DEBUG_STREAM.println(lengthSent);

    // wait for data
    if (nbiot.waitForUDPResponse()) {
        DEBUG_STREAM.println("Received response!");

        while (nbiot.hasPendingUDPBytes()) {
            char data[200];
            // read two bytes at a time
            SaraN2UDPPacketMetadata p;
            int size = nbiot.socketReceiveHex(data, 2, &p);

            if (size) {
                DEBUG_STREAM.println(data);
                // p is a pointer to memory that is owned by nbiot class
                DEBUG_STREAM.println(p.socketID);
                DEBUG_STREAM.println(p.ip);
                DEBUG_STREAM.println(p.port);
                DEBUG_STREAM.println(p.length);
                DEBUG_STREAM.println(p.remainingLength);
            }
            else {
                DEBUG_STREAM.println("Receive failed!");
            }
        }
    }
    else {
        DEBUG_STREAM.println("Timed-out!");
    }

    nbiot.closeSocket(socketID);
    DEBUG_STREAM.println();
}

void setup()
{
    sodaq_wdt_safe_delay(STARTUP_DELAY);

    DEBUG_STREAM.begin(DEBUG_STREAM_BAUD);
    DEBUG_STREAM.println("Initializing and connecting... ");

#ifdef R4XX
    MODEM_STREAM.begin(nbiot.getSaraR4Baudrate());
    nbiot.setDiag(DEBUG_STREAM);
    nbiot.init(MODEM_STREAM, powerPin, enablePin, SARA_R4XX_TOGGLE, cid);
#else // ik denk dat de SARA module niet N4X is 
    MODEM_STREAM.begin(nbiot.getDefaultBaudrate());
    nbiot.setDiag(DEBUG_STREAM);
    nbiot.init(MODEM_STREAM, powerPin, enablePin, -1, cid);
#endif               

#ifdef SARA_RESET
    pinMode(SARA_RESET, OUTPUT);    
    digitalWrite(SARA_RESET, HIGH);
#endif

#ifndef R4XX
    nbiot.overrideNconfigParam("CR_0354_0338_SCRAMBLING", true);
#endif

    if (!nbiot.connect(apn, cdp, forceOperator, band)) {
        DEBUG_STREAM.println("Failed to connect to the modem!");
    }

    sendMessageThroughUDP();
}

void loop()
{
    sodaq_wdt_safe_delay(60000);
    if (!nbiot.isConnected()) {
        if (!nbiot.connect(apn, cdp, forceOperator, band)) {
            DEBUG_STREAM.println("Failed to connect to the modem!");
        }
    }
    else {
        sendMessageThroughUDP();
    }
}

Hi,

I see that AllThingsTalk has a page about the Orange Sims here:
https://docs.allthingstalk.com/networks/use-orangebe-nbiot/

Hope it helps you!

Best regards,
Jan

Hi,

Sorry but it did not help much.
We contacted allthingstalk and gained some insight into how things work and what the problems could be.
Eventually my colleague figured it out.

Thanks anyway,

So to make the sketch below work you need at least these 2 libraries:

  • arduino-nbiot-sdk-master (which you can find on the allthingstalk website)
  • Sodaq_nbIOT
    You might need some other basic or Sodaq libraries, I’m not sure.

In the arduino-nbiot-sdk-master library you need to go into the keys file and adjust the DEVICE_ID and DEVICE_TOKEN to the ones from your device.

To make this sketch work you still need an asset named counter in the orange platform.

The problems with the Sodaq library where that functions and methods are tailored to Vodaphone.
This is fixed in the allthingstalk libraries but I’ll just add it here for information purposes.
These lines should be added if you are using the Sodaq library (not necesary for the sketch below):

const char* apn = "nbiot.iot"; // att.iot?
const char* cdp = "";
uint8_t cid = 1;
const uint8_t band = 20;
const char* forceOperator = "20610"; // optional - depends on SIM / network

The problem with the allthingstalk library was that not all the necesary pins were set right at the start.


SKETCH:

// Select your preferred method of sending data

#define JSON
//#define CBOR
//#define BINARY

#include "ATT_NBIOT.h"

// Mbili support
#define DEBUG_STREAM SerialUSB
#define MODEM_STREAM Serial1
#define MODEM_ON_OFF_PIN SARA_ENABLE 

#define powerPin SARA_ENABLE
#define enablePin SARA_TX_ENABLE
#define voltagePin SARA_R4XX_TOGGLE

ATT_NBIOT device;
/*
#ifdef CBOR
  #include <CborBuilder.h>
  CborBuilder payload(device);
#endif

#ifdef BINARY
  #include <PayloadBuilder.h>
  PayloadBuilder payload(device);
#endif
*/

void setup()
{
  delay(3000);

#ifdef powerPin
    // Put voltage on the nb-iot module
    pinMode(powerPin, OUTPUT);
    digitalWrite(powerPin, HIGH);
#endif

#ifdef R4XX
    // Switch module voltage
    pinMode(voltagePin, OUTPUT);
    digitalWrite(voltagePin, LOW);
#endif

#ifdef enablePin
    // Set state to active
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, HIGH);
#endif // enablePin

  DEBUG_STREAM.begin(57600);
  MODEM_STREAM.begin(9600);
  
  DEBUG_STREAM.println("Initializing and connecting... ");

  device.init(MODEM_STREAM, DEBUG_STREAM, MODEM_ON_OFF_PIN);
  
  if(device.connect())
    DEBUG_STREAM.println("Connected!");
  else
  {
    DEBUG_STREAM.println("Connection failed!");
    while(true) {}  // No connection. No need to continue the program
  }
}

int counter = 1;  // Initialize counter
unsigned long sendNextAt = 0;  // Keep track of time
void loop() 
{
  if(sendNextAt < millis())
  {
    // We will be using JSON 
    // You need to send a message for each asset
    // thus we wild send a GPS message and an Accelerometer message
    // our android app will then subscribe to both topics 
    // proces the data and make it into one coherent message 
    //
    // You can send String, integer boolean and doubles with sendMessage()
    
    #ifdef JSON  // Send data using regular json
    device.sendMessage(counter, "counter");
    device.sendMessage("Steek hier de GPS data","gps_data");
    device.sendMessage("Steek hier de acellerometer data", "accelerometer");
    #endif

    /*
    
    #ifdef CBOR  // Send data using Cbor
    payload.reset();
    payload.map(1);
    payload.addInteger(counter, "counter");
    payload.send();
    #endif

    #ifdef BINARY  // Send data using binary payload, make sure the decoding is set at AllThingsTalk
    payload.reset();
    payload.addInteger(counter);
    payload.send();
    #endif

    */

    counter++;
    sendNextAt = millis() + 10000;
  }
}