#include <stdio.h>
#include <string.h>
#include "RAK.h"
#include "Nano1X2Series.h"
#include "LY096BG30.h"
#include "base64.h"
#include "sensorinit.h"
#include "DHT11.h"
#define SMTP_BUFF 64
#define RESEND 3
#define SENDTIME 90 //3min=180s 180/2=90
#define DEBUG_PRINTF(...) // printf(__VA_ARGS__)
char base64[SMTP_BUFF];
uint8_t g_u8RecData[1024];
volatile int32_t g_i32pointer;
volatile uint16_t time_DHT11[40];
volatile uint32_t capture_count =0;
uint16_t DHT11_D[2];
extern void print_Line(uint8_t Line, char Text[]);
/* SendMail: 如果你的邮箱是ic@126.com,想寄信给ee@139.com
char *id:你的邮箱id,这里是ic
char *pw:你的邮箱密码,自行填写******
char *server:你的邮箱域名,126.com
char *from: 你的邮箱全名,这里是ic@126.com
char *to: 寄信目标地址,这里是ee@139.com
char *subject:邮件标题,这里是Warning
char *data:邮件内容,这里是Temp Over 30. Beep! To do or Not.
*/
int SendMail(char *id, char *pw, char *server, char *from, char *to, char *subject ,char *data);
int mailcmd(char *cmd, char *content, char *expect);
void TMR0_IRQHandler(void)
{
time_DHT11[capture_count] = TIMER_GetCaptureData(TIMER0);
capture_count++;
TIMER_ClearCaptureIntFlag(TIMER0);
}
int main()
{
char tmp[SMTP_BUFF];
uint32_t sendtime=0;
SYS_Init();
UART_Init();
Init_DHT11();
I2C_Init();
WIFI_Init();
while(1)
{
CLK_SysTickDelay(2000000);
Read_DHT11(DHT11_D);
DEBUG_PRINTF("Humidity = %d\n", DHT11_D[0]);
DEBUG_PRINTF("Temperature = %d\n", DHT11_D[1]);
snprintf(tmp, SMTP_BUFF, "Temperature=%2d", DHT11_D[1]);
print_Line(2, tmp);
if((DHT11_D[1] > 30))
{
if((sendtime % SENDTIME) == (SENDTIME-1))
{
print_Line(1, "Send Mail Now ");
if (SendMail("ic", "*******", \
"126.com", "ic@126.com", "ee@139.com", \
"Warning", "Temp Over 30. Beep! To do or Not."))
{
DEBUG_PRINTF("Send Successful\r\n");
print_Line(1, "Send Mail Ok ");
sendtime = 0;
}
else
{
DEBUG_PRINTF("Send Failed\r\n");
print_Line(1, "Send Mail Fail");
sendtime = SENDTIME/3;
}
}
sendtime = (sendtime > SENDTIME)?(0):(sendtime+1);
}
else sendtime = 0;
snprintf(tmp, SMTP_BUFF, "Send Time:%2d", sendtime);
print_Line(3, tmp);
}
}
int SendMail(char *id, char *pw, char *server, char *from, char *to, char *subject ,char *data)
{
int flag;
flag = mailcmd("helo %s\r\n", server, "250");
flag &= mailcmd("%sauth login\r\n", "", "334");
encode_base64(base64 , id, strlen(id));
flag &= mailcmd("%s\r\n", base64, "334");
encode_base64(base64 , pw, strlen(pw));
flag &= mailcmd("%s\r\n", base64, "235");
flag &= mailcmd("mail from: <%s>\r\n", from, "250");
flag &= mailcmd("rcpt to: <%s>\r\n", to, "250");
flag &= mailcmd("%sdata\r\n", "", "354");
flag &= mailcmd("from: %s\r\n", id, "");
flag &= mailcmd("to: %s\r\n", to, "");
flag &= mailcmd("subject: %s\r\n\r\n", subject, "");
flag &= mailcmd("%s\r\n.\r\n", data, "250");
flag &= mailcmd("%squit\r\n\r\n", "", "221");
return flag;
}
int mailcmd(char *cmd, char *content, char *expect)
{
char tmp[SMTP_BUFF];
int i=1;
while(1)
{
snprintf(tmp, SMTP_BUFF, (const char *)cmd, content);
RAK_UART_Send((uint8_t *)tmp, strlen(tmp));
CLK_SysTickDelay(500000);
DEBUG_PRINTF(tmp);
DEBUG_PRINTF((char *)g_u8RecData);
if(strstr((const char *)g_u8RecData, expect))
break;
if(++i > RESEND)
return 0;
}
return 1;
}