top of page
Writer's pictureEmbeddedBrew

Getting Started with Arduino IoT Cloud and NodeMCU to Control an LED using Internet



Controlling an LED with NodeMCU through Arduino IoT Cloud is a fantastic beginner project that introduces you to the world of IoT (Internet of Things). Follow these simple steps to get started:


Step 1: Set Up Arduino IoT Cloud

1. Create an Arduino Account:

   - Go to [Arduino Create](https://create.arduino.cc/) and sign up for a free account or log in if you already have one.


2. Access Arduino IoT Cloud:

   - Navigate to the [Arduino IoT Cloud](https://create.arduino.cc/iot) from the Arduino Create dashboard.


3. Set Up a New Thing:

   - Click on "Create Thing" to set up a new IoT device.

   - Give your Thing a name, such as "LED_Controller."



Step 2: Configure the Device

1. Add a Device:

   - Click on "Add Device" and select "Set up a third-party device."

   - Choose "ESP8266" and select NodeMCU 1.0 from the list of supported boards.


2. Generate Device ID and Secret Key:

   - Follow the instructions to generate a Device ID and Secret Key. Note these down as they are needed later.


3. Install the Arduino IoT Cloud Library:

   - Open the Arduino IDE and install the `ArduinoIoTCloud` and `Arduino_ConnectionHandler` libraries via the Library Manager.


Step 3: Write the Code

1. Set Up the Sketch:

   - Open a new sketch in the Arduino IDE.

   - Include the necessary libraries at the beginning of your sketch:

 #include "thingProperties.h"

2. Define the LED Pin:

   - Define the pin where the LED is connected (e.g., D2 on NodeMCU):

const int ledPin = D2;

3. Setup Function:

   - Initialize the LED pin and IoT Cloud connection:

void setup() {
         // Initialize the serial and wait for the port to open:
         Serial.begin(9600);
         delay(1500); 
         initProperties();
         // Connect to Arduino IoT Cloud
         ArduinoCloud.begin(ArduinoIoTPreferredConnection);
         // Initialize LED pin
         pinMode(ledPin, OUTPUT);
         // Sync initial state
         ArduinoCloud.addCallback(ArduinoIoTPreferredCallback);
     }

4. Loop Function:

   - Use the loop function to keep the connection active:

void loop() {
         ArduinoCloud.update();
     }

5. Callback Function:

   - Create a function to handle the LED control:

void onLedChange() {
        digitalWrite(ledPin, ledState);
     }

Step 4: Connect and Upload

1. Connect NodeMCU:

   - Connect your NodeMCU board to your computer via USB.


2. Upload the Sketch:

   - Select the correct board and port from the Tools menu in the Arduino IDE.

   - Click "Upload" to upload the code to your NodeMCU.


3. Configure Network Credentials:

   - In the `thingProperties.h` file, enter your Wi-Fi SSID and password.

const char SSID[]     = "your_SSID";
const char PASS[]     = "your_PASSWORD";  

Step 5: Create a Dashboard

1. Add a Dashboard:

   - In Arduino IoT Cloud, go to the Dashboards section and create a new dashboard.


2. Add a Widget:

   - Add a switch widget to control the LED.

   - Link the widget to the `ledState` variable.


3. Control Your LED:

   - Use the dashboard switch to turn the LED on and off from anywhere in the world!



Conclusion

By following these steps, you'll successfully control an LED using NodeMCU and Arduino IoT Cloud. This project provides a solid foundation for more complex IoT applications. Also checkout our website for more projects and explore the Skill-Hub by EmbeddedBrew for Enhancing your Skills in IoT. Happy tinkering!


14 views0 comments

Comments


bottom of page