Sponsored Content
Top Forums Programming NodeMCU ESP8266 Blynk SSL Application for Linux Server Load Averages Post 303043556 by Neo on Friday 31st of January 2020 11:56:26 AM
Old 01-31-2020
Update; Version 0.2

Changes: Added 30 minute session activity for members, guests and bots. Reorganized gauges. Changed name to "Load Averages & Users"

NodeMCU ESP8266 Blynk SSL Application for Linux Server Load Averages-img_2153333406a8-1jpeg


Code:
/*************************************************************
  This Arduino sketch is for the NodeMCU ESP8266  v0.2
  The code makes a GET request to a remote Linux server PHP API using SSL
  and GETs the Linux load averages and basic user stats as a simple string, parses the
  string in a very basic way display the results on a Blynk apply
  using virtual pins v0, v1, v2, v3, v4, and v5.

  The entire sketch should be rewritten to use JSON.

  Use anyway you wish, freely and at your own risk of course.

  Neo https://www.unix.com  January 31, 2020
  v0.11  removed  delay(2000); //GET Data at every 2 seconds per PeteKnight
    https://community.blynk.cc/t/blynk-project-nodemcu-esp8266-ssl-application-for-linux-server-load-averages/43305

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on NodeMCU.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right NodeMCU module
  in the Tools -> Board menu!


  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define DEBUG false
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
//#include <ArduinoJson.h> . // unused for now
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YOUR_BLYNK_AUTH_KEY";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YOUR_WIFI_SSID";
char pass[] = "YOUR_SECURE_PASSWORD";

const char *host = "www.yourserver.com";
const int httpsPort = 443;  //HTTPS= 443 and HTTP = 80

//SHA1 fingerprint of certificate. Use your web browser to view the SHA1 fingerprint and copy
//Reply in this discussion if you don't know how to get the SHA1 fingerprint of your HTTPS website, thanks.
const char fingerprint[] PROGMEM = "AA BB DD 7B C0 FE CF 3C A8 2B 6D 31 DB 4B 9F C3 5B 6E A0 DD"; 

void setup()
{
  // Debug serial console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);

}

void loop()
{
  Blynk.run();
  timer.run();
}

void myTimerEvent()
{
  WiFiClientSecure httpsClient;    //Declare object of class WiFiClient

  Serial.println(host);

  Serial.printf("Using fingerprint '%s'\n", fingerprint);
  httpsClient.setFingerprint(fingerprint);
  httpsClient.setTimeout(15000); // 15 Seconds
  delay(1000);

  Serial.print("HTTPS Connecting");
  int r = 0; //retry counter
  while ((!httpsClient.connect(host, httpsPort)) && (r < 30)) {
    delay(100);
    Serial.print(".");
    r++;
  }
  if (r == 30) {
    Serial.println("Connection failed");
  }
  else {
    Serial.println("Connected to web");
  }

  String Link;
  long rssi = WiFi.RSSI();
  String macaddr = WiFi.macAddress();
  Serial.print("RSSI:");
  Serial.println(rssi);
  Serial.print("MAC: ");
  Serial.println(macaddr);

  Link = "/yourAPIdirectory/?type=t&rssi=" + String(rssi) + "&mac=" + macaddr;
  Serial.print("requesting URL: ");
  Serial.println(host + Link);

  httpsClient.print(String("GET ") + Link + " HTTP/1.1\r\n" +
                    "Host: " + host + "\r\n" +
                    "Connection: close\r\n\r\n");

  Serial.println("request sent");

  while (httpsClient.connected()) {
    String line = httpsClient.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }

  Serial.println("reply was:");
  Serial.println("==========");
  String line;
  while (httpsClient.available()) {
    line = httpsClient.readStringUntil('\n');  //Read Line by Line
    Serial.println(line); //Print response
  }
  Serial.println("==========");
  Serial.println("closing connection");

  // crude way to parse string - should convert to json later
  int ind1 = line.indexOf(':');  //finds location of first :
  String l1 = line.substring(0, ind1);   //captures first data String
  int ind2 = line.indexOf(':', ind1 + 1 ); //finds location of second :
  String l5 = line.substring(ind1 + 1, ind2 + 1); //captures second data String
  int ind3 = line.indexOf(':', ind2 + 1 ); //finds location of third :
  String l15 = line.substring(ind2 + 1, ind3 + 1);//captures second data String
  int ind4 = line.indexOf(':', ind3 + 1 ); //finds location of third :
  String bots = line.substring(ind3 + 1, ind4 + 1);//captures second data String
  int ind5 = line.indexOf(':', ind4 + 1 ); //finds location of third :
  String guests = line.substring(ind4 + 1, ind5 + 1);//captures second data String
  int ind6 = line.indexOf(':', ind5 + 1 ); //finds location of third :
  String members = line.substring(ind5 + 1, ind6 + 1);//captures second data String
  if (DEBUG) {
    Serial.println(String(l1)); Serial.println(l5);; Serial.println(l15); //Print response
    Serial.println(String(bots)); Serial.println(guests);; Serial.println(members);
  }

  if (l1.toFloat() > 0) {
    l1.replace(":", ""); l5.replace(":", ""); l15.replace(":", "");
    bots.replace(":", ""); guests.replace(":", ""); members.replace(":", "");
    Blynk.virtualWrite(V0, l1.toFloat());
    Blynk.virtualWrite(V1, l5.toFloat());
    Blynk.virtualWrite(V2, l15.toFloat());
    Blynk.virtualWrite(V3, members.toInt());
    Blynk.virtualWrite(V4, guests.toInt());
    Blynk.virtualWrite(V5, bots.toInt());
  }

}

This more-than-like will be the last Blynk app I create because, with only six gauges, I have reached the limit of the "freebies" and if I create more apps and add more widgets, I will start to have to pay for them.

I may add some "color logic" when the numbers exceed certain thresholds, otherwise I'm most "done and dusted" with this test app.

Anyway, time to move on to another Arduino module.

This app was fun, fast useful and easy to build. Recommended for sure.
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Server load (Unix/Linux, Redhat, CPanel)

Hello, I'm facing a big problem with my hosting server (Dual Xeon 2.4GHz), I'm having a load in the CPU usage and the memory (maybe it's related) ALSO mySQL: Server Load 5.34 (2 cpus) (to 22 sometime) Memory Used 68.4 % (to 70% sometime) When I go to 'CPU/Memory/MySQL Usage' I found: ... (3 Replies)
Discussion started by: Kh@lid
3 Replies

2. Cybersecurity

linux prog for server space, load etc

Hi all what are the ways by which we can know and generate a report of the space remaining, memory(ram) used and the load on the server over a period of time. (1 Reply)
Discussion started by: arlan
1 Replies

3. UNIX for Advanced & Expert Users

Sun: High kernel usage & very high load averages

Hi, I am seeing very high kernel usage and very high load averages on my system (Although we are not loading much data to our database). Here is the output of top...does anyone know what i should be looking at? Thanks, Lorraine last pid: 13144; load averages: 22.32, 19.81, 16.78 ... (4 Replies)
Discussion started by: lorrainenineill
4 Replies

4. Linux

how to setup a virtual IP to control 2 server load for linux

Hi anyone know how to setup a setup a virtual IP to control 2 server load for linux? i only have 2 server, i don want to buy another just for the load balance... is there a way to do it? Sumemr (0 Replies)
Discussion started by: summerpeh
0 Replies

5. Shell Programming and Scripting

Create a list of load averages

`/proc/loadavg` give me three indicators of how much work the system has done during the last 1, 5 & 15 minutes. How can i get a list of load averages that each averaged over the last minute for 10 minutes? (2 Replies)
Discussion started by: navidlog
2 Replies

6. Solaris

Sparc Solaris 10 load averages

our server is running oracle database, it has: load average: 1.77, 1.76, 1.73 using only one cpu. is that too high? thanks. (4 Replies)
Discussion started by: orange47
4 Replies

7. Red Hat

Red Hat application server ssl keystore problem

A client is accessing our JBoss server. In the past, we set up a keystore and everything worked fine. That certificat expired and we've installed the new one. Now the client is getting the following error - HTTP/1.1 500 Internal Server Error Date: Mon, 14 Apr 2014 13:25:44 GMT Server:... (1 Reply)
Discussion started by: kkinney
1 Replies

8. AIX

AIX LDAP client authenticate against Linux Openldap server over TLS/SSL

Hi folks, How can i configure an AIX LDAP client to authenticate against an Linux Openldap server over TLS/SSL? It works like a charm without TLS/SSL. i would like to have SSL encrypted communication for ldap (secldapclntd) and ldapsearch etc. while accepting every kind of certificate/CA.... (6 Replies)
Discussion started by: paco699
6 Replies

9. Programming

Wuhan Coronavirus Status for China - Rapid Prototype Blynk App with ESP8266

Here is a rapid prototype app I just put together which might be of interest to some people. Basically, I have parsed the data from a Chinese web site which is tracking the Wuhan coronavirus, and cache that data every minute via a local cron file and make a simple API available to a Blink app. ... (6 Replies)
Discussion started by: Neo
6 Replies
All times are GMT -4. The time now is 12:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy