Build a weather station using esp8266, ssd1306, dht22

In this post I present an overview building a weather station I could place in my garden, hoping to monitor temperature and humidity. I’ll use a esp8266 NodeMCU WiFi module to stream this data to Losant, an IOT platform. We’ll also display these readings on a ssd1306 0.96″ OLED screen. The temperature and humidity are observed using a DHT22 sensor module.  Eventually I’ll add moisture probes that will be used to trigger relays used to active a sprinkler during the hot, dry months of July and August in Wisconsin. Other goals include adding sensors to monitor rainfall, wind speed, and wind direction.

My code went through a few iterations to start. I wanted to take readings from the DHT22 and put them on the console monitor. I also printed some simple banner to the OLED display as well. At one point I found an i2c scanner to ensure my laptop could see the esp8266. In the end, I combined code with the examples from Losant to stream sensors readings to the cloud over WiFi and to a 128×64 OLED.

i2c Scanner

// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#include <Wire.h>
 
 
void setup()
{
 
 Serial.begin(9600);
 while (!Serial); // Leonardo: wait for serial monitor
 Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
 byte error, address;
 int nDevices;

Wire.begin();
 
 Serial.println("Scanning...");
 
 nDevices = 0;
 for(address = 1; address < 127; address++ )
 {
 // The i2c_scanner uses the return value of
 // the Write.endTransmisstion to see if
 // a device did acknowledge to the address.
 Wire.beginTransmission(address);
 error = Wire.endTransmission();
 
 if (error == 0)
 {
 Serial.print("I2C device found at address 0x");
 if (address<16)
 Serial.print("0");
 Serial.print(address,HEX);
 Serial.println(" !");
 
 nDevices++;
 }
 else if (error==4)
 {
 Serial.print("Unknown error at address 0x");
 if (address<16)
 Serial.print("0");
 Serial.println(address,HEX);
 } 
 }
 if (nDevices == 0)
 Serial.println("No I2C devices found\n");
 else
 Serial.println("done\n");
 
 delay(5000); // wait 5 seconds for next scan
}

128×64 OLED Test

The small display relies on the i2c communication protocol, as does the DHT22, making connections to the esp8266 very straightforward. The display has 4 pins: 3-5v logic, ground, serial clock (SCL) and serial data (SDA).

It’s essential to connect SCL,SDA pins of oled display to pins on esp8266 marked D1 and D2.

The key is to load the Adafruit_GFX and Adafruit_SSD1306 libraries.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup() {
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

// Clear the buffer.
 display.clearDisplay();
 display.display();

display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.println("Hello from:");
 display.println("http://arduino-er.blogspot.com/");
 display.display();

}

void loop() {
 // put your main code here, to run repeatedly:

}

Read DHT22 and display on OLED – Borrowed from Losant

/**
 * Example for reading temperature and humidity
 * using the DHT22 and ESP8266
 *
 * Copyright (c) 2016 Losant IoT. All rights reserved.
 * https://www.losant.com
 */

#include "DHT.h";
#define DHTPIN 2 // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
DHT dht(DHTPIN, DHTTYPE);

//#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
 Serial.begin(9600);
 Serial.setTimeout(2000);

/*display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.println("Temperature:");
 display.println("Humidity");
 display.display();*/
 
 // Wait for serial to initialize.
 while(!Serial) { }

Serial.println("Device Started");
 Serial.println("-------------------------------------");
 Serial.println("Running DHT!");
 Serial.println("-------------------------------------");

}

int timeSinceLastRead = 0;
void loop() {

// Report every 2 seconds.
 if(timeSinceLastRead > 2000) {
 // Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h = dht.readHumidity();
 // Read temperature as Celsius (the default)
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit (isFahrenheit = true)
 float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t) || isnan(f)) {
 Serial.println("Failed to read from DHT sensor!");
 timeSinceLastRead = 0;
 return;
 }

// Compute heat index in Fahrenheit (the default)
 float hif = dht.computeHeatIndex(f, h);
 // Compute heat index in Celsius (isFahreheit = false)
 float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");
 Serial.print(h);
 Serial.print(" %\t");
 Serial.print("Temperature: ");
 Serial.print(t);
 Serial.print(" *C ");
 Serial.print(f);
 Serial.print(" *F\t");
 Serial.print("Heat index: ");
 Serial.print(hic);
 Serial.print(" *C ");
 Serial.print(hif);
 Serial.println(" *F");

// Clear the buffer.
 display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // initialize with the I2C addr 0x3D (for the 128x64)
 // Clear the buffer.
 display.clearDisplay();
 display.display();
 
 //display.clearDisplay();
 //display.display();

display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 //char temperature[5] = "";
 //dtostrf(hif, 3,1,temperature);
 String buf;
 buf += F("Temperature: ");
 buf += String(hif,2);
 buf += " F";
 display.println(buf);
 buf="";
 buf += F("Humidity: ");
 buf += String(h,2);
 buf += "%";
 //char humidity[5] = "";
 //dtostrf(h, 3,1,humidity);
 display.println(buf);
 display.display();

/* //Send Temp & Humidity to OLED
display.clearDisplay();
display.display();
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.println("Temperature: *F");
 display.println("Humidity: %");
 display.display();*/
/*
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.println("");
 display.setTextSize(2);
 display.println("Temperature: F");
 display.println("Humidity: %");
 display.display();
 */

timeSinceLastRead = 0;
 }
 delay(100);
 timeSinceLastRead += 100;
}

Losant Example

The Whole Kaboodle

 

 

Reset Computer Machine Password

Occasionally I’ll receive a report that domain users cannot login to a specific laptop, citing an error message “The trust relationship between this workstation and the primary domain failed.” It’s worth noting that although domain accounts cannot be used to sign in, local computer accounts may still be used.

Microsoft has issued two solutions to this problem. One solution involves logging into the PC using a local computer account and taking the PC off the domain, reboot, sign in again with local account, and once more join the laptop to the domain. This works, but it’s slow.

A better solution that takes less time requires us to use a powershell cmdlet. Again, login to the PC with a local account and issue the following in Powershell running with Administrative rights.

$creds = Get-Credential
Reset-ComputerMachinePassword -Credential $creds -Server dc1.domain.edu

The first command will display a prompt that will collect username and password that will be used to execute the next cmdlet. Be aware you must provide an account with Domain Admin privileges. After execution, log out. You may now log in again using domain accounts.

Youtube Restricted Mode

Youtube is a great resource for learning and entertainment. However, it can also be a great distraction too. In addition to that, there is a considerable amount of content that many would find questionable at best. In order to filter the most offensive content we can employ Youtube restricted or moderate restricted mode.

Although there are many ways to accomplish this, including setting domain wide user policies in Google admin console, this will only target folks who are using their Google profile that was provided by their employer. To ensure we apply this policy to everyone, we can use DNS redirection.

It’s worth noting some articles suggested creating a DNAME record in each zone that points to either restrict.youtube.com or restrictmoderate.com. In my experience this failed. I had success creating an A record instead for each zone.

This article makes the assumption you administer a Windows based DNS server. To begin, create 5 new forward lookup zones:

www.youtube.com

m.youtube.com

www.youtube-nocookie.com

youtube.googleapis.com

youtubei.googleapis.com

Within each zone, create a new A record. Leave the Name field blank and enter 216.239.38.120 in the IP Address field. Use 216.239.38.120 for restricted mode, or 216.239.38.119 for moderate restricted mode.

After having done this you should be able to open a browser and verify the changes by heading to https://www.youtube.com/check_content_restrictions.

 

Global Protect LDAP over SSL

I recently had need to reconfigure LDAP settings on a Palo Alto firewall. I had retired two production domain controllers and stood up two new boxes in their place. Remote access VPN users are authenticated by the firewall via LDAP on port 389, or LDAP over SSL (LDAPS) on port 636.

The quickest solution was to provide the firewall with the FQDN, IP Address, and port numbers needed to communicate with the new domain controllers using the LDAP protocol. Ensure ‘Require SSL/TLS secured connection’ is not selected

I make this note as I had assumed the new domain controllers were talking LDAPS by default but I was wrong. A quick look over the authentication log, authd.log, using the command  > tail follow yes mp-log authd.log, revealed this:

2018-07-11 09:05:04.826 -0500 Error: pan_authd_ldap_bind(pan_authd_shared_ldap.c:615): Failed to bind ldap (Can't contact LDAP server)
2018-07-11 09:05:04.826 -0500 Error: pan_auth_create_a_ldap_session(pan_auth_svr_cctxt.c:1971): Failed to bind, get out
2018-07-11 09:05:04.827 -0500 debug: pan_auth_response_process(pan_auth_state_engine.c:4073): auth status: auth server not available

A more secure implementation would take advantage of LDAPS. In a nutshell, you will need to generate your own self-signed certificate, or better, install a certificate provided to you by a CA. Also, LDP.exe is a helpful utility that can verify a successful connection or binding to your directory. It can be obtained by downloading the Microsoft Remote Server Administration Tools (RSAT)

In this post I’ll review configuring my new domain controllers with self-signed certificates in lieu of obtaining signed certificates from a CA. To start, open Server Manager on a new domain controller and install the Active Directory Certificate Services role. As you proceed through the wizard, be sure to configure AD-CS as an Enterprise, Root CA. 

Open the Certificate Templates Console: Launch mmc.exe and add the Certificate Templates snap in and click OK. In the left pane of the snap in right click the domain controller and click View Object Identifiers. In the dialog window, scroll down to the Server Authentication Policy and ensure the Object Identifier is displayed as 1.3.6.1.5.5.7.3.1.

Add the Certificates snap in to MMC and instruct the Certificates snap in to manage certificates for the Computer account. Expand the Personal folder in Certificates snap in and right click on the Certificates sub folder, select All Tasks -> Request New Certificate. Select Active Directory Enrollment Policy and click Next. Select Domain Controller and click Enroll. Once finished you’ll see a certificate issued to each domain controller in the domain. You can view these new certificates now in the Local Computer Certificate Store, specifically within the Personal->Certificates sub-node.

At this point, your domain controller should be configured to respond to connection attempts using LDAPS. You can verify this using the LDP.exe utility from Microsoft.

 

 

 

Resources:

How to Troubleshoot LDAP Authentication

Troubleshooting GlobalProtect

Configuring Secure LDAP Connection on Server 2016

 

 

Import Palo Alto Firewall Appliance into GNS3

After obtaining a virtual machine in the form of an OVA file, we need to expand the archive and convert the vmdk to a qcow2 format as specified during the GNS3 Import Appliance dialogue.  The full instructions to convert an OVA to a qcow2 format can be found at cloud.garr.it.

tar xvf pa-vm-esxi-8.0.0.ova

A prerequisite package must be installed to provide us with the means to make the conversion.

apt -y install qemu

With the qemu package installed, we can begin the conversion.

qemu-img convert -f vmdk $FILE-disk1.vmdk -O qcow2 $FILE.qcow2

Virtualbox Shared Folder Missing

Here is a short write up of my experience configuring Shared Folders under Oracle Virtualbox 5.2.2 for a Kali 2017.3  appliance. After importing the appliance, I am warned USB 2.0 can only be supported after installing the Oracle Virtualbox Extension Pack, obtained here. You can double click on the installer and Virtualbox will pick up and carry the installation through completion. After that, I reboot.

Next step is to install the  virtualbox-guest-x11 package instead of using the supplied package available through the cdrom0 device in vm settings. I try the advice listed here. After a reboot, I check /media and there is still no monted folder.

Turns out I need to manually mount the drive in my guest operating system.

mount -t vboxsf -o uid=1000,gid=1000 C_DRIVE ~/Documents/cdrive/

Additional resources regarding shared folders can be referenced on the Ubuntu wiki.

I was curious what the options, “-o uid=1000, gid=1000″ meant. Turns out we’re setting the owners of this folder to the user with an id of 1000 and the group with an id of 1000. If you’d like to discover the uid and gid of a user, run the following two commands.

id -u <username>

id -g <groupname>

You can learn more about userid and groupid by having a look at this help page at Indiana University.

 

 

 

Apple Printer Resource Path

When creating a payload in Apple Profile Manager, you need to not only specify the IP address, but also a resource path.

The best way to do so to issue ippfind from a box running MacOS.

Practical Packet Analysis Notes 2

This 2nd post continues my notes from reading Practical Packet Analysis.

Unreachable port / Unreachable host

A loss of network connectivity can often be seen in Wireshark. For example, a Windows host will attempt 5 re-transmissions. While Wireshark does label these TCP Retransmission, you can also identify this process as each packet has the same Sequence value.

Often the the reply to an echo will source from a switch or router, and often you’ll observe an ARP request for the destination host to reply with a MAC address. This reply can be ICMP type 0 or 3. Code 1 in the ICMP packet = host unreachable. Code2 = port unreachable.

Fragmentation

When layer 4 hands segments to layer 3 that are greater than 1500 bytes, the limit that can be carried in a frame across layer 2, the IP packets will be fragmented to fit.

Looking at the IP layer details of such a packet you can see Flags set to 0x01, meaning more fragments follow. The offset of the first packet in this data stream will be 0. The following fragment will show an offset ~1480. Fragmented packets will follow until the last arrives with a flag of 0x00 and that means no more fragments will follow.

 

 

 

Practical Packet Analysis Notes 1

Hubbing out involves placing a hub or something like a Throwing Star lan tap between a host and switch. Per the books, in most situations duplex of target is cut to half.

View->Coloring Rules is a handy way to add your own rules to make traffic stand out.

Mark a packet so it stands out with white text & black backdrop. CTRL+M and you can advance or reverse with ctrl-shift-n and ctrl-shift-b.

You can merge pcaps. Tie together captures from multiple devices to get a better view. File-> Merge

Create capture and display filters. Wireshark uses comparison and logical operators.

 Comparison Operators
==     equal to
!=     not equal to
>     greater than
<     less than
>=     greater than or equal to
<=     less than or equal to

Logical Operators
and     both conditions must be true
or        either one of the conditions must be true
xor     one and only one condition must be true
not     neither one of the conditions is true
Sample Filter Expressions
host www.biokode.net     display all traffic from host www.example.com

!ip     display all non IP traffic

ip.dst==192.168.1.1     Display all traffic with a destination of 192.168.1.1

You can manage and apply existing filters, or create a new filter from a current expression from within the filter construction dialog by clicking on the small filter icon to the left of the filter expression icon within the filter toolbar!

Name Resolution

You can enable name resolution by opening the Capture Options dialog. Capture–> Options and then select Options tab.

  1. MAC Name Resolution – Uses ARP to convert layer 2 MAC address into layer 3 IP addresses;  display 00:11:22:33:44:55:66 as 192.168.1.7.
  2. Network Name Resolution – Uses DNS to resolve IP addresses to human readable DNS names; display 192.168.1.7 as SalesWorkstation001.
  3. Transport Name Resolution – Convert port number to associated transport technology; display port 80 as http.

Name resolution may not be desirable. It can add processing overhead and resolution can fail. A capture does not store name resolution data, so expect this overhead when reviewing previously saved captures.

Protocol Dissection

Wireshark relies on protocol dissectors to to convert raw data into something understood by Wireshark, but it can select the wrong protocol dissector. Most commonly this occurs when a standard protocol is using a non-standard port. For instance, HTTP traffic flowing over port 22 could be classified as SSH!

Right click on a packet and from the menu select “Decode As.” This enables you to select a different protocol dissector than that which was chosen by Wireshark.

Following TCP Streams

Right click on a packet in the Packet list pane of the main window to Follow->TCP Stream  and view related packets and their payload that is delivered to the application level in the OSI stack. You can also select a TCP packet in the TCP  tab of the Statistics->Conversations dialog.

Endpoints

Statistics->Endpoints  summarizes traffic measured in bits, bytes, and packets grouped by hosts.