@@ -1,7 +1,7 @@
|
||||
// Default sensor sketch for MySensor Micro module
|
||||
// Act as a temperature / humidity sensor by default.
|
||||
//
|
||||
// If A1 is held low while powering on, it will clear eeprom (and thereby the stored nodeid)
|
||||
// If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals
|
||||
//
|
||||
// Battery voltage is repported as child sensorId 199, as well as battery percentage
|
||||
|
||||
@@ -10,7 +10,10 @@
|
||||
#include <Wire.h>
|
||||
#include <SI7021.h>
|
||||
#include <SPI.h>
|
||||
#include <SPIFlash.h>
|
||||
#include <EEPROM.h>
|
||||
#include <sha204_lib_return_codes.h>
|
||||
#include <sha204_library.h>
|
||||
|
||||
// Define a static node address, remove if you want auto address assignment
|
||||
//#define NODE_ADDRESS 3
|
||||
@@ -21,8 +24,13 @@
|
||||
#define CHILD_ID_BATT 199
|
||||
|
||||
//Pin definitions
|
||||
#define RESET_CFG_PIN A1
|
||||
#define TEST_PIN A0
|
||||
#define LED_PIN A2
|
||||
#define ATSHA204_PIN 17 // A3
|
||||
|
||||
const int sha204Pin = ATSHA204_PIN;
|
||||
atsha204Class sha204(sha204Pin);
|
||||
|
||||
|
||||
#define MEASURE_INTERVAL 60000
|
||||
|
||||
@@ -30,6 +38,8 @@
|
||||
#define FORCE_TRANSMIT_INTERVAL 30
|
||||
|
||||
SI7021 humiditySensor;
|
||||
SPIFlash flash(8, 0x1F65);
|
||||
|
||||
MySensor gw;
|
||||
|
||||
// Sensor messages
|
||||
@@ -51,12 +61,14 @@ void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
|
||||
// First check if we should boot into clear eeprom mode
|
||||
pinMode(RESET_CFG_PIN, INPUT);
|
||||
digitalWrite(RESET_CFG_PIN, HIGH); // Enable pullup
|
||||
if (!digitalRead(RESET_CFG_PIN)) resetEEP();
|
||||
digitalWrite(RESET_CFG_PIN, LOW);
|
||||
Serial.begin(115200);
|
||||
// First check if we should boot into test mode
|
||||
|
||||
pinMode(TEST_PIN,INPUT);
|
||||
digitalWrite(TEST_PIN, HIGH); // Enable pullup
|
||||
if (!digitalRead(TEST_PIN)) testMode();
|
||||
|
||||
digitalWrite(TEST_PIN,LOW);
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
|
||||
#ifdef NODE_ADDRESS
|
||||
@@ -75,7 +87,7 @@ void setup() {
|
||||
gw.present(CHILD_ID_HUM,S_HUM);
|
||||
|
||||
gw.present(CHILD_ID_BATT, S_POWER);
|
||||
switchClock(1<<CLKPS2);
|
||||
switchClock(1<<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
|
||||
}
|
||||
|
||||
|
||||
@@ -173,21 +185,6 @@ long readVcc() {
|
||||
return result; // Vcc in millivolts
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Resets eeprom to default value (while blinking LED_PIN)
|
||||
*/
|
||||
void resetEEP()
|
||||
{
|
||||
for (int i=0;i<512;i++) {
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(100);
|
||||
EEPROM.write(i, 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
void switchClock(unsigned char clk)
|
||||
{
|
||||
cli();
|
||||
@@ -196,3 +193,99 @@ void switchClock(unsigned char clk)
|
||||
CLKPR = clk;
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
// Verify all peripherals, and signal via the LED if any problems.
|
||||
void testMode()
|
||||
{
|
||||
uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
|
||||
uint8_t ret_code;
|
||||
byte tests = 0;
|
||||
|
||||
digitalWrite(LED_PIN, HIGH); // Turn on LED.
|
||||
|
||||
Serial.println(F("Testing peripherals!"));
|
||||
Serial.print(F("-> SI7021 : "));
|
||||
delay(100);
|
||||
|
||||
if (humiditySensor.begin())
|
||||
{
|
||||
Serial.println(F("ok!"));
|
||||
tests ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("failed!"));
|
||||
}
|
||||
delay(100);
|
||||
|
||||
Serial.print(F("-> Flash : "));
|
||||
delay(100);
|
||||
if (flash.initialize())
|
||||
{
|
||||
Serial.println(F("ok!"));
|
||||
tests ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("failed!"));
|
||||
}
|
||||
delay(100);
|
||||
|
||||
Serial.println(F("-> SHA204 : "));
|
||||
ret_code = sha204.sha204c_wakeup(rx_buffer);
|
||||
if (ret_code != SHA204_SUCCESS)
|
||||
{
|
||||
Serial.print(F("Failed to wake device. Response: ")); Serial.println(ret_code, HEX);
|
||||
}
|
||||
|
||||
if (ret_code == SHA204_SUCCESS)
|
||||
{
|
||||
ret_code = sha204.getSerialNumber(rx_buffer);
|
||||
if (ret_code != SHA204_SUCCESS)
|
||||
{
|
||||
Serial.print(F("Failed to obtain device serial number. Response: ")); Serial.println(ret_code, HEX);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Device serial: "));
|
||||
for (int i=0; i<9; i++)
|
||||
{
|
||||
if (rx_buffer[i] < 0x10)
|
||||
{
|
||||
Serial.print('0'); // Because Serial.print does not 0-pad HEX
|
||||
}
|
||||
Serial.print(rx_buffer[i], HEX);
|
||||
}
|
||||
Serial.println();
|
||||
tests ++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Serial.println(F("Test finished"));
|
||||
|
||||
if (tests == 3)
|
||||
{
|
||||
Serial.println(F("Selftest ok!"));
|
||||
while (1) // Blink OK pattern!
|
||||
{
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(800);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(200);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("----> Selftest failed!"));
|
||||
while (1) // Blink FAILED pattern! Rappidly blinking..
|
||||
{
|
||||
digitalWrite(LED_PIN, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user