1
Denne sektion tillader dig at se alle indlæg oprettet af dette medlem. Bemærk at du kun kan se indlæg der er oprettet i områder du i øjeblikket har adgang til.
// 1: RF24 ds18b20
// 2: RF24 dht22
#include <digitalWriteFast.h>
#include <iBoardRF24Network.h>
#include <iBoardRF24.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Socketswitch.h>
#define NODES 3
Socketswitch mySwitch(30);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 10, 10, 178);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// (cepin 3, cspin 4, mosi_pin 6, miso_pin 7, sck_pin 5, irq_pin 8)
iBoardRF24 radio(3, 8, 5, 6, 7, 2);
iBoardRF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
struct payload_t
{
unsigned long ms;
unsigned long counter;
byte device_type;
float humidity;
float temperature;
long voltage;
};
struct sensors_t
{
unsigned long ms;
unsigned long counter;
byte device_type;
float humidity;
float temperature;
long voltage;
unsigned long local_ms;
};
struct sensors_t sensors[NODES];
String readString;
void setup() {
// Initialize array which holds the data from nodes
for (int i = 0; i < NODES; i++) {
sensors[i].device_type = 0;
sensors[i].humidity = 0;
sensors[i].temperature = 0;
sensors[i].voltage = 0;
sensors[i].counter = 0;
sensors[i].ms = 0;
sensors[i].local_ms = 0;
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
mySwitch.setDevice(0x34FCFF);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 93, /*node address*/ this_node);
}
void socketSwicth(int id, int stat) {
switch (id) {
case 0:
if (stat == 1) {
mySwitch.channelOn(0);
} else {
mySwitch.channelOff(0);
}
break;
case 1:
if (stat == 1) {
mySwitch.channelOn(1);
} else {
mySwitch.channelOff(1);
}
break;
case 2:
if (stat == 1) {
mySwitch.channelOn(2);
} else {
mySwitch.channelOff(2);
}
break;
case 9:
if (stat == 1) {
mySwitch.groupOn();
} else {
mySwitch.groupOff();
}
break;
}
}
void loop() {
const unsigned long tenMinutes = 10 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - tenMinutes; // initialize such that a reading is due the first time through loop()
unsigned long now = millis();
if (now - lastSampleTime >= tenMinutes)
{
lastSampleTime += tenMinutes;
}
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
payload_t payload;
network.read(header, &payload, sizeof(payload));
sensors[header.from_node - 2].device_type = payload.device_type;
sensors[header.from_node - 2].humidity = payload.humidity;
sensors[header.from_node - 2].temperature = payload.temperature;
sensors[header.from_node - 2].voltage = payload.voltage;
sensors[header.from_node - 2].counter = payload.counter;
sensors[header.from_node - 2].ms = payload.ms;
sensors[header.from_node - 2].local_ms = millis();
}
// listen for incoming clients
readString = "";
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
// client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head><style>button { width: 400px; height: 100px; font-size:48px; }</style></head><title>Salomon Switch server (Remote: ??)</title>");
if (readString.indexOf("status=yes") > 0) {
client.println("<center><h1>Status:</h1><br><table>");
for (int i = 0; i < NODES; i++) {
client.print("<tr><td>Node:</td><td>");
client.print(i + 2);
client.print("</td><td>Device type:</td><td>");
client.print(sensors[i].device_type);
client.print("</td><td>Humidity:</td><td>");
client.print(sensors[i].humidity);
client.print("</td><td>Temperature:</td><td>");
client.print(sensors[i].temperature);
client.print("</td><td>Voltage:</td><td>");
client.print(sensors[i].voltage);
client.print("</td><td>Packet #");
client.print(sensors[i].counter);
client.print(" at ");
client.print(sensors[i].ms);
client.print("</td><td>Received:");
client.print(sensors[i].local_ms);
client.print("</td></tr>");
}
client.println("</table><br><br><a href=/?switch=yes>Back to control</a></center>");
} else {
if (readString.indexOf("switch=yes") > 0) {
client.println("<center><h1>Control:</h1><br><form method=get><input type=hidden name=switch value=yes><button name=sw type=submit value=01>SW1 ON</button> <button name=sw type=submit value=00>SW1 OFF</button><br><button name=sw type=submit value=11>SW2 ON</button> <button name=sw type=submit value=10>SW2 OFF</button><br><button name=sw type=submit value=21>SW3 ON</button> <button name=sw type=submit value=20>SW3 OFF</button><br><button name=sw type=submit value=91>SWG ON</button> <button name=sw type=submit value=90>SWG OFF</button></form><br><a href=/?status=yes>Status</a></center>");
int pos = readString.indexOf("sw=");
if (pos > 0) {
socketSwicth(readString.substring(pos + 3, pos + 4).toInt(), readString.substring(pos + 4, pos + 5).toInt());
}
client.println("<br />");
}
}
client.println("<p><center><h1>Dansk Elektronik Forum's artikel konkurrence </h1><br><a href=http://elektronik-forum.dk/index.php?topic=987&asarticle target=_new>Dansk Elektronik Forum artikel</a></center></html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "LowPower.h"
#define ONE_WIRE_BUS 7
#define TEMPERATURE_PRECISION 12
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9, 10);
// Network uses that radio
RF24Network network(radio);
// Address of our node MAX 6 Nodes
const uint16_t this_node = 3;
// Address of the other node
const uint16_t other_node = 0;
// 1: ds18b20
// 2: dht22
const byte this_type = 1;
// How often to send 'hello world to the other unit
const unsigned long interval = 2000; // 2000; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
unsigned long packets_sent;
// Temp
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress termo;
float tempC;
// Structure of our payload
struct payload_t
{
unsigned long ms;
unsigned long counter;
byte device_type;
float humidity;
float temperature;
long voltage;
};
boolean serial = false;
boolean device_found;
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1125300L / result; // Back-calculate AVcc in mV
return result;
}
void sleepTenMinutes()
{
for (int i = 0; i < 75; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
void setup(void)
{
if (serial) {
Serial.begin(57600);
Serial.println("RF24Network/NODE5_tx/");
}
pinMode(8, OUTPUT);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 93, /*node address*/ this_node);
sensors.begin();
device_found = sensors.getAddress(termo, 0);
if (device_found) sensors.setResolution(termo, TEMPERATURE_PRECISION);
if (serial) {
Serial.print("Device found: ");
Serial.println(device_found);
}
}
void loop(void)
{
// Pump the network regularly
network.update();
// If it's time to send a message, send it!
// unsigned long now = millis();
// if ( now - last_sent >= interval ) {
if (device_found) {
sensors.requestTemperatures();
tempC = sensors.getTempC(termo);
} else {
device_found = sensors.getAddress(termo, 0);
if (device_found) sensors.setResolution(termo, TEMPERATURE_PRECISION);
tempC = -123;
}
byte hum = 0;
// last_sent = now;
if (serial) Serial.print("Sending...");
payload_t payload = { millis(), packets_sent++, this_type, hum, tempC, readVcc()};
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header, &payload, sizeof(payload));
if (ok) {
if (serial) Serial.println("ok.");
} else {
if (serial) Serial.println("failed.");
digitalWrite(8, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(8, LOW); // sets the LED off
}
radio.powerDown();
sleepTenMinutes();
}
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include "LowPower.h"
#include <dht.h>
dht DHT;
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9, 10);
// Network uses that radio
RF24Network network(radio);
// Address of our node MAX 6 Nodes
const uint16_t this_node = 2;
// Address of the other node
const uint16_t other_node = 0;
// 1: ds18b20
// 2: dht22
const byte this_type = 2;
// How many have we sent already
unsigned long packets_sent;
// Structure of our payload
struct payload_t
{
unsigned long ms;
unsigned long counter;
byte device_type;
float humidity;
float temperature;
long voltage;
};
boolean serial = true;
float humidity;
float temperature;
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1125300L / result; // Back-calculate AVcc in mV
return result;
}
void sleepFiveMinutes()
{
for (int i = 0; i < 38; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
void setup(void)
{
if (serial) {
Serial.begin(57600);
Serial.println("RF24Network/NODE2_tx/");
}
pinMode(8, OUTPUT);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 93, /*node address*/ this_node);
}
void loop(void)
{
// Pump the network regularly
network.update();
uint32_t start = micros();
int chk = DHT.read22(3);
uint32_t stop = micros();
switch (chk)
{
case DHTLIB_OK:
if (serial) Serial.print("OK,\t");
if (serial) {
Serial.print(DHT.humidity, 2);
Serial.print(",\t");
Serial.println(DHT.temperature, 2);
}
humidity = DHT.humidity;
temperature = DHT.temperature;
break;
case DHTLIB_ERROR_CHECKSUM:
if (serial) Serial.print("Checksum error,\t");
humidity = -123;
temperature = -123;
break;
case DHTLIB_ERROR_TIMEOUT:
if (serial) Serial.print("Time out error,\t");
humidity = -123;
temperature = -123;
break;
default:
if (serial) Serial.print("Unknown error,\t");
humidity = -123;
temperature = -123;
break;
}
if (serial) Serial.print("Sending...");
payload_t payload = { millis(), packets_sent++, this_type, humidity, temperature, readVcc()};
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header, &payload, sizeof(payload));
if (ok) {
if (serial) Serial.println("ok.");
} else {
if (serial) Serial.println("failed.");
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
}
sleepFiveMinutes();
}
#define RF_DATA_PIN 30
#define SHORT_DELAY 250
#define LONG_DELAY (5*SHORT_DELAY)
#define START_DELAY (10*SHORT_DELAY)
#define END_DELAY (40*SHORT_DELAY)
#define TOTAL_DELAY (8*SHORT_DELAY)
#define SYNC_DELAY (29*SHORT_DELAY)
void rf_send(char* sCodeWord) {
for (int nRepeat=0; nRepeat<4; nRepeat++) {
int i = 0;
while (sCodeWord[i] != '\0') {
switch(sCodeWord[i]) {
case 'E':
digitalWrite(RF_DATA_PIN, HIGH);
delayMicroseconds(SHORT_DELAY);
digitalWrite(RF_DATA_PIN, LOW);
delayMicroseconds(END_DELAY);
break;
case 'S':
digitalWrite(RF_DATA_PIN, HIGH);
delayMicroseconds(SHORT_DELAY);
digitalWrite(RF_DATA_PIN, LOW);
delayMicroseconds(START_DELAY);
break;
case '1':
digitalWrite(RF_DATA_PIN, HIGH);
delayMicroseconds(SHORT_DELAY);
digitalWrite(RF_DATA_PIN, LOW);
delayMicroseconds(SHORT_DELAY);
break;
case '0':
digitalWrite(RF_DATA_PIN, HIGH);
delayMicroseconds(SHORT_DELAY);
digitalWrite(RF_DATA_PIN, LOW);
delayMicroseconds(LONG_DELAY);
break;
}
i++;
}
delayMicroseconds(SYNC_DELAY);
}
}
void socketSwicth(int id, int stat) {
Serial.print("ID: ");
Serial.println(id);
Serial.print("Stat: ");
Serial.println(stat);
switch(id) {
case 0:
if (stat==1) {
rf_send("S1001101001011010100110011001010101010101010101010110100101010101E");
} else {
rf_send("S1001101001011010100110011001010101010101010101010110101001010101E");
}
break;
case 1:
if (stat==1) {
rf_send("S1001101001011010100110011001010101010101010101010110100101010110E");
} else {
rf_send("S1001101001011010100110011001010101010101010101010110101001010110E");
}
break;
case 2:
if (stat==1) {
rf_send("S1001101001011010100110011001010101010101010101010110100101011001E");
} else {
rf_send("S1001101001011010100110011001010101010101010101010110101001011001E");
}
break;
/* case 3:
if (stat==1) {
rf_send("S1001101001011010100110011001010101010101010101010110100101010110E");
} else {
rf_send("S1001101001011010100110011001010101010101010101010110101001010110E");
}
break; */
case 9:
if (stat==1) {
rf_send("S1001101001011010100110011001010101010101010101010110010101010101E");
} else {
rf_send("S1001101001011010100110011001010101010101010101010110011001010101E");
}
break;
}
}
#include <Socketswitch.h>
Socketswitch mySwitch(12);
void setup() {
// Set device base address (0x0 - 0xFFFFFF). Device is a group of 16 channels (16 RF Socket Switches)
mySwitch.setDevice(0x34FCFF);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // VCC til RF modulet
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
Serial.begin(9600);
}
void loop() {
// Device channel 0-15
if (digitalRead(2) == HIGH) {
mySwitch.channelOn(0);
Serial.println("0 on");
} else if (digitalRead(3) == HIGH) {
mySwitch.channelOff(0);
Serial.println("0 off");
} else if (digitalRead(4) == HIGH) {
Serial.println("1 on");
mySwitch.channelOn(1);
} else if (digitalRead(5) == HIGH) {
Serial.println("1 off");
mySwitch.channelOff(1);
} else if (digitalRead(6) == HIGH) {
mySwitch.groupOn();
Serial.println("G on");
} else if (digitalRead(7) == HIGH) {
Serial.println("G off");
mySwitch.groupOff();
}
}
#include <MicroLCD.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Math.h>
#define ONE_WIRE_BUS 13
#define TEMPERATURE_PRECISION 12
#define LIGHT A0
#define OnOff 8
#define YELLOW 0
#define RED 9
#define GREEN 10
#define BLUE 11
#define OFF 0
#define ON 1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LCD_SSD1306 lcd;
DeviceAddress termo;
void setup() {
sensors.begin();
lcd.begin();
pinMode(OnOff, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
if (analogRead(LIGHT) < 350) {
unsigned long timer = millis();
int C = 0;
float tempC = 0;
while (millis() < timer + 30000) {
tempC = getTemp();
showIt("Temperature:", tempC);
C = round(tempC);
if (tempC > 7) setColor(GREEN);
else if (tempC <= 0) setColor(RED);
else setColor(BLUE);
switchIt(ON);
delay(500);
}
switchIt(OFF);
delay(2000);
switchIt(ON);
if (C < 0) C = C * -1;
blinkIt(C);
switch (OFF);
delay(2000);
}
else {
lcd.clear();
delay(60000);
}
}
void switchIt(byte state) {
if (state) digitalWrite(OnOff, ON);
else digitalWrite(OnOff, OFF);
}
void blinkIt(byte count) {
for (int i = 0; i < count; i++) {
showIt("Count:", i + 1);
switchIt(ON);
delay(500);
switchIt(OFF);
delay(500);
}
if (count == YELLOW) {
setColor(YELLOW);
for (int i = 0; i < 10; i++) {
switchIt(ON);
delay(50);
switchIt(OFF);
delay(50);
}
}
}
void setColor(byte color) {
analogWrite(RED, 255);
analogWrite(GREEN, 255);
analogWrite(BLUE, 255);
switch (color) {
case RED:
analogWrite(RED, 0);
break;
case GREEN:
analogWrite(GREEN, 0);
break;
case BLUE:
analogWrite(BLUE, 0);
break;
case YELLOW:
analogWrite(RED, 0);
analogWrite(GREEN, 0);
break;
}
}
void showIt(String text, float toPrint) {
int roundedNumber = toPrint;
lcd.clear();
lcd.setFontSize(FONT_SIZE_XLARGE);
lcd.setCursor(0, 0);
lcd.println(text);
lcd.setCursor(0, 3);
if (text.equals("Count:")) lcd.println(roundedNumber);
else lcd.println(toPrint);
lcd.setCursor(0, 6);
lcd.print("LUX: ");
lcd.println(analogRead(LIGHT));
}
float getTemp() {
sensors.getAddress(termo, 0);
sensors.setResolution(termo, TEMPERATURE_PRECISION);
sensors.requestTemperatures();
return sensors.getTempC(termo);
}
Hey Morten (Salomon)
Jeg fik lige 4 styk NRF24l01+ hjem igår
Køre RF24 LIB
Har du eventuelt et forslag til hvordan jeg via Arduino IDE på 2 maskiner laver kommunikation imellem dem? Sådan lidt en Wireless Radio Chat.
Mvh. Danni.