Dragos Iosub
Published

Taste the NB-IoT (BC95G) Flavor - Episode 1

NB-IoT data transmission how to use BC95G modem based shields - UDP test and network status signaling.

IntermediateProtip30 minutes1,085
Taste the NB-IoT (BC95G) Flavor - Episode 1

Things used in this project

Hardware components

xyz-mIoT by itbrainpower.net [ARM0 + BC95G (NB IoT modem), no embedded sensors]
xyz-mIoT by itbrainpower.net [ARM0 + BC95G (NB IoT modem), no embedded sensors]
×1
GSM antenna with uFL connector
×1
capacitor 1000-2200uF/6.3V low ESR
×1
nano SIM card from your NB IoT provider
×1

Software apps and online services

Arduino IDE
Arduino IDE
version >=1.8.5

Story

Read more

Custom parts and enclosures

xyz-mIoT shield mechanical drawing

xyz-mIoT board - ARM0 + sensors + modem compact shield - mechanical drawing and dimensions

Schematics

xyz-mIoT shield block schematics

xyz-mIoT board - ARM0 + sensors + modem compact shield - block schematics

xyz-mIoT shield pinout, port map and more

xyz-mIoT board - ARM0 + sensors + modem compact shield - pinout, port map and more info

Code

xyz_mIoT_NBIoT_Class_example_UDP_echo.ino

Arduino
Arduino code for NB IoT status detection (ENTER / LEAVE ACTIVE, IDLE and PSM modes) and RAW UDP data transmission. Written for itbrainpower.net shields equipped with Quectel BC95G modem.
/*
xyz_mIoT_NBIoT_Class_example_UDP_echo.ino v 0.1/20180915 - UDP SEND RECEIVE SOFTWARE EXAMPLE
COPYRIGHT (c) 2015-2018 Dragos Iosub / R&D Software Solutions srl

You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH xyz-mIOT by itbrainpower.net or u-GSM by itbrainpower.net DEVICES USAGE. Modifications, 
derivates and redistribution of this software must include unmodified this COPYRIGHT NOTICE. You can redistribute this SOFTWARE and/or modify it under the 
terms of this COPYRIGHT NOTICE. Any other usage may be permited only after written notice of Dragos Iosub / R&D Software Solutions srl.

This SOFTWARE is distributed is provide "AS IS" in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Dragos Iosub, Bucharest 2018.
https://itbrainpower.net
*/
/*IMPORTANT NOTICE 0: install and run "udp_echo.py" listner on your server; write down, for use in step 4, the IP address and UDP listener PORT */
/*IMPORTANT NOTICE 1: check itbpGSMdefinition.h for line 60&61 and set "__itbpModem__    xyzmIoT" or "__itbpModem__    uGSM"*/
/*IMPORTANT NOTICE 2: check itbpGSMdefinition.h for line 64&65 and set "__Qmodule__    BC95G" or "__Qmodule__    BG96"*/
/*IMPORTANT NOTICE 3: in itbpGPRSIPdefinition.h update your NB IoT "APN" and "numeric network id code" [Eg. Vodafone RO  ==> 22601]*/
/*IMPORTANT NOTICE 4: in itbpGPRSIPdefinition.h  update your "SERVER_ADDRESS" and "SERVER_PORT", from step 0*/
/*IMPORTANT NOTICE 5: in itbpGSMdefinition.h "itbpGSMClassDebug" and "atDebug" may help you....*/

#define DATAGRAMTOSEND "Mary have a little lamb...."      //what you will send to the ECHO server

/*REST cloud specific, more parameters in itbpGPRSIPdefinition.h*/
int samplingPeriod = 600;							                    //interval in seconds between transmissions... keep it > 60 sec [600 second to test some NB IoT signaling]

char IMEI [16];		

#include <itbpNBIoTClass_v0_1.h>

#define DEBUG(x)    {__itbpDebugPort__.print(millis());__itbpDebugPort__.print(" - ");__itbpDebugPort__.println(x);}
#define DEBUGP(x,y) {__itbpDebugPort__.print(millis());__itbpDebugPort__.print(" - ");__itbpDebugPort__.print(x);__itbpDebugPort__.println(y);}

#if defined (__AVR_ATmega328P__)
  int freeRam () {
    extern int __heap_start, *__brkval; 
    int v; 
    return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
  }
#endif

// initialize the library instance
aGsmClient client;



unsigned long startTime = 0;

bool ledState = 0;


void setup(){
#if (__itbpModem__ == xyzmIoT)
	delay(10000);
#endif

  __itbpDebugPort__.begin(115200);			//start debug port connection
  //configure led
	pinMode(LED_BUILTIN, OUTPUT);			            //embedded LED status indicator  
	digitalWrite(LED_BUILTIN, LOW);			
  ledState = 0;

#if defined (__AVR_ATmega328P__)
  DEBUGP(F("Free RAM: "), freeRam());
#endif

__itbpDebugPort__.println(F("Simple NBIoT UDP echo example...let's rock"));
__itbpDebugPort__.flush();
	
	client.begin();

	client.enableClockUpdate(1); //update available at next modem boot

	client.getIMEI();	//IMEI value in client.resp
	memset(IMEI,0x00, sizeof(IMEI));
	strcpy(IMEI,client.resp);
	DEBUGP(F("IMEI: "), IMEI);

	client.attachGPRS();

  //let's verify the module date&time function
  delay(1000);
  client.getRTC();
  DEBUGP(F("time and date: "), client.resp);

	__itbpDebugPort__.println("connecting...");
	
	//client.readServerDatagramReply();

  startTime = millis() + (unsigned long)samplingPeriod*1000;//force send on the spot
}




void loop(){
  int res=0;
  if (millis() - startTime < (unsigned long)samplingPeriod*1000){//waiting period  
    ledState = ! ledState;
    if(ledState != 0)
      digitalWrite(LED_BUILTIN, HIGH);  
    else
      digitalWrite(LED_BUILTIN, LOW); 
    delay(500);
    
  }else{//start transmission
    startTime = millis();
    digitalWrite(LED_BUILTIN, HIGH);      //show sampling + uploadto cloud

    if (client.connected() || client.connect()){
      __itbpDebugPort__.println(F("send datagram"));
      res = client.sendDatagram(DATAGRAMTOSEND); 
      if(res == 1){
        __itbpDebugPort__.println(F("wait 4 server reply..."));
        unsigned long startlisten = millis();
        res = client.listenForNetworkSignaling(samplingPeriod, 1);//wait until receive server reply (samplingPeriod); //wait for server reply; res
        if(res>0){ 
            __itbpDebugPort__.println("");
            __itbpDebugPort__.print(F("reply received>>"));
            __itbpDebugPort__.print(client.resp);    //server replied data in resp
            __itbpDebugPort__.println(F("<<"));
            //here wait for network signaling
            res = client.listenForNetworkSignaling(samplingPeriod -(int)((millis()-startlisten)/1000), 4);//wait until enter PSM mode        
        }
        /*
        //other variant to send
        res = client.listenForDatagramServerReply(samplingPeriod); //wait for server reply; res
        if(res>0){ 
            __itbpDebugPort__.println("");
            __itbpDebugPort__.print(F("reply received>>"));
            __itbpDebugPort__.print(client.resp);    //server replied data in resp
            __itbpDebugPort__.println(F("<<"));
            //here wait for network signaling
            //res = client.listenForNetworkSignaling(samplingPeriod - res, 4);//wait until ender PSM mode
        }
        */
      }else{//send datagram command report ERROR
        __itbpDebugPort__.println(F("connectivity error...")); 
      }
    }//end connect()
  }//end tranmission 
  
}

Credits

Dragos Iosub

Dragos Iosub

28 projects • 18 followers
Electronics & software, electronics & software, ...

Comments