Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System events for cloud/network connection state changes #1151

merged 9 commits into from Nov 29, 2016


Copy link

commented Oct 30, 2016

This PR closes #977 by enabling generation of the system events for cloud and network connection state changes.

I'm not sure how to test this functionality automatically, here is the application I was playing with during my manual testing:

#include "application.h"


#define CHECK_STATE(data, state) \
        if ((data) == (state)) { \
  "Event: %s", #state); \
            return; \

namespace {

void connectionStateChanged(system_event_t event, int data) {
    if (event == network_status) {
        CHECK_STATE(data, network_status_powering_off);
        CHECK_STATE(data, network_status_off);
        CHECK_STATE(data, network_status_powering_on);
        CHECK_STATE(data, network_status_on);
        CHECK_STATE(data, network_status_connecting);
        CHECK_STATE(data, network_status_connected);
        CHECK_STATE(data, network_status_disconnecting);
        CHECK_STATE(data, network_status_disconnected);
    } else if (event == cloud_status) {
        CHECK_STATE(data, cloud_status_disconnected);
        CHECK_STATE(data, cloud_status_connecting);
        CHECK_STATE(data, cloud_status_connected);
        CHECK_STATE(data, cloud_status_disconnecting);

SerialLogHandler logHandler(LOG_LEVEL_WARN, {
    { "app", LOG_LEVEL_ALL }

uint32_t t = 0;
int step = 0;

} // namespace

void setup() {
    System.on(network_status | cloud_status, connectionStateChanged);

void loop() {
    if (step && millis() - t < 3000) {
    switch (++step) {
    case 1: {"-> WiFi.on()");
    case 2: {"-> WiFi.connect()");
    case 3: {"-> Particle.connect()");
    case 4: {"-> Particle.disconnect()");
    case 5: {"-> WiFi.disconnect()");
        WiFi.disconnect(); // Seems to cause automatic reconnection
    case 6: {"->");;
    t = millis();

Possible logging output:

0000000004 [app] INFO: -> WiFi.on()
0000000004 [app] INFO: Event: network_status_powering_on
0000000422 [app] INFO: Event: network_status_on
0000003422 [app] INFO: -> WiFi.connect()
0000003422 [app] INFO: Event: network_status_connecting
0000004835 [app] INFO: Event: network_status_connected
0000007835 [app] INFO: -> Particle.connect()
0000007835 [app] INFO: Event: cloud_status_connecting
0000009129 [app] INFO: Event: cloud_status_connected
0000010835 [app] INFO: -> Particle.disconnect()
0000010835 [app] INFO: Event: cloud_status_disconnecting
0000010842 [app] INFO: Event: cloud_status_disconnected
0000013835 [app] INFO: -> WiFi.disconnect()
0000013835 [app] INFO: Event: network_status_disconnecting
0000013844 [app] INFO: Event: network_status_disconnected
0000016852 [app] INFO: ->
0000016852 [app] INFO: Event: network_status_powering_off
0000016862 [app] INFO: Event: network_status_off

Note that in the current implementation cloud_status_connected event is generated only after a successful handshake, and no intermediate events are defined for TCP connection state. Also, network_status_connected event is generated only after device's network is successfully configured via DHCP (in case of dynamic IP configuration).


  • Contributor has signed CLA
  • Problem and Solution clearly stated
  • Code peer reviewed
  • API tests compiled
  • Run unit/integration/application tests on device
  • Add documentation
  • Add to after merging (add links to docs and issues)


  • Added System events for cloud/network connection state changes [Closes #977]

@sergeuz sergeuz added this to the 0.7.x milestone Oct 30, 2016

sergeuz added 2 commits Oct 30, 2016

@technobly technobly merged commit 9c06ab8 into develop Nov 29, 2016

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
continuous-integration/travis-ci/push The Travis CI build passed

@technobly technobly deleted the feature/conn_state_events branch Nov 29, 2016

@technobly technobly modified the milestones: 0.7.x, 0.6.1 Nov 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.