Since I got the Expansion Board Base for XIAO board, I wanted to use the monitor to interface with the SHEM. The monitor should display basic things like the date, the weather and how much energy I am consuming each day. I like the idea of accessing the weather since I always forget when it is too late to check the weather so I don't know if it is going to rain or the temperatures (in Germany, the weather can be really unpredictable). It would be awesome to just go to the bathroom and automatically the monitor turns on and I can check the weather while I am brushing my teeth.
The monitor is really small so there is a limit of how much information it can contain at one time. Likely, there is button integrated on the expansion board so I can just press the button to switch screen. An SD card slot is included as well, and I can use to store data like how much energy consumption was used each day and I can use this data to plot it on the monitor.
I followed these instructions to set up the monitor:
Expansion Board Base for XIAO | Seeed Studio Wiki
The code was really easy to learn since it reminded me of the LCD Arduino monitor (luckily there weren't any wire to set up for this monitor if you know what I mean). The button was easy to set up as well. Right now, I have only two pages (a welcome page and one to access the SD card) so the code to switch between pages is really basic but if I want to include more pages I need to rewrite it. By ready the library to write text on the monitor, I notice there are examples also for weather like this one:
U8g2_Arduino/examples/full_buffer/Weather/Weather.ino at master · olikraus/U8g2_Arduino (github.com)
I will try to expand my code to include some images on the display to show the weather (so if it is sunny show a picture of a sun and so on). I need to find something also to plot a scatterplot or histogram to show the power consumption of the SHME. I would say just to plot the hours each day of the week (so Monday the SHME was on for 2 hours, Tuesday for 2.5 hours, Wednesday for 1 hours, and so on).
Right now, I have tried to write code on the SD card. I have used the example from the first link. Although the tutorial mentions for XIAO SAMD21 board I didn't have to download any external libraries, still the FS.h library wasn't included. There wasn't any link to where download the library so I just commented the library and everything worked fine for the example. I was able to write a file and read it.
Problems came when I included the example into my code. First of all, the most annoying problem I was getting was that Arduino IDE couldn't find the port although it showed it was connected.
The only guaranteed way to fix this issue was to restart my computer. I am getting this error too often. Second problem, when I managed to upload the code, reading the file was corrupted. What I was reading wasn't the same. For now I am thinking of 3 possible issues:
- Some libraries are incompatible between each other
- The FS.h library was important and I need to find a way to import it
- This line might be the issue
u8x8.setFont(u8x8_font_chroma48medium8_r); // choose a suitable font
On the tutorial there were two examples to read and write on the SD card. I will try the other one and import the library (for the second example they provided the link to import the library.
So these are the things to do to improve the interface:
- Find a solution for writing and reading on the SD card
- Find a script on the examples to plot data
- Test the weather icons and include them on the welcome page
I was working for a long time to get access to a weather API using the Nicla Vision Board. I found this tutorial that explains how to use the openweather API, read JSON file and do a HTTP GET request. The first two parts were a piece of cake but the third one not. The tutorial uses an ESP32 and different boards. The library HTTPClient.h is not included with the Nicla Vision Board and I didn't find any library compatible with the board to do an HTTP GET request. I can use a board at home that has an ESP32 but this would increase the complexity of the entire system (and it is already complex as it is). Maybe if somebody knows how to do a HTTP GET request can help me. I am new on this field.
ESP32 HTTP GET with Arduino IDE (OpenWeatherMap.org and ThingSpeak) | Random Nerd Tutorials
I am attaching below my code for the interface so far.
#include <Arduino.h> #include <U8x8lib.h> #include <Wire.h> #include <PCF8563.h> PCF8563 pcf; #include <SPI.h> #include <SD.h> //#include "FS.h" U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/ PIN_WIRE_SCL, /* data=*/ PIN_WIRE_SDA, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display const int buttonPin = 1; // the number of the pushbutton pin int buttonState = 0; // variable for reading the pushbutton status int page = 1; File myFile; void setup() { // Open serial communications and wait for port to open: Serial.begin(115200); while(!Serial); // Execute after turning on the serial monitor delay(500); Serial.print("Initializing SD card..."); pinMode(D2, OUTPUT); // Modify the pins here to fit the CS pins of the SD card you are using. if (!SD.begin(D2)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. myFile = SD.open("/test5.txt", FILE_WRITE); // The path to read and write files needs to start with "/" // if the file opened okay, write to it: if (myFile) { Serial.print("Writing to test.txt..."); myFile.println("1"); myFile.println("2"); myFile.println("3"); myFile.println("4"); // close the file: myFile.close(); Serial.println("done."); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT_PULLUP); u8x8.begin(); u8x8.setFlipMode(1); // set number from 1 to 3, the screen word will rotary 180 Wire.begin(); pcf.init();//initialize the clock pcf.stopClock();//stop the clock pcf.setYear(23);//set year pcf.setMonth(10);//set month pcf.setDay(01);//set dat pcf.setHour(10);//set hour pcf.setMinut(33);//set minut pcf.setSecond(0);//set second pcf.startClock();//start the clock } void loop() { buttonState = digitalRead(buttonPin); Time nowTime = pcf.getTime();//get current time u8x8.setFont(u8x8_font_chroma48medium8_r); // choose a suitable font // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == LOW) { page = page * -1; u8x8.setCursor(0, 0); u8x8.print(" "); u8x8.setCursor(0, 1); u8x8.print(" "); u8x8.setCursor(0, 2); u8x8.print(" "); u8x8.setCursor(0, 3); u8x8.print(" "); u8x8.setCursor(0, 4); u8x8.print(" "); u8x8.setCursor(0, 5); u8x8.print(" "); u8x8.setCursor(0, 6); u8x8.print(" "); } if (page > 0) { u8x8.setCursor(0, 0); u8x8.print("Hello World!"); u8x8.setCursor(0, 1); u8x8.print(nowTime.day); u8x8.print("/"); u8x8.print(nowTime.month); u8x8.print("/"); u8x8.print("20"); u8x8.print(nowTime.year); u8x8.setCursor(0, 2); u8x8.print(nowTime.hour); u8x8.print(":"); u8x8.print(nowTime.minute); u8x8.print(":"); u8x8.println(nowTime.second); } else if (page< 0) { // re-open the file for reading: myFile = SD.open("/test5.txt"); // The path to read and write files needs to start with "/" if (myFile) { Serial.println("test5.txt:"); // read from the file until there's nothing else in it: int count = 0; while (myFile.available()) { Serial.write(myFile.read()); u8x8.setCursor(0, count); u8x8.print(myFile.read()); count++; Serial.println(count); delay(500); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } } delay(250); }