Home » Projects » Electronics Projects » Design NodeMCU IoT – Follow an Outline

Design NodeMCU IoT – Follow an Outline

design nodemcu iot

Once you set your goals for an IoT device, you design NodeMCU IoT code. You follow a simple outline.

You can do everything in the Arduino IDE, or better yet, Visual Studio Community with Visual Micro. First, you need to install some libraries to support your devices. This saves you a lot of time. Just find, download and install libraries into your IDE.

#include <PCF8574.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>

I am using libraries to easily control the PCF8574 GPIO expansion boards, run the WiFi and automate remote over-the-air firmware updates. Most libraries will make C++ objects available for your use.

/* Objects */
/* Server listens for client to connect on Port 4000 */
WiFiServer server(WIFI_PORT);
/* Use client to communicate with controlling device */
WiFiClient client;
/* Objects to control relays with different I2C addresses */
PCF8574 B_RX(0x38); // Relay Bank for Receiver Control
PCF8574 B_LOOP(0x39); // Relay bank for Loop Control

Occasionally, you will find several different libraries available. So, you might experiment and find one best meeting your needs.

/* Wifi Variables */
const char* ssid = "Your Network Name";
const char* password = "Your Network Password";
const IPAddress SERVERIP(10, 0, 0, 241); // Static IP
const IPAddress GATEWAY(10, 0, 0, 1);
const IPAddress SUBNET(255, 255, 255, 0);
const IPAddress DNS1(10, 0, 0, 1);
const IPAddress DNS2(10, 0, 0, 1);
String Buffer;
boolean ClientConnected = false;
boolean LastClientConnected = false;

Finally, you need some code to setup your WiFi. For me, I use a static IP address on my network, and a string buffer for passing commands.

Design NodeMCU IoT with Definitions

Definitions are simply names you can give to constant values. You do defines up front and then use them anywhere in your code. For me, this is a useful way to avoid making multiple edits later in the code.

#define DEBUG
#define RELAY_ON LOW
#define RELAY_OFF HIGH
#define WIFI_PORT 4000
/* PCF8574 pins for Relay Bank B_RX */
#define RY_R1 7 // Radio 1
#define RY_R2 6
#define RY_R3 5
#define RY_R4 4 // Radio 4
#define RY_F 3  // Medium Wave Filter
/* PCF8574 pins for Relay Bank B_LOOP */
#define RY_LM1_1 0 // Control dual loop modes AAA-1C
#define RY_LM1_2 1
#define RY_LM1_3 2
#define RY_LM2_1 3
#define RY_LM2_2 4
#define RY_LM2_3 5

These are the preliminaries for design NodeMCU IoT. Next comes the code to do some work.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.