1

So this is probably a very simple question. I have my NodeMCU set up and working where it connects to wifi and sends a push notification. Currently it does that automatically when connected to power.

What I want to do is connect it to my Leonardo Micro so that at a part in the Leonardo sketch it sends a signal to the NodeMCU to run the sketch to trigger the push notification. So simply, which ports should I connect and what code should I change.

Here is my working NodeMCU code.

    #include <ESP8266WiFi.h>

// Wifi Settings
const char* ssid = "MySSID";
const char* password = "MyPassword";
// Pushover settings
char pushoversite[] = "api.pushover.net";
char apitoken[] = "MyAPIToken";
char userkey [] = "MyUserKey";
int length;
WiFiClient client;
void setup() {
  Serial.begin(9600);
  delay(10);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Print the IP address
  Serial.print(WiFi.localIP());

}

void loop()
{
 pushover("OMG, Yes it works!!!");  
 delay(60000); 
}

byte pushover(char *pushovermessage)
{
 String message = pushovermessage;

 length = 81 + message.length();

if(client.connect(pushoversite,80))
 {
   client.println("POST /1/messages.json HTTP/1.1");
   client.println("Host: api.pushover.net");
   client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
   client.print("Content-Length: ");
   client.print(length);
   client.println("\r\n");;
   client.print("token=");
   client.print(apitoken);
   client.print("&user=");
   client.print(userkey);
   client.print("&message=");
   client.print(message);
   while(client.connected())  
   {
     while(client.available())
     {
       char ch = client.read();
       Serial.write(ch);
     }
   }
   client.stop();
 }  
}

And my Leonardo code

#include <Keyboard.h>
#include <Mouse.h>
char returnKey = KEY_RETURN;
char tabKey = KEY_TAB;
void setup() {
  Keyboard.begin();
  Mouse.begin();

  delay(4000); //Testing Delay
// Language Select Screen
  Keyboard.write(returnKey);
  delay(30000);
// Welcome Screen 
  Mouse.click(MOUSE_LEFT);
  delay(100);
  Keyboard.write(tabKey);
  delay(100);
  Keyboard.write(tabKey);
  delay(100);
  Keyboard.write(' ');
  delay(2000);
// Select Keyboard 
  Keyboard.write(tabKey);
  delay(100);
  Keyboard.write(tabKey);
  delay(100);
  Keyboard.write(tabKey);
  delay(100);
  Keyboard.write(' ');
  delay(2000);
  TELL NODEMCU TO SEND PUSH NOTIFICATION HERE
}

void loop() {
}

As a reminder the pinout on my boards are as follows.

Leonardo:

Leonardo Layout

NodeMCU:

NodeMCU Layout

  • 1
    First make it work from a button. Then replace the button with the Leonardo. – Majenko Jan 31 at 14:20
2

HW part:

It's like button, simply connect arduino micro pin to nodeMCU pin and GND of arduino to GND of nodeMCU.

It's also good idea to use two resistors as voltage divider because arduino micro works with 5V and ESP8266 (nodeMCU) with 3V3. 5V can destroy ESP8266 but this thing is still not really clear. Resistor divider connect as in this link link about resistor divider for uart but instead of TX put arduino micro and instead of RX nodeMCU. resistor divider schematic


Code part:

On the arduino micro part use:

  • In void setup(): pinMode(PIN, OUTPUT);

  • In your marked place:

digitalWrite(PIN, HIGH);
delay(100);   //short wait time
digitalWrite(PIN, LOW);

nodeMCU part:

  • In void setup(): pinMode(PIN, INPUT);

  • Remove everything from void loop() and put there this:

if(digitalRead(PIN) == 1) pushover("OMG, Yes it works!!!");

In both codes select the PIN according to your choice.

  • Thanks Matej, exactly what I was looking for and I think i'm beginning to understand things a bit more now too. Resisters are still something I need to read up on. I'd bought a rechargeable battery shield I was going to use to power the mini and the NodeMCU but I guess even if it's powered that way I still need the resister between the digital pins? – Danny Shepherd Jan 31 at 17:25
  • @DannyShepherd Resistor is simple thing, it causes voltage drop which you can calculate from ohm law (U = R*I). In this case it's not so simple because we are working with mcu pins which current consumption is really low - few micro amps. – Matej Jan 31 at 18:03
  • @DannyShepherd If we want to put 5V logic to 3V3 logic, we need to have voltage drop 1.7V (5-3.3) on first resistor, that means on second resistor will be voltage drop 3V3 (between nodeMCU pin and GND). But why two resistor? Because if you use just one, current is so small that value of resistor will be so big to achieve voltage drop equal to 1.7V. So we simple take two resistor in series and they can create proper current. We can apply formula U1/U2 = R1/R2 which came from using ohm law and then trim current from it because in serial connection current is same. – Matej Jan 31 at 18:03
  • @DannyShepherd So if both arduinos will have same voltage level you don't have to use voltage divider or level shifter. Als some MCUs are 5V tolerant but specially ESP8266 is not or it's not still clear. So is this answer for your question? :) – Matej Jan 31 at 18:05
  • 1
    Hey @Matej, that's really well explained thanks. Just picked up a back of resisters so I will play around with this and hopefully not fry any boards! Just to double check something if I have a shield that powers the Leonardo with 5v and the NodeMCU with 3.3v - the digital pinout from the Arduino would still try to send 5V down as well (and thus need the resister) I assume so, just checking the basics of the current flow. Thanks! – Danny Shepherd Feb 3 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.