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();
}
}