Site icon Renzo Mischianti

Raspberry Pico W & EmailSender

Hello. Thank you for the EmailSender library which is really useful. I'm trying to use it on a Raspberry Pico W environment and the ESP8266SdFat library on SPI 1. I defined in the EMailSenderKey.h file #define SD_CS_PIN 13 instead of #define SD_CS_PIN SS The mail message part is ok, but not the attachment of the .CSV file If you can help me. Thank you. Bord is Raspberry Pi W with : Raspberry Pi RP2040 Core platform file By Earle F. Philhower name=Raspberry Pi RP2040 Boards(2.3.3) version=2.3.3

#include <EMailSender.h>
#include <WiFi.h>
#include "SdFat.h"
#include <stdio.h>
#include "sdios.h"
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#include "hardware/gpio.h"

#include "SdFat.h"

// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
#define SD_FAT_TYPE 1
// Try max SPI clock for an SD 50. Reduce SPI_CLOCK if errors occur.
#define SPI_CLOCK SD_SCK_MHZ(80)
// Try to select the best SD card configuration.
#if HAS_SDIO_CLASS
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#elif ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
#else // HAS_SDIO_CLASS
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
#endif // HAS_SDIO_CLASS

#if SD_FAT_TYPE == 0
SdFat sd;
File file;
File root;
#elif SD_FAT_TYPE == 1
SdFat32 sd;
File32 file;
#elif SD_FAT_TYPE == 2
SdExFat sd;
ExFile file;
#elif SD_FAT_TYPE == 3
SdFs sd;
FsFile file;
#else // SD_FAT_TYPE
#error Invalid SD_FAT_TYPE
#endif // SD_FAT_TYPE

//——————————————————————————
// store error strings in flash memory
#define error(s) sd.errorHalt(PSTR(s))
//——————————————————————————

uint8_t connection_state = 0;
uint16_t reconnect_interval = 10000;

EMailSender emailSend(email_login, email_password, email_from, smtp_server,
		smtp_port);

uint8_t WiFiConnect(const char *nSSID = nullptr,
		const char *nPassword = nullptr) {
	static uint16_t attempt = 0;
	Serial.print("Connecting to ");
	if (nSSID) {
		WiFi.begin(nSSID, nPassword);
		Serial.println(nSSID);
	}
	uint8_t i = 0;
	while (WiFi.status() != WL_CONNECTED && i++ < 50) {
		delay(200);
		Serial.print(".");
	}
	++attempt;
	Serial.println("");
	if (i == 51) {
		Serial.print("Connection: TIMEOUT on attempt: ");
		Serial.println(attempt);
		if (attempt % 2 == 0)
			Serial.println(
					"Check if access point available or SSID and Password\r\n");
		return false;
	}
	Serial.println("Connection: ESTABLISHED");
	Serial.print("Got IP address: ");
	Serial.println(WiFi.localIP());
	return true;
}
void Awaits() {
	uint32_t ts = millis();
	while (!connection_state) {
		delay(50);
		if (millis() > (ts + reconnect_interval) && !connection_state) {
			connection_state = WiFiConnect();
			ts = millis();
		}
	}
}

void setup() {
	Serial.begin(115200);
	delay(2000);

// initialisation carte SD
	if (!sd.begin(SD_CS_PIN)) {
		Serial.println("WARNING initialization SD failed!");
		delay(1000);
	} else {
		Serial.println(F("Card is present."));
	}
// Show capacity and free space of SD card
	Serial.print(F("Capacity of card: "));
	Serial.print(long(sd.card()->sectorCount() >> 1));
	Serial.println(F(" kBytes"));
	Serial.print(F("\nList of files on the SD.\n"));
	sd.ls(LS_R);

	connection_state = WiFiConnect(ssid, password);
	if (!connection_state) // if not connected to WIFI
		Awaits(); // constantly trying to connect

	EMailSender::FileDescriptior fileDescriptor[1];
	fileDescriptor[0].filename = F("Bernard_05011701_2023.CSV");
	fileDescriptor[0].url = F("/Bernard_05011701_2023");
	fileDescriptor[0].mime = MIME_TEXT_PLAIN;
	fileDescriptor[0].storageType = EMailSender::EMAIL_STORAGE_TYPE_SD;

	EMailSender::Attachments attachs = { 1, fileDescriptor };

	EMailSender::EMailMessage message;
	message.subject = "Data File Result";
	message.message =
			"Resultat de l’annalyse.\n\r 1er test sans piece attachée\n\r NE PAS REPONDRE A CETTE ADRESSE.";

	Serial.println("All structure created!");

	EMailSender::Response resp = emailSend.send("xxx@gmx.fr", message, attachs);

	Serial.println("Sending status: ");

	Serial.println(resp.status);
	Serial.println(resp.code);
	Serial.println(resp.desc);
}

void loop() {

}

Out Serial.print :

Card is present.
Capacity of card: 7864320 kBytes

List of files on the SD.
.eeprom
.wifi
.log.csv
Bernard_05011701_2023.CSV
Connecting to Linux

Connection: ESTABLISHED
Got IP address: 192.168.1.32
All structure created!
ONLY ONE RECIPIENTmiltiple destination and attachments
Insecure client:0
smtp.free.fr
465
220 smtp6-g21.free.fr ESMTP Postfix
HELO mischianti
250 smtp6-g21.free.fr
AUTH LOGIN:
334 VXNlcm5hbWU6
Encoding
xxxxx@free.fr
13
ZjVleG9AZnJlZS5mcg==
Encoding
xxxx@free.fr
13
334 UGFzc3dvcmQ6
Encoding
pwd
8
c3U4ODY3aTE=
Encoding
pwd
8
235 2.7.0 Authentication successful
MAIL FROM: <xxxx@free.fr>
250 2.1.0 Ok
RCPT TO: <xxxx@gmx.fr>
250 2.1.5 Ok
DATA:
354 End data with <CR><LF>.<CR><LF>
Message end
250 2.0.0 Ok: queued as ABBC37802E5
221 2.0.0 Bye
Sending status:
1
0
Message sent!

Exit mobile version