Mega + Makerfocus ESP8266 Esp-01

I've been trying to get wifi connected to this mega2560 for the past week and a half and am no closer now than when I started. I can't even get it to compile - have tried boards manager, library manager, and the additional boards manager URL in settings (using IDE 2.0.4). Can someone give me some pointers? It's supposed to be a simple connection.

The 3.3v & En on the Esp-01 are connected to the 3.3v on the mega (tied together by breadboard). Ground to ground. The Esp-01 TX to Mega Pin 28 & Esp-01 RX to Mega Pin 29. Note, I haven't tried it with Pins 0 or 1, and Pins 2 thru 18 are in use by the main part of the sketch, as well as a few others from Pins 20 - 25, so have been trying to implement a software serial to get around things, which is why its at 28 & 29.

I have this: https://a.co/d/dYnKfZ1 - although it seems it has several variations, which probably doesn't help. I'm trying to connect it via wifi to a Kasa Smart Plug HS103P4, to turn on/off a 120v pump.

Will this compile and "upload?" It does nothing and contains nothing, but it is a first step.

void setup(){}
void loop(){}

Then fill it with simple sketch...

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  Serial.print(" x");
}

Post your results.

why not use one of the hardware serial ports for communication with the ESP-01?
e.g. Serial1 on pins 18 and 19 and move whatever is currently using pins 18 and 19 to pins 28 and 29
also the mega uses 5V logic and the ESP-01 3.3V logic - you should use a potential divider on the Mega serial TX to ESP-01 Rx line

Simple example of ESP-01 communicating with a Mega
ESP-01transmits a 010101 pattern over Serial

// ESP8266 - transmit 0101010 in timing loop - read new loop time from keyboard
//  Serial 0101010 output will be sent to mega to blink LED

void setup() {
  Serial.begin(115200);
}

void loop() {
  static long timer=millis();
  //  send 010101 etc every time interval
  static int onoff=0, timeonoff=1000;
  if((millis()-timer)>timeonoff) {
    timer=millis();
    Serial.print(onoff);
    onoff=!onoff;                   // invert value 0 or 1
  }
  // if text available read next time value
  if(Serial.available()==0) return; // wait for serial input
  timeonoff=Serial.parseInt();      // read time value
  //Serial.println(timeonoff);
  delay(10);
  while(Serial.available()) Serial.read(); // flush input
}

Mega code receives 01010 pattern over Serial1 to switch LCD OFF/ON
a new blink period in mSec entered on keyboard is transmitted over Serial1 from Mega to ESP-01

// Mega - read Serial1 0101010 input from ESP8266 to blink LED
//    ESP-01 transmits 0 or 1 to Serial1 to switch LED OFF/ON
//    enter new blink period in mSec on keyboard, e.g. 500 for 0.5sec

void setup() {
  Serial.begin(115200);
  Serial.println("\nMega to ESP-01 Serial1 test");
  Serial.println("ESP-01 transmits 0 or 1 to Serial1 to switch LED OFF/ON");
  Serial.println("enter new blink period in mSec on keyboard, e.g. 500 for 0.5sec");
  Serial1.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  // if text available read next time value and send to ESP8266 over Serial1
  if (Serial.available() > 0) {         // wait for serial input
    int timeonoff = Serial.parseInt();  // read time value
    Serial1.println(timeonoff);         // send to ESP8266
    Serial.print("\n\Period in mSec sent to ESP-01 ");
    Serial.println(timeonoff);
    delay(10);
    while (Serial.available()) Serial.read();  // flush input
  }
  if (Serial1.available() > 0) {  // wait for Serial1 input
    char ch = Serial1.read();     // should 0 or 1 to blient LED
    Serial.print(ch);
    if (ch == '0') digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
    else digitalWrite(LED_BUILTIN, HIGH);           // turn the LED on (HIGH is the voltage level)
  }
}

mega serial monitor displays


Mega to ESP-01 Serial1 test
ESP-01 transmits 0 or 1 to Serial1 to switch LED OFF/ON
enter new blink period in mSec on keyboard, e.g. 500 for 0.5sec
01010101010101010101010101
Period in mSec sent to ESP-01 200
010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101Period in mSec sent to ESP-01 5000
1010
Period in mSec sent to ESP-01 500

photo

1 Like

I've tried about every different esp8266 library, but can't get anywhere. Could this be a bug in the Mac IDE (2.0.4)?

I've had no problems compiling & uploading standard sketches to the arduino, but when I add #include <ESP8266WiFi.h>, it can't find it: "fatal error: ESP8266WiFi.h: No such file or directory" - which doesn't make sense. I've deleted and tried different versions. I've added it in with boards manager, library manager, and the additional boards manager URL in settings. Under Boards Manager, I've tried compiling it as the arduino mega,

 fatal error: ESP8266WiFi.h: No such file or directory
 #include <ESP8266WiFi.h>

but also tried it as the Generic esp8266 Module:

. Variables and constants in RAM (global, static), used 28008 / 80192 bytes (34%)
β•‘   SEGMENT  BYTES    DESCRIPTION
╠══ DATA     1496     initialized variables
╠══ RODATA   920      constants       
β•šβ•β• BSS      25592    zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 59143 / 65536 bytes (90%)
β•‘   SEGMENT  BYTES    DESCRIPTION
╠══ ICACHE   32768    reserved space for flash instruction cache
β•šβ•β• IRAM     26375    code in IRAM    
. Code in flash (default, ICACHE_FLASH_ATTR), used 231620 / 1048576 bytes (22%)
β•‘   SEGMENT  BYTES    DESCRIPTION
β•šβ•β• IROM     231620   code in flash  

Let's ask @ptillisch about the MAC IDE (2.0.4)...

1 Like

Thanks, Horace! Wired it according to your layout. The red led lit up on the ESP-01 and it ran your first sketch without issue.... But the 2nd one, I enter the mSec, but nothing shows. I'm probably missing something simple, just not sure what.

if you just run the ESP-01 code does the serial monitor display "010101010"?

is your ESP an ESP-01 or an ESP-01S?
CH_P/EN needed to be connected to 3v3! to enable the ESP
esp-01S has on board pull-up for CH-PD. normal esp-01 doesn't. CH-PD is "enable" pin. it should be pulled up for esp8266 to work
from How-to-use-the-ESP8266-01-pins
The ESP-01S has 12K resistors on the board for GPIO0, RST and CH_PD

what ESP-01 programmer are you using?

if the Tools>board is a Mega the ESP8266 libraries will not be found

this looks like the code compiled for the ESP-01 OK

when I compile code for the ESP-01 using

#include <ESP8266WiFi.h>

I get

. Variables and constants in RAM (global, static), used 29408 / 80192 bytes (36%)
β•‘   SEGMENT  BYTES    DESCRIPTION
╠══ DATA     1504     initialized variables
╠══ RODATA   1808     constants       
β•šβ•β• BSS      26096    zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60439 / 65536 bytes (92%)
β•‘   SEGMENT  BYTES    DESCRIPTION
╠══ ICACHE   32768    reserved space for flash instruction cache
β•šβ•β• IRAM     27671    code in IRAM    
. Code in flash (default, ICACHE_FLASH_ATTR), used 270992 / 1048576 bytes (25%)
β•‘   SEGMENT  BYTES    DESCRIPTION
β•šβ•β• IROM     270992   code in flash   

Using library ESP8266WiFi at version 1.0 in folder: C:\Users\bb\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.1\libraries\ESP8266WiFi 
Using library ESP8266WebServer at version 1.0 in folder: C:\Users\bb\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.1\libraries\ESP8266WebServer 

It says ESP-01 on the board, but I can't confirm which chip is on it: the numbers printed on the chip aren't very legible. To make matters worse, the Makerfocus listing makes it seem like there are two different versions.

If I run the first one, I get

010101010

But if I run the 2nd one, and type in 200, the only thing that prints is:

Period in mSec sent to ESP-01 200

Edit: I did not know I had to use an ESP-01 programmer?

the ESP-01 comes with AT firmware pre-installed so you don't need to program it - see Using AT commands on the ESP8266

however, you can load you own code into the ESP-01 (overwriting the AT firmware) using a ESP-01 programmer

I assumed when you were including ESP8266WiFi.h that you were going to program the ESP-01 with your own code (you cannot use ESP8266WiFi.h on the Mega as it is not a ESP8266 processor)

I don't use the ESP-01 AT firmware prefering to load my own code into the device
for example, in post #3 the first program should be loaded into the ESP-01 and the second program loaded into the Mega - the ESP-01 transmits 0101010 characters to the Mega to switch the LED OFF/ON

perhaps you need to clarify what you wish to do?

Thanks, horace. The project I'm working on is for our brewery. I need a safe way to turn on/off a 120v pump and a 120v heating element, and figured connecting the ESP-01 to a Kasa Smart Plug HS103P4 via wifi was safer than running wires from the arduino. Safety first, so the entire project has been 12v and low amps. I'm using Logic level MOSFETs across several breadboards, to run the 12v valves which are timed to keep the amperage low enough to run 16x valves, 2x temperature sensors, etc., without overheating or overloading the breadboards. I want this to be a project I can teach the next person how to use & program. Even though there's a lot going on, no breadboard gets more than 2a at any given time, due to the timing. It has been working without issue, but I need to add the pump and heating element in now, in case someone walks away and there's a power outage. So the work around is to use the ESP-01 and Smart Plugs.

have you experimented with an ESP-01 controlling a Kasa smart plug ?
e.g. can you control it using AT commands from a ESP-01?
try a web search for arduino Kasa Smart Plug

have you seen the ESP0101S-RELAY-MODULE - could you use this to switch the pump and heater?

No, this kinda is my experiment lol. If I can get it to turn on and off, I was hoping to drop it into the full sketch and add a few for/while loops without too much issue. I'll take a look at the AT commands a little later this afternoon, when I have a few minutes to digest what I'm reading better - I'm assuming it's not as simple as sending an "on" or "off" command to the Kasa?

The ESP0101S-RELAY-MODULE's max says 10a, but the pump and element pull closer to 1500w each, which is part of the headache; I went with the kasa, because it was rated up to 1800w. Thanks again!

no idea - try a web search for arduino Kasa Smart Plug and see if there are any examples

the stated maximum loads of low cost relays are for resistive loads, e.g. a heater
for inductive loads double the requirement
more expensive contactors have AC1 and AC3 ratings, e.g. Contactor, LC1D, 12 A, DIN Rail, 230 VAC, 3PNO, 3 Pole, 5.5 kW

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