質問編集履歴

1

エラーに対応したコードを追加しました

2019/11/23 12:33

投稿

ryu422
ryu422

スコア17

test CHANGED
File without changes
test CHANGED
@@ -299,3 +299,351 @@
299
299
 
300
300
 
301
301
  よろしくお願いします。
302
+
303
+
304
+
305
+ 追記
306
+
307
+ エラーに対応するのは以下のコードになります
308
+
309
+ ```
310
+
311
+ #define FTP_WAIT 1
312
+
313
+ #define BUFFER_SIZE 128
314
+
315
+
316
+
317
+ byte doFTP(char *filename){
318
+
319
+ char ftpBuf[BUFFER_SIZE];
320
+
321
+ WiFiClient client;
322
+
323
+ WiFiClient dclient;
324
+
325
+ int i;
326
+
327
+
328
+
329
+ File file = SPIFFS.open(filename,"r");
330
+
331
+ if(!file){
332
+
333
+ Serial.println("SPIFFS open fail");
334
+
335
+ return 11;
336
+
337
+ }
338
+
339
+ Serial.println("SPIFFS opened");
340
+
341
+ if (client.connect(FTP_TO,21)) {
342
+
343
+ Serial.println("Command connected");
344
+
345
+ }
346
+
347
+ if(eRcv(client,ftpBuf)) return 21;
348
+
349
+
350
+
351
+ sprintf(ftpBuf,"USER %s\r\n",FTP_USER);
352
+
353
+ client.print(ftpBuf);
354
+
355
+ delay(FTP_WAIT);
356
+
357
+ Serial.print(ftpBuf);
358
+
359
+ if(eRcv(client,ftpBuf)) return 22;
360
+
361
+
362
+
363
+ sprintf(ftpBuf,"PASS %s\r\n",FTP_PASS);
364
+
365
+ client.print(ftpBuf);
366
+
367
+ delay(FTP_WAIT);
368
+
369
+ Serial.println("PASS");
370
+
371
+ if(eRcv(client,ftpBuf)) return 23;
372
+
373
+
374
+
375
+ client.print("Type I\r\n");
376
+
377
+ delay(FTP_WAIT);
378
+
379
+ Serial.println("Type i");
380
+
381
+ if(eRcv(client,ftpBuf)) return 25;
382
+
383
+
384
+
385
+ client.print("PASV\r\n");
386
+
387
+ delay(FTP_WAIT);
388
+
389
+ Serial.println("PASV");
390
+
391
+ delay(100);
392
+
393
+ if(eRcv(client,ftpBuf)) return 26;
394
+
395
+
396
+
397
+ char *tStr = strtok(ftpBuf,"(,");
398
+
399
+ if(tStr == NULL) return 27;
400
+
401
+ int array_pasv[6];
402
+
403
+ for (i = 0; i < 6; i++) {
404
+
405
+ tStr = strtok(NULL,"(,");
406
+
407
+ array_pasv[i] = atoi(tStr);
408
+
409
+ if(tStr == NULL){
410
+
411
+ Serial.println("Bad PASV Answer");
412
+
413
+ return 28;
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ unsigned int hiPort,loPort;
422
+
423
+ hiPort = array_pasv[4] << 8;
424
+
425
+ loPort = array_pasv[5] & 255;
426
+
427
+ Serial.print("Data port: ");
428
+
429
+ hiPort = hiPort | loPort;
430
+
431
+ Serial.println(hiPort);
432
+
433
+ if (dclient.connect(FTP_TO,hiPort)) {
434
+
435
+ Serial.println("Data connected");
436
+
437
+ }else{
438
+
439
+ Serial.println("Data connection failed");
440
+
441
+ client.stop();
442
+
443
+ file.close();
444
+
445
+ return 31;
446
+
447
+ }
448
+
449
+ sprintf(ftpBuf,"STOR %s%s\r\n",FTP_DIR,filename);
450
+
451
+ client.print(ftpBuf);
452
+
453
+ delay(FTP_WAIT);
454
+
455
+ Serial.print(ftpBuf);
456
+
457
+ if(eRcv(client,ftpBuf)){
458
+
459
+ dclient.stop();
460
+
461
+ file.close();
462
+
463
+ return 32;
464
+
465
+ }
466
+
467
+ Serial.println("Writing");
468
+
469
+ i=0;
470
+
471
+ while(file.available()){
472
+
473
+ ftpBuf[i]=file.read();
474
+
475
+ i++;
476
+
477
+ if(i >= BUFFER_SIZE){
478
+
479
+ if(!dclient.connected()) break;
480
+
481
+ dclient.write( (byte *)ftpBuf, BUFFER_SIZE);
482
+
483
+ i=0;
484
+
485
+ Serial.print(".");
486
+
487
+ delay(1);
488
+
489
+ }
490
+
491
+ }
492
+
493
+ if(i > 0){
494
+
495
+ if(dclient.connected()){
496
+
497
+ dclient.write((byte *)ftpBuf, i);
498
+
499
+ }
500
+
501
+ }
502
+
503
+ dclient.stop();
504
+
505
+ Serial.println("Data disconnected");
506
+
507
+ if(eRcv(client,ftpBuf)) return 33;
508
+
509
+ client.print("QUIT\r\n");
510
+
511
+ delay(FTP_WAIT);
512
+
513
+ Serial.println("QUIT");
514
+
515
+ if(eRcv(client,ftpBuf)) return 91;
516
+
517
+ client.stop();
518
+
519
+ Serial.println("Command disconnected");
520
+
521
+ file.close();
522
+
523
+ Serial.println("SPIFFS closed");
524
+
525
+ return 0;
526
+
527
+ }
528
+
529
+
530
+
531
+ byte eRcv(WiFiClient &client,char *ftpBuf){
532
+
533
+ byte thisByte,i=0,len=0;
534
+
535
+
536
+
537
+ while(!client.available()){
538
+
539
+ delay(FTP_WAIT);
540
+
541
+ if(!client.connected()){
542
+
543
+ Serial.println("FTP:eRcv:disC");
544
+
545
+ return 11;
546
+
547
+ }
548
+
549
+ i++;
550
+
551
+ if(i>1000){ // 200ms以上必要
552
+
553
+ Serial.println("FTP:eRcv:noRes");
554
+
555
+ return 12;
556
+
557
+ }
558
+
559
+ }
560
+
561
+ while(client.connected()){
562
+
563
+ if(!client.available()){
564
+
565
+ delay(FTP_WAIT);
566
+
567
+ if(!client.available()) break;
568
+
569
+ }
570
+
571
+ thisByte = client.read();
572
+
573
+ if(thisByte==(byte)'\r');
574
+
575
+ else if(thisByte==(byte)'\n'){
576
+
577
+ Serial.write('>');
578
+
579
+ Serial.println(ftpBuf);
580
+
581
+ if(ftpBuf[0] >= '4'){
582
+
583
+ client.print("QUIT\r\n");
584
+
585
+ delay(FTP_WAIT);
586
+
587
+ Serial.println("QUIT");
588
+
589
+ return 1;
590
+
591
+ }
592
+
593
+ if(len>3 && ftpBuf[3] == ' '){
594
+
595
+ return 0;
596
+
597
+ }
598
+
599
+ len = 0;
600
+
601
+ }else if(len < BUFFER_SIZE - 1 ){
602
+
603
+ ftpBuf[len] = thisByte;
604
+
605
+ len++;
606
+
607
+ ftpBuf[len] = 0;
608
+
609
+ }
610
+
611
+ }
612
+
613
+ return 0;
614
+
615
+ }
616
+
617
+
618
+
619
+ void efail(WiFiClient &client){
620
+
621
+ byte thisByte = 0;
622
+
623
+ client.print("QUIT\r\n");
624
+
625
+ delay(FTP_WAIT);
626
+
627
+ while(!client.available()){
628
+
629
+ if(!client.connected()) return;
630
+
631
+ delay(1);
632
+
633
+ }
634
+
635
+ while(client.available()){
636
+
637
+ thisByte = client.read();
638
+
639
+ Serial.write(thisByte);
640
+
641
+ }
642
+
643
+ client.stop();
644
+
645
+ Serial.println("Command disconnected");
646
+
647
+ }
648
+
649
+ ```