-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
191 lines (142 loc) · 5.69 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the AIROC™ CCM MQTT HELLO WORLD Example
* for ModusToolbox.
*
* Related Document: See README.md
*
*
*******************************************************************************
* $ Copyright 2023 Cypress Semiconductor $
*******************************************************************************/
#include "CCM.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/* define MODIFY_SSID macro as 1 for disconnecting the existing Wi-Fi connection and
* connect to different Access point*/
#define MODIFY_SSID_AFTER_CONNECTED (0)
/* define CIRRENT_APP_ONBOARDING macro as 1 for Wi-Fi Onboarding via Cirrent APP.*/
#define CIRRENT_APP_ONBOARDING (0)
/*define AWS_FLOW macro as 1 for choosing AWS flow and 0 for Cirrent flow*/
#define AWS_FLOW (1)
/* Max response delay in milliseconds for AT commands*/
#define RESPONSE_DELAY (120000)
#define POLLING_DELAY (60000)
#define SUCCESS 1
#define FAILURE 0
/* Set SSID, Passphrase and Endpoint as follows
* AT+CONF SSID=XXXX\n; where XXXX is the required SSID
* AT+CONF Passphrase=YYYY\n ; YYYY is the Passphrase
* AT+CONF EndPoint=ZZZZ\n; ZZZZ is the endpoint
*/
#define SET_SSID "AT+CONF SSID=\n"
#define SET_PASSPHRASE "AT+CONF Passphrase=\n"
#define SET_ENDPOINT "AT+CONF Endpoint=\n"
/******************************************************************************
* Function Prototypes
*******************************************************************************/
static void wifionboarding();
/******************************************************************************
* Global Variables
*******************************************************************************/
int result = 0;
/*******************************************************************************
* Function Name: main
*******************************************************************************
* Summary:
* System entrance point. This function
* - performs initial setup of device
* - initializes UART peripherals to send AT Commands to CCM and view debug messages.
* - sends required AT Commands to CCM module
*
* Return:
* int
*
*******************************************************************************/
int main()
{
bsp_init();
uart_init();
printf("\r ******************AIROC™ CCM MQTT HELLO WORLD******************\n");
#if MODIFY_SSID_AFTER_CONNECTED
/* AT command for disconnecting from Wi-Fi network */
at_command_send_receive("AT+DISCONNECT\n", RESPONSE_DELAY, &result, NULL);
#endif
#if AWS_FLOW
/*Check if CCM module already connected to AWS*/
if (!is_aws_connected())
{
/*AT command for sending Device Endpoint*/
at_command_send_receive(SET_ENDPOINT, RESPONSE_DELAY, &result, NULL);
/*Connect to Wi-Fi network if it is not connected already*/
if (!is_wifi_connected())
{
wifionboarding();
}
/*AT command for Connecting to AWS Cloud*/
at_command_send_receive("AT+CONNECT\n", RESPONSE_DELAY, &result, "OK 1 CONNECTED\r\n");
if (result != SUCCESS)
{
handle_error();
}
}
#else
/*Check if CCM module already connected to AWS*/
if (!is_aws_connected())
{
/*Connect to Wi-Fi network if it is not connected already*/
if (!is_wifi_connected())
{
wifionboarding();
}
/*AT command for Connecting CCM device to AWS staging*/
at_command_send_receive("AT+CONNECT\n", RESPONSE_DELAY, &result, "OK 1 CONNECTED\r\n");
if (result != SUCCESS)
{
handle_error();
}
/*AT command for Getting Endpoint from Cirrent Cloud*/
at_command_send_receive("AT+CLOUD_SYNC\n", RESPONSE_DELAY, &result, NULL);
/* Check in Cirrent console if the Job executed succesfully */
printf("\nThe Connection Automatically switches to the new endpoint after 120 seconds\n\n");
delay_ms(RESPONSE_DELAY);
while (!is_aws_connected())
;
}
#endif
/* The device subscribes to a topic "data" .
Send AT command for sending message to AWS cloud*/
at_command_send_receive("AT+CONF Topic1=data\n", RESPONSE_DELAY, &result, NULL);
at_command_send_receive("AT+SEND1 Hello World!\n", RESPONSE_DELAY, &result, NULL);
return 0;
}
/*******************************************************************************
* Function Name: WifiOnboarding
********************************************************************************
* Summary: Send AT commands to set SSID and Passphrase for CCM module.
* or
* Send AT command to enter Onboarding mode and connect to Wi-Fi via Cirrent APP
* Return:
* void
*
*******************************************************************************/
static void wifionboarding()
{
#if CIRRENT_APP_ONBOARDING
/* AT command to enter Wi-Fi onboarding mode*/
at_command_send_receive("AT+CONFMODE\n", RESPONSE_DELAY, &result, NULL);
printf("\n\rOpen Cirrent APP on your mobile device and choose your Wi-Fi SSID. \n\rThe program continues after successfully connecting to Wi-Fi SSID.\n\r");
while (!is_wifi_connected())
{
delay_ms(POLLING_DELAY);
}
#else
/* AT command for sending SSID */
at_command_send_receive(SET_SSID, RESPONSE_DELAY, &result, NULL);
/*AT command for sending Passphrase*/
at_command_send_receive(SET_PASSPHRASE, RESPONSE_DELAY, &result, NULL);
#endif
}
/* [] END OF FILE */