Programming the XinaBox CW01 (ESP8266) to display weather sensor data on a mini-OLED display

Programming the XinaBox CW01 (ESP8266) to display weather sensor data on a mini-OLED display

Here is the fifth in our blog series for Arduino IDE users - this time programming the XinaBox CW01 to display weather sensor data on the XinaBox OD01 mini-OLED display. Previous blogs have covered:

  1. Getting Started with xChip Core CW01 (ESP8266) and IP01 programmer
  2. Programming CW01 (ESP8266) to drive Red, Green and Blue LEDs
  3. Connecting CW01 (ESP8266) to WiFi
  4. Retrieving the time from a time server using ESP8266

Please see our getting started page for a full list of resources supporting Arduino IDE users. 

Contents 

  1. What you need
  2. Installations
  3. To do
  4. When things don't work as expected  
  5. What to do next  
  6. Code file 

 

    1. What you need

    • the IP01 and CW01 set up as per 'XinaBox IP01 and CW01 Setup', and the SW01 and OD01

                        

      • familiarity with the IP01, CW01, and SW01 boards
      • successful execution of the Quickie_1-1 code
      • the Quickie_2-2.ino Arduino Sketch file

       

      2. Installations

      • see IP01 and CW01 Setup, make sure that the IP01, CW01 and Arduino IDE are set up as in the Quickie 1_1 example
        • if not already added, add the xCore and Arduino_SW01 library files as per the Quickie 2_1 example
          • browse to https://github.com/xinabox/Arduino_OD01.git and select the "clone or download" drop down box

                               

            • select the "Download ZIP" button, and save the file in a folder of your choosing
            • select Sketch->Include Library->Add .ZIP Library, and select the ZIP file saved in the previous step

                               

             

            3. To do

            • set the switches on the IP01 so that the CW01 can be programmed (flashed)
              • the first version of the IP01 has 2 small switches - make sure SW1 is set to position B, and SW2 is set to the DCE position

                                                       

                                  top view                                           side view

              • the second version of the IP01 does not have switches
            • open the Quickie_2-2.ino Arduino Sketch file in the Arduino IDE
            • click the 'Verify' button or select Sketch->Verify/Compile or type CTRL+R, make sure no errors are reported, and that 'Done compiling' is reported

                                

            • save the file using File->Save or CTRL+S if you have made any modifications to the code
            • once the compile is successful, click the 'Upload' button or select Sketch->Upload or type CTRL+U, and make sure 'Done compiling' is reported

                                 

            • as soon as the Sketch starts, the red LED on the CW01 will flash at 1 second intervals
            • the altitude, humidity, pressure, and temperature will be displayed on the OD01 OLED display
                     
              • select Tools->Serial Monitor to observe the sensor parameters output by the Serial.print("...") statements

                        

              • the weather sensor values will be updated at 5 second intervals
              • gently rest a finger on the small metal component situated in the middle of the SW01 board and observe how the readings printed in the serial monitor, change

               

              4. When things don't work as expected  
              • revisit Quickie 1-1, and follow the same 'when things don't work as expected' procedure as indicated in the Quickie 1-1 example document

               

              5. What to do next  
              • read and understand the code in this example

               

              6. Code file 

              
              /*****************************************************************************
                  This Sketch is an example for the XinaBox OD01 OLED Display Board.
                  
                  It demonstrates how to display data collected from the weather sensor
                  on the XinaBox OD01 OLED display, using the XinaBox CW01 ESP8266 and 
                  SW01 modules.
                  
                  It reads the altitude, temperature, atmospheric, and humidity information 
                  from the SW01, displays this on the OLED display, and also prints it to 
                  the serial monitor console.
                  
                  You can buy these devices on our store!
                   -----> https://xinabox.cc/products/od01
              *****************************************************************************/
              
              /************************** Library Includes ********************************/
              #include 
              #include 
              #include 
              #include 
              
              /**************************** SYSTEM DEFINES *********************************/
              #define RED 12
              #define GREEN 13
              #define BLUE 5
              
              /***************************** General Purpose *******************************/
              // loop counter
              unsigned int loop_count;
              
              // SW01 instance variable
              xSW01 SW01;
              
              // OD01 instance variable
              xOD01 OD01;
              
              // altitude measurement
              float altitude;
              
              // humidity measurement
              float humidity;
              
              // barmetric pressure measurement
              float pressure;
              
              // temperature measurement
              float temperatureC;
              float temperatureF;
              
              /***************************** Sketch Code ***********************************/
              void setup() {
                
                // start the Serial Monitor
                Serial.begin(115200);
                
                // set the CW01 ports as digital (on/off) OUTPUTs, the digitalWrite(...) statement is used to set digital ports on or off
                pinMode(RED, OUTPUT);
                pinMode(GREEN, OUTPUT);
                pinMode(BLUE, OUTPUT);
              
                // 'Wire' relates to the I²C interface.
                // configure I²C, set GPIO2 == SDA and GPIO14 == SCL on the CW01 ESP8266
                Wire.pins(2,14);
              
                // start the I²C communication
                Wire.begin();
                Serial.println("Starting I²C communication");
              
                // start the OD01 OLED display
                OD01.begin();
                Serial.println("Starting the OD01");
                // clear the OLED display
                OD01.clear();
              
                // start the SW01 sensor
                SW01.begin();
                Serial.println("Starting the SW01");
              }
              
              void loop() {
              
                // read and print sensor values from the SW01
                readSW01();
              
                // send the sensor values to the serial monitor
                sensorValuesToSerialMonitor();
              
                // send the sensor values on the OLED display
                sensorValuesToOLED();
              
                // flash the red LED and wait for 5 seconds
                red_led_flash_seconds(5);
              }
              
              // read and print Altitude, Humidity, Atmospheric Pressure, 
              // and Temperature from the sensor on the XinaBox SW01 board
              void readSW01() {
              
                // clear the variables
                altitude = 0;
                humidity = 0;
                pressure = 0;
                temperatureC = 0;
                temperatureF = 0;
              
                // poll the sensor for measurement values
                SW01.poll();
              
                // read altitude - a unit of pressure is defined as 101325 Pa
                altitude = SW01.getAltitude(101325);
                 
                // read humidity
                humidity = SW01.getHumidity();
              
                // read pressure
                pressure = SW01.getPressure();
                
                // read temperature
                temperatureC = SW01.getTempC(); // get temperature in degrees Celcuis
                temperatureF = SW01.getTempF(); // get temperature in degrees Farenheit
              }
              
              // send the sensor values to the serial monitor
              void sensorValuesToSerialMonitor() {
              
                // print the altitude reading
                Serial.print("Altitude: ");
                Serial.print(altitude);
                Serial.println(" m");
              
                // print the humidity reading
                Serial.print("Humidity: ");
                Serial.print(humidity);
                Serial.println(" %");
              
                // print the pressure reading
                Serial.print("Pressure: ");
                Serial.print(pressure);
                Serial.println(" Pa"); 
              
                // print the temperature readings   
                Serial.print("Temperature: ");
                Serial.print(temperatureC);
                Serial.println(" C");
                Serial.print("Temperature: ");
                Serial.print(temperatureF);
                Serial.println(" F");
                
                // print an empty line for spacing
                Serial.println();
              }
              
              // send the sensor values on the OLED display
              void sensorValuesToOLED() {
              
                // clear the OLED display
                OD01.clear();
              
                // print the altitude reading
                OD01.print("Altitude: ");
                OD01.print(altitude);
                OD01.println(" m");
                
                // print the humidity reading
                OD01.print("Humidity: ");
                OD01.print(humidity);
                OD01.println(" %");
              
                // print the pressure reading
                OD01.print("Pressure: ");
                OD01.print(pressure);
                OD01.println(" Pa"); 
              
                // print the temperature readings   
                OD01.print("Temperature: ");
                OD01.print(temperatureC);
                OD01.println(" C");
                OD01.print("Temperature: ");
                OD01.print(temperatureF);
                OD01.print(" F");
              }
              
              // wait n seconds while flashing red led once per second
              void red_led_flash_seconds(int seconds) {
              
                // repeat seconds times
                while (seconds > 0) {
                  seconds--;
                  red_led_flash(); // flashes once in 1 second
                }  
              }
              
              // flash the red LED, 5ms on and 995ms off , thus also acts as a 1 second delay 
              void red_led_flash() {
                
                // turn the RED LED on by writing a logic HIGH to the port
                digitalWrite(RED, HIGH);
                delay(5);
                
                // turn the RED LED off by writing a logic LOW to the port
                digitalWrite(RED, LOW);
                delay(995);
              }
              
              

               

              Previous article Getting Started with XinaBox for Raspberry Pi Users
              Next article Hands On 1: Assembling xChips

              Leave a comment

              Comments must be approved before appearing

              * Required fields