First Commit
This commit is contained in:
commit
31565dcde2
14 changed files with 970 additions and 0 deletions
332
src/arduino-sensors-serial.cpp
Normal file
332
src/arduino-sensors-serial.cpp
Normal file
|
|
@ -0,0 +1,332 @@
|
|||
// /bin/avrdude -C//etc/avrdude.conf -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:/tmp/arduino_build_80915/arduino-sensors-serial.ino.hex:i
|
||||
// /bin/avrdude -C//etc/avrdude.conf -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:/tmp/arduino_build_80915/arduino-sensors-serial.ino.hex:i
|
||||
|
||||
#include <SimpleDHT.h>
|
||||
#include <RCSwitch.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <Wire.h>
|
||||
#include <BH1750.h>
|
||||
|
||||
#include <bitBangedSPI.h>
|
||||
#include <MAX7219.h>
|
||||
|
||||
#include <NonBlockingRtttl.h>
|
||||
|
||||
BH1750 lightMeter; // I2C SDA: A4, SCL: A5
|
||||
|
||||
RCSwitch mySwitch = RCSwitch();
|
||||
|
||||
const byte chips = 1;
|
||||
// 1 chip, CS: 4
|
||||
MAX7219 display (chips, 4);
|
||||
|
||||
const byte PIN_PIR = 2;
|
||||
const byte PIN_BUZZER = 10;
|
||||
const byte PIN_433 = 7;
|
||||
const byte PIN_DHT22 = 8;
|
||||
/*const byte PIN_LED_R = 5;
|
||||
const byte PIN_LED_G = 6;
|
||||
const byte PIN_LED_B = 9;*/
|
||||
//PWM: 3, 5, 6, 9, 10, and 11
|
||||
//SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK).
|
||||
|
||||
|
||||
const unsigned long chacon_OFF = 1381716;
|
||||
const unsigned long chacon_ON = 1381717;
|
||||
const unsigned long chacondim_OFF = 1394004;
|
||||
const unsigned long chacondim_ON = 1394005;
|
||||
|
||||
char mode = 'z';
|
||||
String val;
|
||||
|
||||
SimpleDHT22 dht22;
|
||||
|
||||
unsigned long timemillis=millis();
|
||||
const unsigned long timertemp = 30000;
|
||||
unsigned long timemillislux=millis();
|
||||
const unsigned long timerlux = 1000;
|
||||
float oldtemperature = 0;
|
||||
int oldhumidity = 0;
|
||||
int oldpirstate = 0;
|
||||
|
||||
uint16_t oldlux = 0;
|
||||
|
||||
/*unsigned long cmmillis = 0;
|
||||
short cmindex = -1;
|
||||
const unsigned int cmmusic[] = { 230, 560, 450, 140, 670, 300, 520, 0, 0, 0, 0, 0, 0, 0 };
|
||||
*/
|
||||
|
||||
//const char * tetris = "tetris:d=4,o=5,b=160:e6,8b,8c6,8d6,16e6,16d6,8c6,8b,a,8a,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,2a,8p,d6,8f6,a6,8g6,8f6,e6,8e6,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,a";
|
||||
//const char * arkanoid = "Arkanoid:d=4,o=5,b=140:8g6,16p,16g.6,2a#6,32p,8a6,8g6,8f6,8a6,2g6";
|
||||
//const char * mario = "mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6";
|
||||
|
||||
//t:d=4,o=5,b=100:c,p,c,p,c,p,c
|
||||
|
||||
String ptext;
|
||||
boolean ptextstate = false;
|
||||
short ptextindex = -1;
|
||||
short ptextdirection = 1;
|
||||
unsigned long ptextmillis = 0;
|
||||
|
||||
/** Affiche une couleur */
|
||||
/*void displayColor(byte r, byte g, byte b) {
|
||||
analogWrite(PIN_LED_R, ~r);
|
||||
analogWrite(PIN_LED_G, ~g);
|
||||
analogWrite(PIN_LED_B, ~b);
|
||||
}*/
|
||||
|
||||
void pirchange() {
|
||||
int pirstate = digitalRead(2);
|
||||
if(pirstate!=oldpirstate)
|
||||
{
|
||||
oldpirstate=pirstate;
|
||||
if(pirstate == HIGH) {
|
||||
Serial.println("p 1");
|
||||
} else {
|
||||
Serial.println("p 0");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// Emetteur connecté au pin #10 de l'Arduino
|
||||
mySwitch.enableTransmit(PIN_433);
|
||||
mySwitch.setRepeatTransmit(8);
|
||||
//Serial.println("init ");
|
||||
|
||||
/*pinMode(PIN_LED_R, OUTPUT);
|
||||
pinMode(PIN_LED_G, OUTPUT);
|
||||
pinMode(PIN_LED_B, OUTPUT);
|
||||
displayColor(0, 0, 0);*/
|
||||
|
||||
// Initialize the I2C bus (BH1750 library doesn't do this automatically)
|
||||
Wire.begin();
|
||||
// On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
|
||||
|
||||
lightMeter.begin();
|
||||
|
||||
pinMode(PIN_PIR, INPUT_PULLUP);
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_PIR), pirchange, CHANGE);
|
||||
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
|
||||
display.begin ();
|
||||
display.setIntensity (10);
|
||||
display.sendString ("HELLO NG");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
/* Serial.print("allume ");
|
||||
mySwitch.send(chacon_ON, 24);
|
||||
delay(3000);
|
||||
Serial.println("eteint ");
|
||||
mySwitch.send(chacon_OFF, 24);
|
||||
delay(3000); */
|
||||
//Serial.print("allume ");
|
||||
//mySwitch.send(chacondim_ON, 24);
|
||||
//delay(3000);
|
||||
//Serial.println("eteint ");
|
||||
//mySwitch.send(chacondim_OFF, 24);
|
||||
//delay(3000);
|
||||
char schar;
|
||||
|
||||
|
||||
|
||||
if(Serial.available() > 0)
|
||||
{
|
||||
schar=Serial.read();
|
||||
if(schar != '\r' && schar != '\n')
|
||||
{
|
||||
if(mode == 'z') {
|
||||
mode=schar;
|
||||
} else {
|
||||
//val=schar;
|
||||
val.concat(schar);
|
||||
//Serial.print(mode);
|
||||
//Serial.println(val);
|
||||
//mode='z'; }
|
||||
}
|
||||
} else {
|
||||
|
||||
// Serial.print(mode);
|
||||
// Serial.println(val.toInt());
|
||||
long valint;
|
||||
if(mode=='d')
|
||||
{
|
||||
valint=val.toInt();
|
||||
if(valint==0) {
|
||||
//Serial.println("Dimmer OFF");
|
||||
mySwitch.send(chacondim_OFF, 24);
|
||||
} else if(valint==1) {
|
||||
//Serial.println("Dimmer ON");
|
||||
mySwitch.send(chacondim_ON, 24);
|
||||
}
|
||||
} else if(mode=='s') {
|
||||
valint=val.toInt();
|
||||
if(valint==0) {
|
||||
//Serial.println("Switch OFF");
|
||||
mySwitch.send(chacon_OFF, 24);
|
||||
} else if(valint==1) {
|
||||
//Serial.println("Switch ON");
|
||||
mySwitch.send(chacon_ON, 24);
|
||||
}
|
||||
/* } else if(mode=='l') {
|
||||
byte r,g,b;
|
||||
valint = strtol(val.c_str(), NULL, 16);
|
||||
r = ((valint >> 16) & 0xFF);
|
||||
g = ((valint >> 8) & 0xFF);
|
||||
b = ((valint) & 0xFF);
|
||||
displayColor(r,g,b); */
|
||||
} else if(mode=='t' and ptextstate==false) {
|
||||
display.sendString(val.c_str());
|
||||
} else if(mode=='i') {
|
||||
display.setIntensity(val.toInt());
|
||||
} else if(mode=='m') {
|
||||
valint=val.toInt();
|
||||
if(valint==0) {
|
||||
display.sleep();
|
||||
} else if(valint==1) {
|
||||
display.wakeUp();
|
||||
}
|
||||
|
||||
} else if(mode=='b') {
|
||||
valint=val.toInt();
|
||||
if(valint==1) {
|
||||
tone(PIN_BUZZER, 450, 250);
|
||||
//toneAC(450, 10, 250);
|
||||
delay(300);
|
||||
tone(PIN_BUZZER, 450, 250);
|
||||
//toneAC(450, 10, 250);
|
||||
} else if(valint==2) {
|
||||
tone(PIN_BUZZER, 450, 250);
|
||||
//toneAC(450, 10, 250);
|
||||
delay(300);
|
||||
tone(PIN_BUZZER, 550, 250);
|
||||
//toneAC(550, 10, 250);
|
||||
} else if(valint==3) {
|
||||
tone(PIN_BUZZER, 450, 250);
|
||||
//toneAC(450, 10, 250);
|
||||
delay(300);
|
||||
tone(PIN_BUZZER, 350, 250);
|
||||
//toneAC(350, 10, 250);
|
||||
/* } else if(valint==4) {
|
||||
cmindex = 0;
|
||||
} else if(valint==5) {
|
||||
noTone(PIN_BUZZER);
|
||||
cmindex = -1;*/
|
||||
}
|
||||
} else if(mode=='c') {
|
||||
rtttl::begin(PIN_BUZZER, val.c_str());
|
||||
|
||||
} else if(mode=='f') {
|
||||
valint=val.toInt();
|
||||
tone(PIN_BUZZER, valint, 250);
|
||||
//toneAC(valint, 10, 250);
|
||||
} else if(mode=='v') {
|
||||
valint=val.toInt();
|
||||
//char vsend[3];
|
||||
unsigned long vsendl = 10759680;
|
||||
//vsend[0]=0xa4;
|
||||
//vsend[1]=0x2e;
|
||||
//vsend[2] = 0x00;
|
||||
vsendl += valint;
|
||||
mySwitch.send(vsendl, 24);
|
||||
} else if(mode=='p')
|
||||
{
|
||||
if(val=="")
|
||||
{
|
||||
ptextstate=false;
|
||||
} else {
|
||||
ptextstate=true;
|
||||
if(val.length()<=8)
|
||||
{
|
||||
ptextindex=-1;
|
||||
display.sendString(val.c_str());
|
||||
} else {
|
||||
ptextindex=0;
|
||||
ptextdirection=1;
|
||||
ptextmillis=0;
|
||||
ptext=val;
|
||||
}
|
||||
}
|
||||
}
|
||||
mode='z';
|
||||
val="";
|
||||
}
|
||||
}
|
||||
|
||||
if(millis()>timemillis+timertemp)
|
||||
{
|
||||
timemillis=millis();
|
||||
float temperature = 0;
|
||||
float humidity = 0;
|
||||
int humidity2 = 0;
|
||||
int err = SimpleDHTErrSuccess;
|
||||
if ((err = dht22.read2(PIN_DHT22, &temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
|
||||
humidity2=round(humidity);
|
||||
/* Serial.print("t ");
|
||||
Serial.print(timemillis);
|
||||
Serial.print(' ');
|
||||
Serial.print(temperature);
|
||||
Serial.print(' ');
|
||||
Serial.print(humidity);
|
||||
Serial.print(' ');
|
||||
Serial.print(humidity2);
|
||||
Serial.print(' ');
|
||||
Serial.println(millis());*/
|
||||
if(oldtemperature!=temperature) { Serial.print("t "); Serial.println(temperature); }
|
||||
if(oldhumidity!=humidity2) { Serial.print("h "); Serial.println(humidity2); }
|
||||
oldtemperature=temperature;
|
||||
oldhumidity=humidity2;
|
||||
//} else {
|
||||
// Serial.print("Read DHT22 failed, err="); Serial.println(err);
|
||||
}
|
||||
}
|
||||
if(millis()>timemillislux+timerlux)
|
||||
{
|
||||
timemillislux=millis();
|
||||
uint16_t lux = lightMeter.readLightLevel();
|
||||
if(lux!=oldlux)
|
||||
{
|
||||
oldlux=lux;
|
||||
Serial.print("x "); Serial.println(lux);
|
||||
}
|
||||
}
|
||||
/*if(cmindex != -1)
|
||||
{
|
||||
if(millis()-cmmillis > 200)
|
||||
{
|
||||
cmmillis = millis();
|
||||
noTone(PIN_BUZZER);
|
||||
if(cmmusic[cmindex] != 0)
|
||||
{
|
||||
tone(PIN_BUZZER, cmmusic[cmindex]);
|
||||
}
|
||||
cmindex++;
|
||||
if(cmindex == sizeof(cmmusic)/sizeof(cmmusic[0])) { cmindex = 0; }
|
||||
}
|
||||
}*/
|
||||
|
||||
while( !rtttl::done() )
|
||||
{
|
||||
rtttl::play();
|
||||
}
|
||||
|
||||
if(ptextstate==true and ptextindex != -1 and millis()-ptextmillis > 1500)
|
||||
{
|
||||
ptextmillis=millis();
|
||||
display.sendString(ptext.substring(ptextindex, ptextindex+8).c_str());
|
||||
if((unsigned)(ptextindex+ptextdirection+8)>ptext.length())
|
||||
{
|
||||
ptextdirection=-1;
|
||||
ptextindex=ptextindex-1;
|
||||
} else if(ptextindex+ptextdirection == -1) {
|
||||
ptextdirection=1;
|
||||
ptextindex=ptextindex+1;
|
||||
} else {
|
||||
ptextindex=ptextindex+ptextdirection;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue