|
| 1 | +// libraries |
| 2 | +#include "SipfClient.h" |
| 3 | +#include <String> |
| 4 | +#include <LTE.h> |
| 5 | +#include <Camera.h> |
| 6 | + |
| 7 | +// APN nameard |
| 8 | + |
| 9 | +#define APP_LTE_APN "sakura" // replace your APN |
| 10 | + |
| 11 | +/* APN authentication settings |
| 12 | + * Ignore these parameters when setting LTE_NET_AUTHTYPE_NONE. |
| 13 | + */ |
| 14 | + |
| 15 | +#define APP_LTE_USER_NAME "" // replace with your username |
| 16 | +#define APP_LTE_PASSWORD "" // replace with your password |
| 17 | + |
| 18 | +// APN IP type |
| 19 | +// #define APP_LTE_IP_TYPE (LTE_NET_IPTYPE_V4V6) // IP : IPv4v6 |
| 20 | +#define APP_LTE_IP_TYPE (LTE_NET_IPTYPE_V4) // IP : IPv4 |
| 21 | +// #define APP_LTE_IP_TYPE (LTE_NET_IPTYPE_V6) // IP : IPv6 |
| 22 | + |
| 23 | +// APN authentication type |
| 24 | +// #define APP_LTE_AUTH_TYPE (LTE_NET_AUTHTYPE_CHAP) // Authentication : CHAP |
| 25 | +// #define APP_LTE_AUTH_TYPE (LTE_NET_AUTHTYPE_PAP) // Authentication : PAP |
| 26 | +#define APP_LTE_AUTH_TYPE (LTE_NET_AUTHTYPE_NONE) // Authentication : NONE |
| 27 | + |
| 28 | +/* RAT to use |
| 29 | + * Refer to the cellular carriers information |
| 30 | + * to find out which RAT your SIM supports. |
| 31 | + * The RAT set on the modem can be checked with LTEModemVerification::getRAT(). |
| 32 | + */ |
| 33 | + |
| 34 | +#define APP_LTE_RAT (LTE_NET_RAT_CATM) // RAT : LTE-M (LTE Cat-M1) |
| 35 | +// #define APP_LTE_RAT (LTE_NET_RAT_NBIOT) // RAT : NB-IoT |
| 36 | + |
| 37 | +LTE lteAccess; |
| 38 | +LTEClient client; |
| 39 | +SipfClient sipfClient; |
| 40 | + |
| 41 | +/** |
| 42 | + * Print camera error message |
| 43 | + */ |
| 44 | + |
| 45 | +void printCameraError(enum CamErr err) |
| 46 | +{ |
| 47 | + Serial.print("Error: "); |
| 48 | + switch (err) |
| 49 | + { |
| 50 | + case CAM_ERR_NO_DEVICE: |
| 51 | + Serial.println("No Device"); |
| 52 | + break; |
| 53 | + case CAM_ERR_ILLEGAL_DEVERR: |
| 54 | + Serial.println("Illegal device error"); |
| 55 | + break; |
| 56 | + case CAM_ERR_ALREADY_INITIALIZED: |
| 57 | + Serial.println("Already initialized"); |
| 58 | + break; |
| 59 | + case CAM_ERR_NOT_INITIALIZED: |
| 60 | + Serial.println("Not initialized"); |
| 61 | + break; |
| 62 | + case CAM_ERR_NOT_STILL_INITIALIZED: |
| 63 | + Serial.println("Still picture not initialized"); |
| 64 | + break; |
| 65 | + case CAM_ERR_CANT_CREATE_THREAD: |
| 66 | + Serial.println("Failed to create thread"); |
| 67 | + break; |
| 68 | + case CAM_ERR_INVALID_PARAM: |
| 69 | + Serial.println("Invalid parameter"); |
| 70 | + break; |
| 71 | + case CAM_ERR_NO_MEMORY: |
| 72 | + Serial.println("No memory"); |
| 73 | + break; |
| 74 | + case CAM_ERR_USR_INUSED: |
| 75 | + Serial.println("Buffer already in use"); |
| 76 | + break; |
| 77 | + case CAM_ERR_NOT_PERMITTED: |
| 78 | + Serial.println("Operation not permitted"); |
| 79 | + break; |
| 80 | + default: |
| 81 | + break; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +bool initializeCamera() |
| 86 | +{ |
| 87 | + /* begin() without parameters means that |
| 88 | + * number of buffers = 1, 30FPS, QVGA, YUV 4:2:2 format */ |
| 89 | + int err = theCamera.begin(); |
| 90 | + if (err != CAM_ERR_SUCCESS) |
| 91 | + { |
| 92 | + printCameraError(err); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /* Auto white balance configuration */ |
| 97 | + |
| 98 | + Serial.println("Set Auto white balance parameter"); |
| 99 | + err = theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_AUTO); |
| 100 | + if (err != CAM_ERR_SUCCESS) |
| 101 | + { |
| 102 | + printCameraError(err); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + /* Set parameters about still picture. |
| 107 | + * In the following case, QUADVGA and JPEG. |
| 108 | + */ |
| 109 | + Serial.println("Set still picture format"); |
| 110 | + err = theCamera.setStillPictureImageFormat( |
| 111 | + CAM_IMGSIZE_QUADVGA_H, |
| 112 | + CAM_IMGSIZE_QUADVGA_V, |
| 113 | + CAM_IMAGE_PIX_FMT_JPG, |
| 114 | + 5); |
| 115 | + if (err != CAM_ERR_SUCCESS) |
| 116 | + { |
| 117 | + printCameraError(err); |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + err = theCamera.setJPEGQuality(90); |
| 122 | + if (err != CAM_ERR_SUCCESS) |
| 123 | + { |
| 124 | + printCameraError(err); |
| 125 | + return false; |
| 126 | + } |
| 127 | + return true; |
| 128 | +} |
| 129 | + |
| 130 | +void setup() |
| 131 | +{ |
| 132 | + char apn[LTE_NET_APN_MAXLEN] = APP_LTE_APN; |
| 133 | + LTENetworkAuthType authtype = APP_LTE_AUTH_TYPE; |
| 134 | + char user_name[LTE_NET_USER_MAXLEN] = APP_LTE_USER_NAME; |
| 135 | + char password[LTE_NET_PASSWORD_MAXLEN] = APP_LTE_PASSWORD; |
| 136 | + |
| 137 | + // initialize serial communications and wait for port to open: |
| 138 | + Serial.begin(115200); |
| 139 | + while (!Serial) |
| 140 | + { |
| 141 | + ; // wait for serial port to connect. Needed for native USB port only |
| 142 | + } |
| 143 | + |
| 144 | + // initialize camera |
| 145 | + if (!initializeCamera()) |
| 146 | + { |
| 147 | + Serial.println("Camera initialize error"); |
| 148 | + } |
| 149 | + |
| 150 | + // Chech Access Point Name is empty |
| 151 | + if (strlen(APP_LTE_APN) == 0) |
| 152 | + { |
| 153 | + Serial.println("ERROR: This sketch doesn't have a APN information."); |
| 154 | + while (1) |
| 155 | + { |
| 156 | + sleep(1); |
| 157 | + } |
| 158 | + } |
| 159 | + Serial.println("=========== APN information ==========="); |
| 160 | + Serial.print("Access Point Name : "); |
| 161 | + Serial.println(apn); |
| 162 | + Serial.print("Authentication Type: "); |
| 163 | + Serial.println(authtype == LTE_NET_AUTHTYPE_CHAP ? "CHAP" : authtype == LTE_NET_AUTHTYPE_NONE ? "NONE" |
| 164 | + : "PAP"); |
| 165 | + if (authtype != LTE_NET_AUTHTYPE_NONE) |
| 166 | + { |
| 167 | + Serial.print("User Name : "); |
| 168 | + Serial.println(user_name); |
| 169 | + Serial.print("Password : "); |
| 170 | + Serial.println(password); |
| 171 | + } |
| 172 | + |
| 173 | + while (true) |
| 174 | + { |
| 175 | + |
| 176 | + /* Power on the modem and Enable the radio function. */ |
| 177 | + |
| 178 | + if (lteAccess.begin() != LTE_SEARCHING) |
| 179 | + { |
| 180 | + Serial.println("Could not transition to LTE_SEARCHING."); |
| 181 | + Serial.println("Please check the status of the LTE board."); |
| 182 | + for (;;) |
| 183 | + { |
| 184 | + sleep(1); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /* The connection process to the APN will start. |
| 189 | + * If the synchronous parameter is false, |
| 190 | + * the return value will be returned when the connection process is started. |
| 191 | + */ |
| 192 | + if (lteAccess.attach(APP_LTE_RAT, |
| 193 | + apn, |
| 194 | + user_name, |
| 195 | + password, |
| 196 | + authtype, |
| 197 | + APP_LTE_IP_TYPE) == LTE_READY) |
| 198 | + { |
| 199 | + Serial.println("Attach succeeded."); |
| 200 | + break; |
| 201 | + } |
| 202 | + |
| 203 | + /* If the following logs occur frequently, one of the following might be a cause: |
| 204 | + * - APN settings are incorrect |
| 205 | + * - SIM is not inserted correctly |
| 206 | + * - If you have specified LTE_NET_RAT_NBIOT for APP_LTE_RAT, |
| 207 | + * your LTE board may not support it. |
| 208 | + * - Rejected from LTE network |
| 209 | + */ |
| 210 | + Serial.println("An error has occurred. Shutdown and retry the network attach process after 1 second."); |
| 211 | + lteAccess.shutdown(); |
| 212 | + sleep(1); |
| 213 | + } |
| 214 | + |
| 215 | + sipfClient.begin(&client, 80); |
| 216 | + |
| 217 | + if (!sipfClient.authenticate()) |
| 218 | + { |
| 219 | + Serial.println("SIFP Authentication error"); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +void loop() |
| 224 | +{ |
| 225 | + static uint64_t utime = 0; |
| 226 | + static uint32_t counter = 0; |
| 227 | + |
| 228 | + bool res; |
| 229 | + |
| 230 | + String filename = "image.jpg"; |
| 231 | + |
| 232 | + // Capture Image |
| 233 | + CamImage img = theCamera.takePicture(); |
| 234 | + if (img.isAvailable()) |
| 235 | + { |
| 236 | + Serial.print("Image size: "); |
| 237 | + Serial.println(img.getImgSize()); |
| 238 | + |
| 239 | + res = sipfClient.uploadFile(filename, img.getImgBuff(), img.getImgSize()); |
| 240 | + if (res) |
| 241 | + { |
| 242 | + Serial.println("Upload OK"); |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + Serial.println("Upload Error"); |
| 247 | + } |
| 248 | + } |
| 249 | + else |
| 250 | + { |
| 251 | + Serial.println("Camera error"); |
| 252 | + } |
| 253 | + |
| 254 | + uint8_t buf[32]; |
| 255 | + filename.toCharArray(buf, sizeof(buf) - 1); |
| 256 | + SipfObjectObject objs[] = { |
| 257 | + {OBJ_TYPE_UINT32, 0x01, OBJ_SIZE_UINT32, (uint8_t *)&counter}, |
| 258 | + {OBJ_TYPE_STR_UTF8, 0x02, filename.length(), (uint8_t *)buf}, |
| 259 | + |
| 260 | + }; |
| 261 | + counter++; |
| 262 | + |
| 263 | + uint64_t otid = sipfClient.uploadObjects(uint64_t(0), objs, sizeof(objs) / sizeof(SipfObjectObject)); |
| 264 | + if (otid != 0) |
| 265 | + { |
| 266 | + Serial.print("OTID: 0x"); |
| 267 | + Serial.println(otid, HEX); |
| 268 | + } |
| 269 | + |
| 270 | + sleep(60); |
| 271 | +} |
0 commit comments