Statistics
| Branch: | Tag: | Revision:

mongoose / examples / PIC32 / mqtt_client / firmware / src / app.c @ eaef5bd1

History | View | Annotate | Download (5.25 KB)

1
/*
2
 * Copyright (c) 2016 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "app.h"
7
#include "system/debug/sys_debug.h"
8
#include "./../../../../../mongoose.h"
9
#include "user_params.h"
10

    
11
static struct mg_mgr mgr;
12
static int msg_id;
13
struct mg_mqtt_topic_expression topic_expressions[] = {{"/test", 0}};
14

    
15
APP_DATA appData;
16

    
17
void APP_Initialize(void) {
18
  appData.state = APP_STATE_INIT;
19
  DRV_RTCC_Start();
20
  mg_mgr_init(&mgr, NULL);
21
}
22

    
23
static void ev_handler(struct mg_connection *nc, int ev, void *p);
24

    
25
void APP_Tasks(void) {
26
  /*
27
   * Processing CC3100 Tasks
28
   * It looks like SYS task and might be processed in SYS_Tasks
29
   * But for this demo it is here to make this call
30
   * visible
31
   */
32
  _SlNonOsMainLoopTask();
33

    
34
  /* Check the application's current state. */
35
  switch (appData.state) {
36
    /* Application's initial state. */
37
    case APP_STATE_INIT: {
38
      SYS_PRINT("\n\r*** PIC32 MQTT CLIENT ***\n\r");
39

    
40
      SYS_PRINT("\n\rInitializing CC3100\n\r");
41
      int res = sl_Start(NULL, NULL, NULL);
42
      if (res != 0) {
43
        SYS_PRINT("FAILED\n\r");
44
        appData.state = APP_STATE_DONE;
45
        break;
46
      }
47

    
48
      SlSecParams_t sec_params;
49
      memset(&sec_params, 0, sizeof(sec_params));
50
      sec_params.Key = NET_PWD;
51
      sec_params.KeyLen = sizeof(NET_PWD) - 1;
52
      sec_params.Type = NET_SECURITY;
53

    
54
      SYS_PRINT("Connecting to WiFi\n\r");
55

    
56
      sl_WlanConnect(NET_SSID, sizeof(NET_SSID) - 1, 0, &sec_params, NULL);
57

    
58
      SYS_PRINT("Initialization done\n\r");
59

    
60
      appData.state = APP_STATE_SERVICE_TASKS;
61

    
62
      break;
63
    }
64

    
65
    case APP_STATE_CONNECT_BROKER: {
66
      if (mg_connect(&mgr, MQTT_BROKER_ADDRESS, ev_handler) == NULL) {
67
        SYS_PRINT("Failed to create connection\n\r");
68
        appData.state = APP_STATE_DONE;
69
      } else {
70
        appData.state = APP_STATE_SERVICE_TASKS;
71
      }
72
      break;
73
    }
74
    case APP_STATE_SERVICE_TASKS: {
75
      static uint32_t prev_poll_time = 0;
76
      uint32_t now = DRV_RTCC_TimeGet();
77
      if (now - prev_poll_time > 100) {
78
        /*
79
         * We cannot call mg_mgr_poll every cycle
80
         * it leads to SPI overload (internaly mg_mgr_poll calls
81
         * CC3100 via SPI
82
         */
83
        mg_mgr_poll(&mgr, 1);
84
        prev_poll_time = now;
85
      }
86
      break;
87
    }
88

    
89
    case APP_STATE_DONE: {
90
      /* Do nothing here */
91
      break;
92
    }
93
    default: {
94
      /* TODO: Handle error in application's state machine. */
95
      break;
96
    }
97
  }
98
}
99

    
100
void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) {
101
  switch (pWlanEvent->Event) {
102
    case SL_WLAN_CONNECT_EVENT:
103
      SYS_PRINT("Connected to WiFi\r\n");
104
      break;
105
    case SL_WLAN_DISCONNECT_EVENT:
106
      SYS_PRINT("Disconnected WiFi\r\n");
107
      break;
108
    default:
109
      SYS_PRINT("Got Wlan event %d\r\n", pWlanEvent->Event);
110
      break;
111
  }
112
}
113

    
114
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent) {
115
  switch (pNetAppEvent->Event) {
116
    case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
117
      SYS_PRINT("Got IP\r\n");
118
      appData.state = APP_STATE_CONNECT_BROKER;
119
      break;
120
    default:
121
      SYS_PRINT("Got NetApp Event: %d\r\n", pNetAppEvent->Event);
122
      break;
123
  }
124
}
125

    
126
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
127
  struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
128
  static int count1 = 0;
129
  switch (ev) {
130
    case MG_EV_CONNECT:
131
      if (*(int *) p != 0) {
132
        SYS_PRINT("Failed to connect to %s\r\n", MQTT_BROKER_ADDRESS);
133
      } else {
134
        SYS_PRINT("Connected to %s\r\n", MQTT_BROKER_ADDRESS);
135
      }
136
      struct mg_send_mqtt_handshake_opts opts;
137
      memset(&opts, 0, sizeof(opts));
138
      opts.user_name = MQTT_USER_NAME;
139
      opts.password = MQTT_USER_PWD;
140
      mg_set_protocol_mqtt(nc);
141
      mg_send_mqtt_handshake_opt(nc, "PIC32", opts);
142
      break;
143
    case MG_EV_MQTT_CONNACK:
144
      if (msg->connack_ret_code != MG_EV_MQTT_CONNACK_ACCEPTED) {
145
        SYS_PRINT("Got mqtt connection error %d\n\r", msg->connack_ret_code);
146
      } else {
147
        SYS_PRINT("Connected to broker\n\r");
148
      }
149
      SYS_PRINT("Subscribing to /test\n\r");
150
      mg_mqtt_subscribe(nc, topic_expressions,
151
                        sizeof(topic_expressions) / sizeof(*topic_expressions),
152
                        ++msg_id);
153
      nc->flags |= MG_F_USER_1;
154
      break;
155
    case MG_EV_MQTT_SUBACK:
156
      SYS_PRINT("Subscription acknowledged\r\n");
157
      break;
158
    case MG_EV_MQTT_PUBLISH:
159
      SYS_PRINT("Got incoming message %s: %.*s\r\n", msg->topic,
160
                (int) msg->payload.len, msg->payload.p);
161
      break;
162
    case MG_EV_POLL: {
163
      if (nc->flags & MG_F_USER_1) {
164
        char msg[100];
165
        static uint32_t prev_send = 0;
166
        uint32_t now = DRV_RTCC_TimeGet();
167
        if (now - prev_send > 1000) {
168
          int len = snprintf(msg, sizeof(msg), "Current RTC value: %u", now);
169
          SYS_PRINT("Publishing message with RTC value=%u\r\n", now);
170
          mg_mqtt_publish(nc, "/stuff", ++msg_id, MG_MQTT_QOS(0), msg, len);
171
          prev_send = now;
172
        }
173
      }
174
      break;
175
    }
176
    case MG_EV_CLOSE:
177
      SYS_PRINT("Connection to broker is closed\r\n");
178
      appData.state = APP_STATE_DONE;
179
      break;
180
    default:
181
      break;
182
  }
183
}