esp8266 wifi doesn’t connect

*SEE UPDATE BELOW*

I spent quite some time debugging an issue where the wifi wouldn’t connect in station mode on the esp8266 today. wifi_station_get_connect_status() would return either 1 or 3 (which means CONNECTING or AP_NOT_FOUND). The issue /appears/ to be that wifi_station_set_config does not work reliably when called outside user_init (even if called from a function called from user_init. I.e.

void ICACHE_FLASH_ATTR wifi_config() {
    // Wifi configuration
    char ssid[32] = SSID;
    char password[64] = SSID_PASSWORD;
    struct station_config stationConf;

    //Set station mode
    wifi_set_opmode( 0x1 );

    //Set ap settings
    os_memcpy(&stationConf.ssid, ssid, 32);
    os_memcpy(&stationConf.password, password, 64);
    wifi_station_set_config(&stationConf);
    //wifi_station_connect(); // should not be required
    debug("init wifi");
    debug(SSID);
    debug(SSID_PASSWORD);
}

//Init function
void ICACHE_FLASH_ATTR user_init() {

    // Set UART Speed (default appears to be rather odd 77KBPS)
    uart_init(BIT_RATE_9600,BIT_RATE_9600);

    wifi_config();

    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);
    system_os_post(user_procTaskPrio, 0, 0 );

    network_init();
}

Does NOT work consistantly. Whereas:

//Init function
void ICACHE_FLASH_ATTR user_init() {

    // Set UART Speed (default appears to be rather odd 77KBPS)
    uart_init(BIT_RATE_9600,BIT_RATE_9600);

    //wifi_config();
    // Wifi configuration
    char ssid[32] = SSID;
    char password[64] = SSID_PASSWORD;
    struct station_config stationConf;

    //Set station mode
    wifi_set_opmode( 0x1 );

    //Set ap settings
    os_memcpy(&stationConf.ssid, ssid, 32);
    os_memcpy(&stationConf.password, password, 64);
    wifi_station_set_config(&stationConf);
    //wifi_station_connect(); // should not be required
    debug("init wifi");
    debug(SSID);
    debug(SSID_PASSWORD);

    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);
    system_os_post(user_procTaskPrio, 0, 0 );

    network_init();
}

Works as expected. The reason that wifi_config() above may sometimes work is that I would guess it occasionally gets inlined. I’ve no idea why the calls need to be in user_init directly though, there’s perhaps some funky stack related stuff going on.

UPDATE

Wow, Espressif saw my post and emailed me out of the blue. There suggestion was that stationConf.bssid_set should also be set to 0 in this case. Otherwise it will also check the AP mac address i.e it should be:

void wifi_config()
{
// Wifi configuration
char ssid[32] = SSID;
char password[64] = PASSWORD;
struct station_config stationConf;

//Set station mode
wifi_set_opmode( 0x1 );

stationConf.bssid_set = 0;

//Set ap settings
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 64);
wifi_station_set_config(&stationConf);

}

//Init function
void user_init()
{
wifi_config();
}

This also makes sense to me in terms of random fiddling fixed this issue, I guess in some cases the uninitialized memory is 0 by chance.

esp8266 module types

I’ve been looking at the different ESP8266 modules that are available and have collected those I found here for reference. They seem to differ in terms of which pins are broken out, if a ceramic antenna, or PCB antenna is used, or if they have a connector for an external antenna. There seem to be quite a few options available…

Alibaba:
esp8266module_types

Ebay:
esp8266_module

Ebay:
esp8266_moremods

In addition to these, Olimex also have two custom modules. For which they’ve also kindly provided eagle files: find them here, pics below.
olimex_esp

olimex_esp2

There’s now also the “Wee” an XBee pin compatible board which looks pretty neat info here:
wee

A standalone sscanf implementation (for the esp8266)

The esp8266 standard SDK include a sprintf like function called os_sprintf. However there doesn’t seem to be a corresponding sscanf. I needed this for a quick and dirty parser I was using and so hacked the sscanf implementation out of the friendliest library I could find. This turned out to be pdclib, which has a permissive public domain license. The code is below, it’s pretty hideous but I’ve tested that it compiles on the esp8266 and at least parses the first value correctly under gcc! I’ll continue to update this post with any issues.

UPDATE: The original version didn’t support %f. I’ve update the code to include this (probably quite buggy). It still doesn’t support %d however.

//#include <limits.h>
#include "stdarg.h"
//#include <stdio.h>

#define UINT_MAX 4294967295U
/* Using an integer's bits as flags for both the conversion flags and length
   modifiers.
*/
#define E_suppressed 1<<0
#define E_char       1<<6
#define E_short      1<<7
#define E_long       1<<8
#define E_llong      1<<9
#define E_intmax     1<<10
#define E_size       1<<11
#define E_ptrdiff    1<<12
#define E_intptr     1<<13
#define E_ldouble    1<<14
#define E_unsigned   1<<16
#define E_float      1<<17
#define E_double     1<<18

#define SIZE_MAX 500
typedef unsigned int uintmax_t; 
typedef int intmax_t;
typedef unsigned int size_t;
typedef int ptrdiff_t;

#define EOF -1
#define NULL 0
#define bool int
#define true 1
#define false 0

char _PDCLIB_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz.";

int isspace(int c) { 
  if(c == ' ' ) return true;
  if(c == '\t') return true;

  return false;
}

/* Status structure required by _PDCLIB_print(). */
struct _PDCLIB_status_t
{
    /* XXX This structure is horrible now. scanf needs its own */

    int              base;   /* base to which the value shall be converted   */
    unsigned int flags; /* flags and length modifiers                */
    unsigned         n;      /* print: maximum characters to be written (snprintf) */
                             /* scan:  number matched conversion specifiers  */
    unsigned         i;      /* number of characters read/written            */
    unsigned         current;/* chars read/written in the CURRENT conversion */
    unsigned         width;  /* specified field width                        */
    int              prec;   /* specified field precision                    */

    const char *     s;      /* input string for scanf */

    va_list  arg;    /* argument stack                               */
};


void * nmemchr( const void * s, int c, size_t n ) {
    const unsigned char * p = (const unsigned char *) s;
    while ( n-- )
    {
        if ( *p == (unsigned char) c )
        {
            return (void *) p;
        }
        ++p;
    }
    return NULL;
}


/* Helper function to get a character from the string or stream, whatever is
   used for input. When reading from a string, returns EOF on end-of-string
   so that handling of the return value can be uniform for both streams and
   strings.
*/
static int GET( struct _PDCLIB_status_t * status )
{
    int rc = EOF;
    rc = ( *status->s == '\0' ) ? EOF : (unsigned char)*((status->s)++);
    if ( rc != EOF )
    {
        ++(status->i);
        ++(status->current);
    }
    return rc;
}


/* Helper function to put a read character back into the string or stream,
   whatever is used for input.
*/
static void UNGET( int c, struct _PDCLIB_status_t * status )
{
    --(status->s);
    --(status->i);
    --(status->current);
}


/* Helper function to check if a character is part of a given scanset */
static bool IN_SCANSET( const char * scanlist, const char * end_scanlist, int rc )
{
    // SOLAR
    int previous = -1;
    while ( scanlist != end_scanlist )
    {
        if ( ( *scanlist == '-' ) && ( previous != -1 ) )
        {
            /* possible scangroup ("a-z") */
            if ( ++scanlist == end_scanlist )
            {
                /* '-' at end of scanlist does not describe a scangroup */
                return rc == '-';
            }
            while ( ++previous <= (unsigned char)*scanlist )
            {
                if ( previous == rc )
                {
                    return true;
                }
            }
            previous = -1;
        }
        else
        {
            /* not a scangroup, check verbatim */
            if ( rc == (unsigned char)*scanlist )
            {
                return true;
            }
            previous = (unsigned char)(*scanlist++);
        }
    }
    return false;
}


const char * _PDCLIB_scan(const char * spec, struct _PDCLIB_status_t * status )
{
    /* generic input character */
    int rc;
    const char * orig_spec = spec;
    if ( *(++spec) == '%' )
    {
        /* %% -> match single '%' */
        rc = GET( status );
        switch ( rc )
        {
            case EOF:
                /* input error */
                if ( status->n == 0 )
                {
                    status->n = -1;
                }
                return NULL;
            case '%':
                return ++spec;
            default:
                UNGET( rc, status );
                break;
        }
    }
    /* Initializing status structure */
    status->flags = 0;
    status->base = -1;
    status->current = 0;
    status->width = 0;
    status->prec = 0;

    /* '*' suppresses assigning parsed value to variable */
    if ( *spec == '*' )
    {
        status->flags |= E_suppressed;
        ++spec;
    }

    /* If a width is given, strtol() will return its value. If not given,
       strtol() will return zero. In both cases, endptr will point to the
       rest of the conversion specifier - just what we need.
    */
    char const * prev_spec = spec;
    status->width = (int)strtol( spec, (char**)&spec, 10 );
    if ( spec == prev_spec )
    {
        status->width = UINT_MAX;
    }

    /* Optional length modifier
       We step one character ahead in any case, and step back only if we find
       there has been no length modifier (or step ahead another character if it
       has been "hh" or "ll").
    */
    switch ( *(spec++) )
    {
        case 'h':
            if ( *spec == 'h' )
            {
                /* hh -> char */
                status->flags |= E_char;
                ++spec;
            }
            else
            {
                /* h -> short */
                status->flags |= E_short;
            }
            break;
        case 'l':
            if ( *spec == 'l' )
            {
                /* ll -> long long */
                status->flags |= E_llong;
                ++spec;
            }
            else
            {
                /* l -> long */
                status->flags |= E_long;
            }
            break;
        case 'j':
            /* j -> intmax_t, which might or might not be long long */
            status->flags |= E_intmax;
            break;
        case 'z':
            /* z -> size_t, which might or might not be unsigned int */
            status->flags |= E_size;
            break;
        case 't':
            /* t -> ptrdiff_t, which might or might not be long */
            status->flags |= E_ptrdiff;
            break;
        case 'L':
            /* L -> long double */
            status->flags |= E_ldouble;
            break;
        default:
            --spec;
            break;
    }

    /* Conversion specifier */

    /* whether valid input had been parsed */
    bool value_parsed = false;

    switch ( *spec )
    {
        case 'd':
            status->base = 10;
            break;
        case 'i':
            status->base = 0;
            break;
        case 'o':
            status->base = 8;
            status->flags |= E_unsigned;
            break;
        case 'u':
            status->base = 10;
            status->flags |= E_unsigned;
            break;
        case 'x':
            status->base = 16;
            status->flags |= E_unsigned;
            break;
        case 'f':
            status->flags |= E_float;
            status->base = 10;
            break;
        case 'F':
        case 'e':
        case 'E':
        case 'g':
        case 'G':
        case 'a':
        case 'A':
            break;
        case 'c':
        {
            char * c = va_arg( status->arg, char * );
            /* for %c, default width is one */
            //if ( status->width == SIZE_MAX )
            if ( status->width == -1 )
            {
                status->width = 1;
            }
            /* reading until width reached or input exhausted */
            while ( ( status->current < status->width ) &&
                    ( ( rc = GET( status ) ) != EOF ) )
            {
                *(c++) = rc;
                value_parsed = true;
            }
            /* width or input exhausted */
            if ( value_parsed )
            {
                ++status->n;
                return ++spec;
            }
            else
            {
                /* input error, no character read */
                if ( status->n == 0 )
                {
                    status->n = -1;
                }
                return NULL;
            }
        }
        case 's':
        {
            char * c = va_arg( status->arg, char * );
            while ( ( status->current < status->width ) &&
                    ( ( rc = GET( status ) ) != EOF ) )
            {
                if ( isspace( rc ) )
                {
                    UNGET( rc, status );
                    if ( value_parsed )
                    {
                        /* matching sequence terminated by whitespace */
                        *c = '\0';
                        ++status->n;
                        return ++spec;
                    }
                    else
                    {
                        /* matching error */
                        return NULL;
                    }
                }
                else
                {
                    /* match */
                    value_parsed = true;
                    *(c++) = rc;
                }
            }
            /* width or input exhausted */
            if ( value_parsed )
            {
                *c = '\0';
                ++status->n;
                return ++spec;
            }
            else
            {
                /* input error, no character read */
                if ( status->n == 0 )
                {
                    status->n = -1;
                }
                return NULL;
            }
        }
        case '[':
        {
            const char * endspec = spec;
            bool negative_scanlist = false;
            if ( *(++endspec) == '^' )
            {
                negative_scanlist = true;
                ++endspec;
            }
            spec = endspec;
            do
            {
                // TODO: This can run beyond a malformed format string
                ++endspec;
            } while ( *endspec != ']' );
            // read according to scanlist, equiv. to %s above
            char * c = va_arg( status->arg, char * );
            while ( ( status->current < status->width ) &&
                    ( ( rc = GET( status ) ) != EOF ) )
            {
                if ( negative_scanlist )
                {
                    if ( IN_SCANSET( spec, endspec, rc ) )
                    {
                        UNGET( rc, status );
                        break;
                    }
                }
                else
                {
                    if ( ! IN_SCANSET( spec, endspec, rc ) )
                    {
                        UNGET( rc, status );
                        break;
                    }
                }
                value_parsed = true;
                *(c++) = rc;
            }
            if ( value_parsed )
            {
                *c = '\0';
                ++status->n;
                return ++endspec;
            }
            else
            {
                if ( rc == EOF )
                {
                    status->n = -1;
                }
                return NULL;
            }
        }
        case 'p':
            status->base = 16;
            // TODO: Like _PDCLIB_print, E_pointer(?)
            status->flags |= E_unsigned | E_long;
            break;
        case 'n':
        {
            int * val = va_arg( status->arg, int * );
            *val = status->i;
            return ++spec;
        }
        default:
            /* No conversion specifier. Bad conversion. */
            return orig_spec;
    }

    if ( status->base != -1 )
    {
        int dp_count=0;
        bool dp_seen = false;
        /* integer conversion */
        uintmax_t value   = 0;         /* absolute value read */
        double    value_f = 0;         /* absolute value read */
        bool prefix_parsed = false;
        int sign = 0;
        while ( ( status->current < status->width ) &&
                ( ( rc = GET( status ) ) != EOF ) )
        {
            if ( isspace( rc ) )
            {
                if ( sign )
                {
                    /* matching sequence terminated by whitespace */
                    UNGET( rc, status );
                    break;
                }
                else
                {
                    /* leading whitespace not counted against width */
                    status->current--;
                }
            }
            else if ( ! sign )
            {
                /* no sign parsed yet */
                switch ( rc )
                {
                    case '-':
                        sign = -1;
                        break;
                    case '+':
                        sign = 1;
                        break;
                    default:
                        /* not a sign; put back character */
                        sign = 1;
                        UNGET( rc, status );
                        break;
                }
            }
            else if ( ! prefix_parsed )
            {
                /* no prefix (0x... for hex, 0... for octal) parsed yet */
                prefix_parsed = true;
                if ( rc != '0' )
                {
                    /* not a prefix; if base not yet set, set to decimal */
                    if ( status->base == 0 )
                    {
                        status->base = 10;
                    }
                    UNGET( rc, status );
                }
                else
                {
                    /* starts with zero, so it might be a prefix. */
                    /* check what follows next (might be 0x...) */
                    if ( ( status->current < status->width ) &&
                         ( ( rc = GET( status ) ) != EOF ) )
                    {
                        if ( tolower( rc ) == 'x' )
                        {
                            /* 0x... would be prefix for hex base... */
                            if ( ( status->base == 0 ) ||
                                 ( status->base == 16 ) )
                            {
                                status->base = 16;
                            }
                            else
                            {
                                /* ...unless already set to other value */
                                UNGET( rc, status );
                                value_parsed = true;
                            }
                        }
                        else
                        {
                            /* 0... but not 0x.... would be octal prefix */
                            UNGET( rc, status );
                            if ( status->base == 0 )
                            {
                                status->base = 8;
                            }
                            /* in any case we have read a zero */
                            value_parsed = true;
                        }
                    }
                    else
                    {
                        /* failed to read beyond the initial zero */
                        value_parsed = true;
                        break;
                    }
                }
            }
            else
            {

                char * digitptr = nmemchr( _PDCLIB_digits, tolower( rc ), status->base );
                if(rc != '.') {
                  if ( digitptr == NULL ) {
                      /* end of input item */
                      UNGET( rc, status );
                      break;
                  } else {
                    value *= status->base;
                    value += digitptr - _PDCLIB_digits;

                    value_f *= status->base;
                    value_f += digitptr - _PDCLIB_digits;
                  }
                } else {
                  dp_seen = true;
                }
                if(dp_seen) dp_count++;
                value_parsed = true;
            }
        }
        /* width or input exhausted, or non-matching character */
        if ( ! value_parsed )
        {
            /* out of input before anything could be parsed - input error */
            /* FIXME: if first character does not match, value_parsed is not set - but it is NOT an input error */
            if ( ( status->n == 0 ) && ( rc == EOF ) )
            {
                status->n = -1;
            }
            return NULL;
        }
        /* convert value to target type and assign to parameter */
        if ( ! ( status->flags & E_suppressed ) )
        {
            switch ( status->flags & ( E_char | E_short | E_long | E_llong |
                                       E_intmax | E_size | E_ptrdiff | E_float |
                                       E_unsigned ) )
            {
                case E_char:
                    *( va_arg( status->arg,               char * ) ) =               (char)( value * sign );
                    break;
                case E_char | E_unsigned:
                    *( va_arg( status->arg,      unsigned char * ) ) =      (unsigned char)( value * sign );
                    break;

                case E_short:
                    *( va_arg( status->arg,              short * ) ) =              (short)( value * sign );
                    break;
                case E_short | E_unsigned:
                    *( va_arg( status->arg,     unsigned short * ) ) =     (unsigned short)( value * sign );
                    break;

                case 0:
                    *( va_arg( status->arg,                int * ) ) =                (int)( value * sign );
                    break;
                case E_unsigned:
                    *( va_arg( status->arg,       unsigned int * ) ) =       (unsigned int)( value * sign );
                    break;

                case E_long:
                    *( va_arg( status->arg,               long * ) ) =               (long)( value * sign );
                    break;
                case E_long | E_unsigned:
                    *( va_arg( status->arg,      unsigned long * ) ) =      (unsigned long)( value * sign );
                    break;

                case E_llong:
                    *( va_arg( status->arg,          long long * ) ) =          (long long)( value * sign );
                    break;
                case E_llong | E_unsigned:
                    *( va_arg( status->arg, unsigned long long * ) ) = (unsigned long long)( value * sign );
                    break;

                case E_intmax:
                    *( va_arg( status->arg,           intmax_t * ) ) =           (intmax_t)( value * sign );
                    break;
                case E_float:
                    {
                      int n;
                      for(n=1;n<dp_count;n++) value_f = value_f/10;
                      *( va_arg( status->arg,           float * ) ) =           (float)( (value_f * sign) );
                    }
                    break;
                case E_intmax | E_unsigned:
                    *( va_arg( status->arg,          uintmax_t * ) ) =          (uintmax_t)( value * sign );
                    break;

                case E_size:
                    /* E_size always implies unsigned */
                    *( va_arg( status->arg,             size_t * ) ) =             (size_t)( value * sign );
                    break;

                case E_ptrdiff:
                    /* E_ptrdiff always implies signed */
                    *( va_arg( status->arg,          ptrdiff_t * ) ) =          (ptrdiff_t)( value * sign );
                    break;

                default:
                    //puts( "UNSUPPORTED SCANF FLAG COMBINATION" );
                    return NULL; /* behaviour unspecified */
            }
            ++(status->n);
        }
        return ++spec;
    }
    /* TODO: Floats. */
    return NULL;
}

int nsscanf( const char *s, const char *format, ... ) {
    /* TODO: This function should interpret format as multibyte characters.  */
    struct _PDCLIB_status_t status;
    status.base = 0;
    status.flags = 0;
    status.n = 0;
    status.i = 0;
    status.current = 0;
    status.s = (char *) s;
    status.width = 0;
    status.prec = 0;

    va_list ap;
    va_start(ap,format);
    va_copy( status.arg, ap );

    while ( *format != '\0' )
    {
        const char * rc;
        if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) )
        {
            /* No conversion specifier, match verbatim */
            if ( isspace( *format ) )
            {
                /* Whitespace char in format string: Skip all whitespaces */
                /* No whitespaces in input do not result in matching error */
                while ( isspace( *status.s ) )
                {
                    ++status.s;
                    ++status.i;
                }
            }
            else
            {
                /* Non-whitespace char in format string: Match verbatim */
                if ( *status.s != *format )
                {
                    if ( *status.s == '\0' && status.n == 0 )
                    {
                        /* Early input error */
                        return EOF;
                    }
                    /* Matching error */
                    return status.n;
                }
                else
                {
                    ++status.s;
                    ++status.i;
                }
            }
            ++format;
        }
        else
        {
            /* NULL return code indicates error */
            if ( rc == NULL )
            {
                if ( ( *status.s == '\n' ) && ( status.n == 0 ) )
                {
                    status.n = EOF;
                }
                break;
            }
            /* Continue parsing after conversion specifier */
            format = rc;
        }
    }
    va_end( status.arg );
    return status.n;
}

Safecast NEMA format notes (bGeigieNano)

Safecast use a modified NEMA format on the bGeigieNano for writing logfiles, and when dumping data over the serial port to the XBee interface. I need to parse this for a project, so these are my notes on the format. The line of code that generates the NEMA string follows, and is found here:

  sprintf_P(buf, PSTR("$%s,%04d,%02d-%02d-%02dT%02d:%02d:%02dZ,%ld,%ld,%ld,%c,%s,%c,%s,%c,%s,%c,%d,%ld"),  \
              NANO_HEADER, \
              config.device_id, \
              year, month, day,  \
              hour, minute, second, \
              cpm, \
              cpb, \
              total_count, \
              geiger_status, \
              lat, NS,\
              lon, WE,\
              strbuffer, \
              gps_status, \
              nbsat  == TinyGPS::GPS_INVALID_SATELLITES ? 0 : nbsat, \
              precission == TinyGPS::GPS_INVALID_HDOP ? 0 : precission);

Here are a few example strings I captured (though in these strings the reading is marked as VOID):

$BNRDD,0000,2015-01-06T13:49:32Z,0,0,0,V,0000.0000,N,00000.0000,E,0.00,V,0,0*4A
$BNRDD,0000,2015-01-06T13:49:55Z,0,0,0,V,3537.2793,N,13938.0878,E,110.60,A,3,558B
$BNRDD,0000,2015-01-06T13:50:23Z,0,0,0,V,3537.2776,N,13938.0777,E,105.20,A,4,3036
$BNRDD,0000,2015-01-06T13:52:58Z,15,15,15,V,3537.2776,N,13938.0735,E,100.80,A,4,3
$BNRDD,0000,2015-01-06T13:53:05Z,28,13,28,V,3537.2778,N,13938.0740,E,100.70,A,4,F
$BNRDD,0000,2015-01-06T13:53:10Z,33,5,33,V,3537.2779,N,13938.0744,E,100.70,A,4,39
$BNRDD,0000,2015-01-06T13:53:16Z,89,56,89,V,3537.2776,N,13938.0740,E,100.60,A,4,0
$BNRDD,0000,2015-01-06T13:53:21Z,128,39,128,V,3537.2771,N,13938.0732,E,100.60,A,F
$BNRDD,0000,2015-01-06T13:53:27Z,128,0,128,V,3537.2777,N,13938.0743,E,100.50,A,40
$BNRDD,0000,2015-01-06T13:53:32Z,128,0,128,V,3537.2775,N,13938.0751,E,100.30,A

The format can be summarized as follows (values in brackets indicate variables):

$BNRDD,[4digit_device_id],[ISO8601_time],[cpm],[cpb],[total_count],[geiger_status],[lat],[NS],[long],[WE],[altitude],[gps_status],[nbsat],[precision]

4digit_device_id: This defaults to 0000, but can be set from “did” in SAFECAST.TXT on the SD card.
cpm: Count per minute. I don’t believe this is the count over the last min, but rather an average using a variable window size dependent on the current rate.
cpb: As I understand “count per bin”. I think count in the last time period (last second by default?)
total_count: Total event count since the device was turned on.
geiger_status: The device needs to acquire at least 12 windows of data until it is considered valid. “V” for VOID or “A” for available.
lat: Latitude float, 4.4
NS: N or S, North or South
long: Longitude 4.4
WE: W or E, West or East
altitude: altitude
gps_status: “V” for VOID or “A” for available.
nbsat: Satellite count
precision: GPS precision

There’s also a Safecast JSON format. This is used by the Safecast webservice API to post data. It looks like this:

{"longitude":"111.1111","latitude":"11.1111","device_id":"47","value":"63","unit":"cpm"}

The following C function converts from NEMA to JSON format, it also adds a “captured_at” JSON field with the time shown in the NEMA string. captured_at is used by the safecast API when measurements are download, but I don’t think it’s normal present when a measurement is posted. The function also returns true if the measurement is valid and false if it’s not. The code has only been briefly tested, please comment with fixes:

#include <stdio.h>
#include <string.h>

// json_string needs to be preallocated and large enough to hold the output
int safecast_nema2json(const char *nema_string,char *json_string) {

  int    device_id;
  char   iso_timestr[50];
  int    cpm;
  int    cpb;
  int    total_count;
  char   geiger_status;
  float  latitude;
  char   NorS;
  float  longitude;
  char   WorE;
  float  altitude;
  char   gps_status;
  int    nbsat;
  char    precision[50];

  iso_timestr[0]=0;

  nsscanf(nema_string,
         "$BNRDD,%04d,%[^,],%d,%d,%d,%c,%f,%c,%f,%c,%f,%c,%d,%s",
         &device_id,
         iso_timestr,
         &cpm,
         &cpb,
         &total_count,
         &geiger_status,
         &latitude,
         &NorS,
         &longitude,
         &WorE,
         &altitude,
         &gps_status,
         &nbsat,
         precision);

  if(NorS == 'S') latitude  = 0-latitude;
  if(WorE == 'W') longitude = 0-longitude;

  sprintf(json_string,"{\"captured_at\":\"%s\",\"device_id\":\"%d\",\"value\":\"%d\",\"unit\":\"cpm\", \"longitude\":\"%f\", \"latitude\":\"%f\"  }\n", iso_timestr, device_id,cpm, longitude,latitude);

  if((gps_status == 'A') && (geiger_status == 'A')) return 1;
                                               else return 0;
}

int main() {

  char json[1024];

  int safecast_nema_valid = safecast_nema2json("$BNRDD,0101,2015-01-06T15:11:28Z,11,12,128,V,3537.2618,N,13938.0256,E,40.70,A,5,138*64",json);

  if( safecast_nema_valid) printf("VALIDJSON: %s\n",json);
  if(!safecast_nema_valid) printf("INVALIDJSON: %s\n",json);
}