Skip to content

Basic v1 Bring Up Test Code

Dave Williams | DitroniX | G8PUO edited this page Jan 12, 2023 · 1 revision

This CWX-1 Basic Bring Up Test Code Wiki page, provides basic information to connect your computer to the CWX-1 and then flash some sample code which will read the sensors and update to ThingSpeak.

Overview

The CWX 1 –  STEM SDK IoT (Compact Weather Station), contains a compact BME280 sensor which provides three readings, namely.

  • Barometric Pressure
  • Temperature
  • Humidity
In addition, a SFH2440 Ambient Light Sensor is fitted to measure ambient light level.
  • Light Level
These two sensors interface to an ESP-12S Series WiFi module, based on ESP8266 with built-in Flash.  The BME280 is connected to the I2C bus and the SFH2440 connects to the ESP's built-in ADC.

 

Programming / Flashing UART Control

In order to ease programming, a simple two transistor circuit is added to the board which when connected to a USB to UART module, allows the host computer to control the DTR = Reset, and RTS = Boot Mode.  This makes recompiling and flashing so much simpler as you have no button pressing.

Our USB to UART module has both of these connections.

 

Code Summary

The example bring up code will:

  • Setup the ESP prerequisites
  • Initialise I/O
  • Loop (once every minute)
    • Read Temperature
    • Read Humidity
    • Read Pressure
    • Read Light
    • Print out reading
Note:
  • This bring-up example code does not enable power saving or sleep.
 

 

Arduino IDE Setup

Once your ThingSpeak account and channel is configured, you can then load the example code into the Arduio IDE, Compile and Flash to the ESP.   The code has been annotated to be helpful in working through and should hopefully make sense.

Should you not have Arduino IDE installed, it can be easily downloaded for Windows, Ubuntu, Linux, Mac etc via

Before you can compile the code, you will need to add the ESP8266 board and Libraries to the Arduino IDE.  Open Arduino IDE.

  • Board Manager
    • Tools > Board > Board Manager
      • type ESP8266 in the search.  Click install the ESP8266 Community.
    • Tools > Board > ESP8266 Boards. Select Generic ESP8266 Module.
    • Tools > Manage Libraries.  This will open the Library Manager.
      • type BME280 in the search. Click install the Adafruit BME280 Library
      • type Uptime in the search. Click install the Uptime Library by Yiannis Bourkelis
Your Arduino IDE should now be setup to load the example code and successfully flash to the CWX.

The below example is the CWX connected to the USB to UART. (Ignore the switch, which was used in development).  Ensure the header is plugged into the CWX the correct way round!

Note: DO NOT connect the battery during programming - as the battery is not isolated from the programming header 3V3.  Your CWX will totally flash and run from the UART, with no external power.

Example Code

This code is an example. Please feel free to download this and update as needed.  The code includes some preamble to assist learning and diagnosis.

You will need to add sleep modes to this code in order to conserve power.

Remember, the Arduino IDE has a Serial Debug Monitor built in.  If you select this, it will display the printed output in a separate window.   See Tools > Serial Monitor.  (CTRL+SHIFT+M)

 

[wpdm_package id='1894']

/*
  Dave Williams, DitroniX 2019-2022 (ditronix.net)
  CWX-1 Compact Weather Station ESP-12 Series SDK
  PCA v2204-106 - Compact Weather Station - Basic Bring Up Test Code Firmware 1.220406 - 6th April 2022

The purpose of this test code is to cycle through the various main functions of the board as part of bring up testing.

This test code is OPEN SOURCE and although is is not intended for real world use, it may be freely used, or modified as needed.

Example Output:

Temperature (ºC): 22.2 Humidity (%): 49 Pressure (hPa): 988 Light: 62 System Uptime 0 days, 0 hours, 0 minutes, 14 seconds
*/

#include "Wire.h" #include <Adafruit_BME280.h> #include "uptime_formatter.h" // https://github.com/YiannisBourkelis/Uptime-Library

// ######### OBJECTS ######### Adafruit_BME280 bme; //BME280 connect to ESP8266 I2C (GPIO 4 = SDA, GPIO 5 = SCL) // BME280 0x76 | BMP280 0x76 | BMP180 0x77

// ######### VARIABLES / DEFINES / STATIC #########

// App String AppVersion = "v1.220406"; String AppBuild = "DitroniX CWX 12S SDK PCA v2204-106"; String AppName = "Compact Weather Station - Basic Bring Up Test";

// Variables char SensorResult[10]; float TemperatureC; float Humidity; int Pressure; int LightMeter;

// **************** IO ****************

// Define I2C (Expansion Port) #define I2C_SDA 4 #define I2C_SCL 5

// **************** OUTPUTS **************** #define LED_Module 2 // Define ESP Output Port LED

// **************** INPUTS **************** #define ADC A0 // Define ADC (0 DC - 1V DC)

// ######### FUNCTIONS #########

// Scan for I2C Devices void scan_i2c_devices() {
// Scan I2C Devices Serial.print("Scanning I2C\t"); byte count = 0; Serial.print("Found Devices: "); Serial.print(" Devices: "); for (byte i = 8; i < 120; i++) { Wire.beginTransmission(i); if (Wire.endTransmission() == 0) { Serial.print(" (0x"); Serial.print(i, HEX); Serial.print(")\t"); count++; delay(1); } }
Serial.print("Found "); Serial.print(count, HEX); Serial.println(" Device(s)."); }

// Initialise BME280 void InitialiseTemperatureSensor(){ if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); // while (1); } }

// ######### SETUP ######### void setup() {

//Stabalise
delay(500);   

// Initialize UART:
Serial.begin(115200, SERIAL_8N1);  //115200  
while (!Serial);
Serial.println(""); 
Serial.println(AppVersion + " Initialized");
Serial.println(AppBuild);
Serial.println(AppName);
Serial.println(""); 

// LED
pinMode(LED_Module, OUTPUT);    

// Initialize I2C 
Wire.begin(I2C_SDA, I2C_SCL);
scan_i2c_devices();    
InitialiseTemperatureSensor();

Serial.println(""); 

}

// ######### LOOP ######### void loop() {

// Get Temperature ºC and round to 1 decimal place
TemperatureC = bme.readTemperature();
dtostrf (TemperatureC, 5, 1, SensorResult);
sprintf (SensorResult, "%s", SensorResult);
Serial.print("Temperature (ºC): "); 
Serial.println(SensorResult);   
    
// Get Humity % and round to no decimal places
Humidity = round(bme.readHumidity());
dtostrf(Humidity,5,0,SensorResult);      
Serial.print("Humidity (%): ");
Serial.println(SensorResult);    

// Get Pressure and round to no decimal places
Pressure = (bme.readPressure() / 100.0F);
Pressure = round(Pressure);
Serial.print("Pressure (hPa): ");
Serial.println(Pressure);  

// Get Light Reading
LightMeter = analogRead(ADC);
Serial.print("Light: ");    
Serial.print(LightMeter ); // data output from the photoresistor (0-1024)
Serial.println("");    
   
// Timestamp
Serial.println("System Uptime " + uptime_formatter::getUptime());
Serial.println("");     
         
// Hearbeat LED (Temporary during development)
digitalWrite(LED_Module, LOW);   // turn the LED on (HIGH is the voltage level)
delay(500);                       // wait for a second
digitalWrite(LED_Module, HIGH);    // turn the LED off by making the voltage LOW

// Loop Delay
delay(1000);

}