Rodney Copeland
Published

Monitor/Control NodeMCU Blinky From Phone in Minutes

Using OnSiteMonitor S/W, you can be monitoring the status and issuing controls to turn your device on/off in just a few minutes.

BeginnerFull instructions provided903
Monitor/Control NodeMCU Blinky From Phone in Minutes

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

OnSiteMonitor.com desktop software

Story

Read more

Schematics

NodeMCU On Board LED

Code

BlinkyLED for OnSiteMonitor

Arduino
This code creates a web server on the NodeMCU board which accepts commands from the OnSiteMonitor system. This allows the current status of the device to be reported. Commands from OnSiteMonitor can be issues by an event, scheduled, or on demand.
//****************************************************************************
// On Board Blinking LED
// This sketch will turn the device's LED light on & off
// 
// Developed to work with OnSiteMonitor
//****************************************************************************

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <Ticker.h>

Ticker tickerOSWatch;

#define OSWATCH_RESET_TIME 30

static unsigned long last_loop;

void ICACHE_RAM_ATTR osWatch(void) 
{
  unsigned long t = millis();
  unsigned long last_run = abs(t - last_loop);
  if(last_run >= (OSWATCH_RESET_TIME * 1000)) 
  {
    // save the hit here to eeprom or to rtc memory if needed
      ESP.restart();  // normal reboot 
      //ESP.reset();  // hard reset
  }
}


// WiFi parameters to be configured
const char* ssid = "updateme";
const char* password = "updateme";

//Required static values for device
String account_id = "updateme";
String device_type = "NodeMCU";
String device_id = "BlinkyLED";
String command_class = "1";

//Initial values
String ip_address = "";
String osm_ip_address = "";
String current_status = "";
int httpCode;

// Status Function
uint8_t pin_status( uint8_t level, int pin)
{
  uint8_t state;
  digitalWrite(pin, 1);
  pinMode(pin, INPUT);
  state = digitalRead(pin);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, level);
  return state;
}

ESP8266WebServer server(80);   //instantiate server at port 80 (http port)

String page = "";
void setup(void)
{
  last_loop = millis();
  tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), osWatch);

  //make the LED pin output and initially turned off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  current_status = "OFF";      
   
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

  ip_address = WiFi.localIP().toString();
    
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");

  //Serial.println(WiFi.localIP());
  Serial.println(ip_address);
  
  //Get the IP Address for OnSiteMonitor
  HTTPClient http;  //Declare an object of class HTTPClient
  http.begin("http://www.onsitemonitor.com/osm_ip.aspx?aid=" + account_id);
  httpCode = http.GET();
  if (httpCode > 0) { //Check the returning code
    osm_ip_address = http.getString();   //Get the request response payload
    osm_ip_address.replace("\n","");
    osm_ip_address.replace("\r","");
  }

  Serial.println("OSM IP Address:" + osm_ip_address);   
  http.begin("http://www.onsitemonitor.com/update_diy_ip_address.aspx?dev=" + account_id + "&dt=" + device_type + "&did=" + device_id + "&ip=" + ip_address);
  httpCode = http.GET();    //Send the request
  Serial.println(httpCode);
  http.end();

  server.on("/CMD=STATUS", []()
  {
    //Report the current status of the device
    server.send(200, "text/plain", current_status);
    Serial.println("Current Status = " + current_status);
  });
  
  server.on("/CMD=ON", []()
  {
    //Process the command to turn the On Board LED ON
    server.send(200, "text/plain", "SUCCESS");
    digitalWrite(LED_BUILTIN, LOW);
    current_status = "ON";      
    Serial.println("ON");
  });
  
  server.on("/CMD=OFF", []()
  {
    //Process the command to turn the On Board LED OFF
    server.send(200, "text/plain", "SUCCESS");
    digitalWrite(LED_BUILTIN, HIGH);
    current_status = "OFF";      
    Serial.println("OFF");
  });
  
  server.begin();
  Serial.println("Web server started!");
}
 
void loop(void)
{
  server.handleClient();
  last_loop = millis();
  delay(1000);
}

Credits

Rodney Copeland
2 projects • 4 followers
Rodney Copeland IT Professional with a special interest in the design and development of IoT automation.

Comments