Skip to content

Commit 4a06a05

Browse files
committed
Adding PiCowbell Adalogger Examples
Adding CP and Arduino examples for PiCowbell Adalogger product guide
1 parent fa18a19 commit 4a06a05

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed

PiCowbell_Adalogger_Examples/Arduino_PiCowbell_Adalogger_Example/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
const int _MISO = 16;
2+
const int _MOSI = 19;
3+
const int _CS = 17;
4+
const int _SCK = 18;
5+
6+
#include <SPI.h>
7+
#include <SD.h>
8+
#include <Wire.h>
9+
#include "Adafruit_MCP9808.h"
10+
#include "RTClib.h"
11+
12+
RTC_PCF8523 rtc;
13+
14+
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
15+
16+
// Create the MCP9808 temperature sensor object
17+
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();
18+
19+
File logfile;
20+
21+
// blink out an error code
22+
void error(uint8_t errno) {
23+
while(1) {
24+
uint8_t i;
25+
for (i=0; i<errno; i++) {
26+
digitalWrite(LED_BUILTIN, HIGH);
27+
delay(100);
28+
digitalWrite(LED_BUILTIN, LOW);
29+
delay(100);
30+
}
31+
for (i=errno; i<10; i++) {
32+
delay(200);
33+
}
34+
}
35+
}
36+
37+
void setup() {
38+
39+
Serial.begin(115200);
40+
while (!Serial);
41+
Serial.println("\r\nPiCowbell Adalogger Test");
42+
43+
// Ensure the SPI pinout the SD card is connected to is configured properly
44+
SPI.setRX(_MISO);
45+
SPI.setTX(_MOSI);
46+
SPI.setSCK(_SCK);
47+
48+
pinMode(LED_BUILTIN, OUTPUT);
49+
50+
if (!tempsensor.begin(0x18)) {
51+
Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
52+
while (1);
53+
}
54+
Serial.println("Found MCP9808!");
55+
56+
tempsensor.setResolution(3);
57+
58+
if (! rtc.begin()) {
59+
Serial.println("Couldn't find RTC");
60+
Serial.flush();
61+
while (1) delay(10);
62+
}
63+
64+
if (! rtc.initialized() || rtc.lostPower()) {
65+
Serial.println("RTC is NOT initialized, let's set the time!");
66+
// When time needs to be set on a new device, or after a power loss, the
67+
// following line sets the RTC to the date & time this sketch was compiled
68+
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
69+
// This line sets the RTC with an explicit date & time, for example to set
70+
// January 21, 2014 at 3am you would call:
71+
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
72+
//
73+
// Note: allow 2 seconds after inserting battery or applying external power
74+
// without battery before calling adjust(). This gives the PCF8523's
75+
// crystal oscillator time to stabilize. If you call adjust() very quickly
76+
// after the RTC is powered, lostPower() may still return true.
77+
}
78+
79+
// When time needs to be re-set on a previously configured device, the
80+
// following line sets the RTC to the date & time this sketch was compiled
81+
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
82+
// This line sets the RTC with an explicit date & time, for example to set
83+
// January 21, 2014 at 3am you would call:
84+
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
85+
86+
// When the RTC was stopped and stays connected to the battery, it has
87+
// to be restarted by clearing the STOP bit. Let's do this to ensure
88+
// the RTC is running.
89+
rtc.start();
90+
91+
float drift = 43; // seconds plus or minus over oservation period - set to 0 to cancel previous calibration.
92+
float period_sec = (7 * 86400); // total obsevation period in seconds (86400 = seconds in 1 day: 7 days = (7 * 86400) seconds )
93+
float deviation_ppm = (drift / period_sec * 1000000); // deviation in parts per million (μs)
94+
float drift_unit = 4.34; // use with offset mode PCF8523_TwoHours
95+
// float drift_unit = 4.069; //For corrections every min the drift_unit is 4.069 ppm (use with offset mode PCF8523_OneMinute)
96+
int offset = round(deviation_ppm / drift_unit);
97+
// rtc.calibrate(PCF8523_TwoHours, offset); // Un-comment to perform calibration once drift (seconds) and observation period (seconds) are correct
98+
// rtc.calibrate(PCF8523_TwoHours, 0); // Un-comment to cancel previous calibration
99+
100+
Serial.print("Offset is "); Serial.println(offset); // Print to control offset
101+
102+
// see if the card is present and can be initialized:
103+
if (!SD.begin(_CS)) {
104+
Serial.println("initialization failed!");
105+
return;
106+
}
107+
Serial.println("initialization done.");
108+
}
109+
110+
void loop() {
111+
tempsensor.wake();
112+
DateTime now = rtc.now();
113+
float c = tempsensor.readTempC();
114+
float f = tempsensor.readTempF();
115+
Serial.println("Writing to SD card");
116+
digitalWrite(LED_BUILTIN, HIGH);
117+
118+
// make a string for assembling the data to log:
119+
String dataString = "";
120+
121+
dataString += "The current temp is: ";
122+
dataString += String(c);
123+
dataString += "C, ";
124+
dataString += String(f);
125+
dataString += "F, at ";
126+
dataString += String(now.year(), DEC);
127+
dataString += String('/');
128+
dataString += String(now.month(), DEC);
129+
dataString += String('/');
130+
dataString += String(now.day(), DEC);
131+
dataString += String(" (");
132+
dataString += String(daysOfTheWeek[now.dayOfTheWeek()]);
133+
dataString += String(") ");
134+
dataString += String(now.hour(), DEC);
135+
dataString += String(':');
136+
dataString += String(now.minute(), DEC);
137+
dataString += String(':');
138+
dataString += String(now.second(), DEC);
139+
140+
141+
// open the file. note that only one file can be open at a time,
142+
// so you have to close this one before opening another.
143+
File dataFile = SD.open("datalog.txt", FILE_WRITE);
144+
145+
// if the file is available, write to it:
146+
if (dataFile) {
147+
dataFile.println(dataString);
148+
dataFile.close();
149+
// print to the serial port too:
150+
Serial.println(dataString);
151+
}
152+
// if the file isn't open, pop up an error:
153+
else {
154+
Serial.println("error opening datalog.txt");
155+
}
156+
digitalWrite(LED_BUILTIN, LOW);
157+
158+
delay(5000);
159+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""CircuitPython PiCowbell Adalogger Example"""
5+
import time
6+
import board
7+
import sdcardio
8+
import busio
9+
import storage
10+
import adafruit_mcp9808
11+
import adafruit_pcf8523
12+
13+
# setup for Pico I2C
14+
i2c = busio.I2C(board.GP5, board.GP4)
15+
# setup for mcp9808 temp monitor
16+
mcp9808 = adafruit_mcp9808.MCP9808(i2c)
17+
# setup for RTC
18+
rtc = adafruit_pcf8523.PCF8523(i2c)
19+
20+
# list of days to print to the text file on boot
21+
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
22+
23+
# SPI SD_CS pin
24+
SD_CS = board.GP17
25+
26+
# SPI setup for SD card
27+
spi = busio.SPI(board.GP18, board.GP19, board.GP16)
28+
sdcard = sdcardio.SDCard(spi, SD_CS)
29+
vfs = storage.VfsFat(sdcard)
30+
try:
31+
storage.mount(vfs, "/sd")
32+
print("sd card mounted")
33+
except ValueError:
34+
print("no SD card")
35+
36+
# to update the RTC, change set_clock to True
37+
# otherwise RTC will remain set
38+
# it should only be needed after the initial set
39+
# if you've removed the coincell battery
40+
set_clock = False
41+
42+
if set_clock:
43+
# year, mon, date, hour, min, sec, wday, yday, isdst
44+
t = time.struct_time((2023, 3, 6, 00, 00, 00, 0, -1, -1))
45+
46+
print("Setting time to:", t)
47+
rtc.datetime = t
48+
print()
49+
50+
# variable to hold RTC datetime
51+
t = rtc.datetime
52+
53+
time.sleep(1)
54+
55+
def get_temp(sensor):
56+
temperature_celsius = sensor
57+
temperature_fahrenheit = temperature_celsius * 9 / 5 + 32
58+
return temperature_fahrenheit
59+
60+
# initial write to the SD card on startup
61+
try:
62+
with open("/sd/temp.txt", "a") as f:
63+
# writes the date
64+
f.write('The date is {} {}/{}/{}\n'.format(days[t.tm_wday], t.tm_mon, t.tm_mday, t.tm_year))
65+
# writes the start time
66+
f.write('Start time: {}:{}:{}\n'.format(t.tm_hour, t.tm_min, t.tm_sec))
67+
# headers for data, comma-delimited
68+
f.write('Temp,Time\n')
69+
# debug statement for REPL
70+
print("initial write to SD card complete, starting to log")
71+
except ValueError:
72+
print("initial write to SD card failed - check card")
73+
74+
while True:
75+
try:
76+
# variable for RTC datetime
77+
t = rtc.datetime
78+
# append SD card text file
79+
with open("/sd/temp.txt", "a") as f:
80+
# read temp data from mcp9808
81+
temp = get_temp(mcp9808.temperature)
82+
# write temp data followed by the time, comma-delimited
83+
f.write('{},{}:{}:{}\n'.format(temp, t.tm_hour, t.tm_min, t.tm_sec))
84+
print("data written to sd card")
85+
# repeat every 30 seconds
86+
time.sleep(30)
87+
except ValueError:
88+
print("data error - cannot write to SD card")
89+
time.sleep(10)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2017 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import busio
7+
import adafruit_pcf8523
8+
9+
I2C = busio.I2C(board.GP5, board.GP4)
10+
rtc = adafruit_pcf8523.PCF8523(I2C)
11+
12+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
13+
14+
set_time = False
15+
16+
if set_time: # change to True if you want to write the time!
17+
# year, mon, date, hour, min, sec, wday, yday, isdst
18+
t = time.struct_time((2023, 3, 6, 10, 55, 00, 1, -1, -1))
19+
# you must set year, mon, date, hour, min, sec and weekday
20+
# yearday is not supported, isdst can be set but we don't do anything with it at this time
21+
22+
print("Setting time to:", t) # uncomment for debugging
23+
rtc.datetime = t
24+
print()
25+
26+
while True:
27+
t = rtc.datetime
28+
#print(t) # uncomment for debugging
29+
30+
print("The date is %s %d/%d/%d" % (days[t.tm_wday], t.tm_mon, t.tm_mday, t.tm_year))
31+
print("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
32+
33+
time.sleep(1) # wait a second

0 commit comments

Comments
 (0)