Wemos D1 Mini - Send email, interact with Arduino

Hi. I'm an ultra basic user and I've been working on this project to open a greenhouse hatch subject to temperature using ArduinoMega, L298N, K-Type thermocouple and Max6675. Now I need it to send an email if temperature reaches a high temperature (say 35C). I tried with a CC3000 shield and didn't work. Now I have a Wemos D1 Mini, and I really don't know what to do in terms of

a) how to connect it to the Arduino Mega (what pins go where) and the voltage situation. The current setup somehow has the Arduino getting voltage from a 12V battery that connected to the L298N (not sure how that happened, but when I disconnected my USB cable to the Arduino, the circuit was still on with the battery power, which was meant to power the actuator that moves the hatch). Am I burning the arduino? Anyway, the Wemos uses 3.3V I have ready, so should I put a put a few resistors there to reduce voltage?
b) whether I can mix in one script what I have with the Arduino + what I need to send an email.

I'm out of my depth here.

This is the code I have so far for the non Internet part of it.


// Hardware: Arduino Mega + LN298N to control the linear actuator + Max6675 to interact with the K-Type thermocouple

#include "max6675.h" //for MAX6675 thermocouple chip

//Constants
#define actuator 4

//Variables
float temp; //Stores temperature value
int maxTemp = 28; // define maximum temperature on which actuator starts
int minTemp = 20; // define minimmum temperature on which actuator retracts

const int IN1_PIN = 7; // the Arduino pin connected to the IN1 pin L298N
const int IN2_PIN = 8; // the Arduino pin connected to the IN2 pin L298N

//initialising the thermocouple K / MAX6675
int thermoDO = 12;
int thermoCS = 11;
int thermoCLK = 13;

// initialise MAX6675
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void setup() 
{
  //Initialize serial port
  Serial.begin(9600);

  // initialize digital pins as outputs.
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);

  // wait for MAX chip to stabilize
  delay(500);
}

void loop() 
{ 
    //Read data and store it to variable temp
   
    temp= thermocouple.readCelsius();
    Serial.print("C = "); 
    Serial.println(temp);

    if(temp > maxTemp) {
        // extend the actuator
        digitalWrite(IN1_PIN, HIGH);
        digitalWrite(IN2_PIN, LOW);
        //only open for 5 seconds and leave hatch opened half way (when in doing so, temperature is mantained within min/max limits)
        delay (2000);
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, LOW);
    } 
    else if (temp <minTemp){
        // retracts the actuator
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, HIGH);
        //only open for 5 seconds and leave hatch opened half way (when in doing so, temperature is mantained within min/max limits)
        delay (2000);
        digitalWrite(IN1_PIN, LOW);
        digitalWrite(IN2_PIN, LOW);
   }
    else {
  }
    
    
    //2000mS delay between reads
    delay(10000);
}
```![PXL_20210927_042018377|375x500](upload://tyvVa4e4p5W6XwtvDiSDyRV8zL2.jpeg)

a). No. Use converters giving the correct Vcc.
b). Yes.

Agreed. My advice would be to stop, disconnect everything to avoid damage and go back to the drawing board. We can work with you to design a much simpler circuit that will work.

Here are some changes I would suggest:

  1. Save the Mega for another project, just use the Wemos.
  2. Save the thermocouple for another project, one where temperatures in the hundreds of degrees need to be measured. Use a ds18b20 sensor for this project.
  3. Get a DC-DC "buck" converter to provide a 3.3V or 5V supply for the Wemos from the 12V battery. This can be quite small, 0.5A output will be enough for the Wemos and sensors. Alternatively, some LN298 modules have a built-in 5V regulator. Perhaps this can provide a suitable supply for the Wemos, I'm not very familiar with such modules. Hopefully another forum member can advise about this (although they may point out that LN298 is a very old design and much better replacements are available).

I am incluned with Paul_RB.

I would add that the LN298 is a huge power consumer.
I don't think anyone would recomend for battery use.

Not knowing if you have dozens of sensors or just a few, the ESP32 has more I/O that a ESP8266.

But to address your actual question.
The ESP-01 originally could not be programmed to act like an Arduino and was sold as a WiFi module.
There are lots of old pages showing how to do that. No one recommends that anymore. The ESP chips are far superior in speed amd memory.

Thank you very much for the advice so far. I would need a lot of help to rebuild I think. Currently the setup works (except the email thing). It seems like the L298N does have a 12V to 5V converter and I think that's why the Arduino is being powered.

Ok, so if I am to follow the recommendation here, I do have initial basic questions that I'd really appreciate if I can get some guidance. Some context first. I only have 1 sensor (temperature) and only need to create two types of actions, to move linear actuator up and down based on Temperature triggers and to send an email when another temperature trigger. Attached what I've got. I'll get a waterproof DS18B20, but in the meantime, although a bit more innacurate, I'll stick with the Type K thermocouple + Max6775 (will replace in future).

My first questions:

  1. Do I need a DC-DC "buck" converter if the L298N already has a converter to 5V? Or could I just do it with resistors? I have a lot of them. Found this one in case I need it. Is this what you meant? https://www.ebay.com.au/itm/185040576576
  2. If the L298N consumes too much power/and it's a bit old. What would be the recommended alternative? I would prefer to work with what I've got it possible, but if I'm using completely the wrong thing, then of course open to do it right.

Thanks so much again!

Can't say as I have never used the L298N. Nor will; I believe "those who know" here seem to say it is obsolete! :roll_eyes:

Never! :astonished:

A very non-useful module. Just put them carefully away in your little "museum"! :grin:

Thanks Paul. I'm a bit lost on what I should do. I'm kind of hearing what I shouldn't do. For an application like this what alternative would you use to the L298N to run the linear actuator? and what DC-DC 12V to 3.3V would you recommend to use for this application? I imagine there would be premium models for everything. I'm kind of after the "Toyota" here, something durable, but not overly pricey.

You keep referring to L298N which is a chip, but your picture seems to show a module containing an L298N chip and other components. This is what I was asking back in reply #3, but you still haven't provided any more details or a link. If this is what I suspect, it may have a built-in 5V regulator which could be used to power the Wemos.

True, L298N is not very efficient, and it's built-in regulator won't be either, so not recommended for projects with small batteries, but you seem to be planning to use a reasonably large SLA battery, so while not ideal, it should be ok I think, at least for now. If you find later that you need more battery life, then you can start worrying about more efficient motor drivers and buck converters.

My next concern is: will the 3.3V signals from the Wemos be able to drive the L298N?

EDIT: According to page 3 of the L298 data sheet, any voltage over 2.3V should be recognised as a HIGH input, so the 3.3V from the Wemos pins is no problem.

However, with the max6675, if you power that with 5V, it would need at least 0.7*5=3.5V HIGH signal, so the 3.3V Wemos pins would not be high enough. Fortunately, it looks like you can power the max6675 with 3.3V from the Wemos' 3V3 pin, and again no problem.

Thanks Paul. I didn't know the L298N was a chip. I thought it was the whole module. It does look like the module I have has an in-built voltage regulator. I've tried several times with the seller and I can't the get confirmation, but the Arduino is working well with that L298N chip-based module as an input (5V I assume, need to get a tester).

The below code is where I'm at with a Wemos D1 WiFi Arduino UNO Development Board (don't konw how to interact with a Wemos stand alone, I'm just too basic on this stuff).

It sends the email well (tested that already), but the Max6675 is giving me troubles. It shows the below code on the serial print. I've tested the Max6675 on the Arduino Mega and it works well, so I know the unit works and also my ArduinoIDE. I've tried different pins configuration, a bit lost. Fixing this one and I have the whole project going. I have ordered a DS18B20 as recommended, so will see in 3 weeks time if that solves it (if I can't get to a solution in the interim with your help).

ets Jan 8 2013,rst cause:4, boot mode:(3,0)

wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld

My code below....

#include <Arduino.h>
#if defined(ESP32)
  #include <WiFi.h>
#elif defined(ESP8266)
  #include <ESP8266WiFi.h>
#endif
#include <ESP_Mail_Client.h>

#define WIFI_SSID "xxxxxxxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxxxxxxx"

#define SMTP_HOST "smtp.gmail.com"
#define SMTP_PORT 465

/* The sign in credentials */
#define AUTHOR_EMAIL "xxxxxxxxxxxxxxxxxx"
#define AUTHOR_PASSWORD "xxxxxxxxxxxxxxxxxxxxx"

/* Recipient's email*/
#define RECIPIENT_EMAIL "xxxxxxxxxxxxxxxxxxxxx"

/* The SMTP Session object used for Email sending */
SMTPSession smtp;

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status);

#include "max6675.h" //for MAX6675 thermocouple chip
//Constants
#define actuator 4

//Variables
float temp; //Stores temperature value
float maxTemp = 28; // define maximum temperature on which actuator starts
float minTemp = 20; // define minimmum temperature on which actuator retracts
float extremeTemp = 35; // define minimmum temperature on which actuator retracts

const int IN1_PIN = 14; // the Arduino pin connected to the IN1 pin L298N
const int IN2_PIN = 15; // the Arduino pin connected to the IN2 pin L298N

//initialising the thermocouple K / MAX6675
int thermoDO = 7;
int thermoCS = 6;
int thermoCLK = 5;

// initialise MAX6675
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void setup(){
  Serial.begin(115200);
 // initialize digital pins as outputs.
  pinMode(IN1_PIN, OUTPUT);
  pinMode(IN2_PIN, OUTPUT);

}

void loop(){
   
    //2000mS delay between reads
    delay(10000);    
    //Read data and store it to variable temp  
    temp= thermocouple.readCelsius();
   
    Serial.print("C = "); 
    Serial.println(temp);

    if((temp>=extremeTemp) && (temp<100)) 
        sendEmail(String(temp));  
     
    else if ((extremeTemp>temp) && (temp > maxTemp))
        openHatch();  
    
    else if (temp <minTemp)
        closeHatch();    

}
void closeHatch(){
    // retracts the actuator
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, HIGH);
    //only open for 5 seconds and leave hatch opened half way (when in doing so, temperature is mantained within min/max limits)
    delay (2000);
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, LOW);
}
void openHatch(){
    // extend the actuator
    digitalWrite(IN1_PIN, HIGH);
    digitalWrite(IN2_PIN, LOW);
    //only open for 5 seconds and leave hatch opened half way (when in doing so, temperature is mantained within min/max limits)
    delay (2000);
    digitalWrite(IN1_PIN, LOW);
    digitalWrite(IN2_PIN, LOW);
}

void sendEmail(String temperature){
      Serial.println();
      Serial.print("Connecting to AP");
      WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
      while (WiFi.status() != WL_CONNECTED){
          Serial.print(".");
          delay(200);
      }
      Serial.println("");
      Serial.println("WiFi connected.");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      Serial.println();
    
      /** Enable the debug via Serial port
       * none debug or 0
       * basic debug or 1*/
      smtp.debug(1);
      /* Set the callback function to get the sending results */
      smtp.callback(smtpCallback);
      /* Declare the session config data */
      ESP_Mail_Session session;
    
      /* Set the session config */
      session.server.host_name = SMTP_HOST;
      session.server.port = SMTP_PORT;
      session.login.email = AUTHOR_EMAIL;
      session.login.password = AUTHOR_PASSWORD;
      session.login.user_domain = "";
    
      /* Declare the message class */
      SMTP_Message message;
    
      /* Set the message headers */
      message.sender.name = "GreenHouse";
      message.sender.email = AUTHOR_EMAIL;
      String Str1 = "Greenhouse temperature alert: "+temperature+" degrees";
      const char* string1 = Str1.c_str();
      message.subject = string1;
      message.addRecipient("Sara", RECIPIENT_EMAIL);
    
      /*Send HTML message*/
      
      String htmlMsg = "<div style=\"color:#2f4468;\"><h1>Please open hatch. Temperature has reached "+temperature+" degrees</h1></div>" +temperature;
      message.html.content = htmlMsg.c_str();
      message.html.content = htmlMsg.c_str();
      message.text.charSet = "us-ascii";
      message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;
    
      /* Connect to server with the session config */
      if (!smtp.connect(&session))
        return;
    
      /* Start sending Email and close the session */
      if (!MailClient.sendMail(&smtp, &message))
        Serial.println("Error sending Email, " + smtp.errorReason());
}

/* Callback function to get the Email sending status */
void smtpCallback(SMTP_Status status){
  /* Print the current status */
  Serial.println(status.info());

  /* Print the sending result */
  if (status.success()){
    Serial.println("----------------");
    ESP_MAIL_PRINTF("Message sent success: %d\n", status.completedCount());
    ESP_MAIL_PRINTF("Message sent failled: %d\n", status.failedCount());
    Serial.println("----------------\n");
    struct tm dt;

    for (size_t i = 0; i < smtp.sendingResult.size(); i++){
      /* Get the result item */
      SMTP_Result result = smtp.sendingResult.getItem(i);
      time_t ts = (time_t)result.timestamp;
      localtime_r(&ts, &dt);

      ESP_MAIL_PRINTF("Message No: %d\n", i + 1);
      ESP_MAIL_PRINTF("Status: %s\n", result.completed ? "success" : "failed");
      ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec);
      ESP_MAIL_PRINTF("Recipient: %s\n", result.recipients);
      ESP_MAIL_PRINTF("Subject: %s\n", result.subject);
    }
    Serial.println("----------------\n");
  }
}

A DMM (Digital MultiMeter) should have been one of the very first items you purchased. Does not need to be a high quality expensive one, something costing £10~15 will be fine. If in doubt about what to buy, post a link here.

Even if you never do another electronics project again, a DMM is a useful thing to have around the home for various purposes. You will probably wonder how you got along without one.

A what? Please post a link, that sounds like an incorrect description. Do you mean a Wemos D1 mini, as shown in your picture in post #5, or a Wemos D1 R2, which was the unsuccessful attempt to make an Uno shaped version?

You plug it into your pc/laptop with usb cable, like almost all other types Arduino.

This is a common problem with projects using Arduino's boards based on esp8266 chips. It's called a "watchdog reset error" and is caused by something in your code, or one of the libraries used, which is blocking the chip from managing the WiFi connection.

It sounds like this error started appearing when you added the code for the thermocouple, so it's probably related to that. Perhaps the library is not esp8266 compatible, for example.

How are you connecting the max6675 to the Wemos? Please post a schematic. Pen and paper is fine.

Thanks Paul. Really appreciate your time and help. Yes I do have a DDM, it just broke down a few months ago (I think I broke it down by putting wrong input, wrong setting). Will get another one. Agree it's a must have.

I have a Wemos D1 mini, but didn't know what to connect where, so I got this other one, which as you say it's kind of a ESP8266 embedded on an Arduino Uno. Not sure if it's R2 or R3 (but it's exactly the same as in the pic attached). Maybe I should use the Wemos D1 Mini I have.

yes the max6675 works standalone, but in this code it doesn't. So not sure if it's the libraries or something else. Attached the schematic of how I connected things. Is pdf format ok?

Thanks again for the guidance.

Diagram.pdf (131.8 KB)

Not really, it cannot be embedded into the post. PDFs are not a graphic format, you need to use an actual .jpg or .png.

I cannot see a 5 V power supply in your diagram!

It's not actually a schematic, but it does indicate how you have wired things up, so thanks for that.

Try this change:

int thermoDO = D7;
int thermoCS = D6;
int thermoCLK = D5;
...
const int IN1_PIN = D3; // the Arduino pin connected to the IN1 pin L298N
const int IN2_PIN = D4; // the Arduino pin connected to the IN2 pin L298N

On ESP based Arduino, "D5" is not the same as "5". There are two different pin numbering schemes in play!

Hopefully the above will allow the esp to communicate with the max6675 and prevent the watchdog errors.

It is believed that the L298 module purchased may have a built-in 5V regulator. @emonter will confirm that when their new DMM arrives.
I recommended that the max6675 be powered with 3.3V. What's your opinion?

You bought that on purpose? Oh well, we all made mistakes as a beginner!

It's not an Uno. It has the same shape/form factor as an Uno, but other than that is basically the same as the Wemos Mini. The idea was that the shape would make the board physically compatible with shields designed for the Uno. In practice, almost no shields were electrically compatible, so the product was a flop. The Wemos Mini outsold it 100 times over.

As you don't plan to use it with a shield, it's form factor is not an advantage to you. In fact it's a disadvantage, because unlike the Mini, it is not breadboard compatible.

Which to use? Well, I suppose we should attempt to use it rather than waste it. Maybe you could get a proto-shield, which is basically a PCB designed to build prototype circuits on, but in the shape of a shield. You could then use that to solder the rest of your circuit on. Being a "dumb" shield, there should be no compatibility problems.

Indeed! In the form factor of a UNO, but worse than a UNO! :astonished:

Hi Paul, that's it. It worked. It was the "D" before the pin numbers. It works now and it sends the email when reaching 35 degrees. I have the Max6675 with 3.3V from the Arduino/Wemos Mini (or whatever it is what I bought).

I'll do what you said around the proto-shield to replace the breadboard setup and also have already ordered the other temp sensor, so I hope I don't have issues changing the Max6675+K-type with this DS18B20 one.

Thank you again for the very useful advice.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.