Show Posts
Pages: 1 2 [3] 4 5 ... 8
31  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 08, 2013, 06:02:59 pm
The code is send over ethernet to the second arduino.
Code:
if (justreleased[0]) {
      justreleased[0] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just released");
}
Sometimes shows:
5 Just released
3 Just released
2 Just released
3 Just released
in the serial Monitor.
But also if i see for example "5 Just released" i receive the code in Mosquitto until it stops working.
32  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 08, 2013, 02:36:54 pm
Ok, it must be the button naming.
Can somebody tell me where i have to change the "i" to a button number:
Code:
if (justreleased[i]) {
      justreleased[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just released");
      client.publish("foo/shower","0"); // Turn off Shower Light
}
I don't get it.
And maybe how to make it work with "just pressed" and sending the code just one time.
33  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 07, 2013, 09:04:44 pm
Is there a way to debug this stuff to find out why it stops working after some time?
34  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 07, 2013, 07:32:11 pm
In Ladyada's code it's:
Code:
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {14, 15, 16, 17, 18, 19}; // the analog 0-5 pins are also known as 14-19
So for the Mega i thought it's "54, 55, 56, 57, 58, 59".
And looks like this is correct cause the button (A0) works.
35  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 07, 2013, 02:58:09 pm
I have the code now on an Mega and set:
Code:
byte buttons[] = {54, 55, 56, 57, 58, 59};
But it's the same. It works and after some time stops working (also without doing anything).

In the command prompt on my windows server i receive nothing when it stops working.
If it works i see:
Code:
Client mosqsub/4868-SIREN received PUBLISH (d0, q0, r0, m0, 'foo/shower', ... (1 bytes)) 0
if Mosquitto is started with "mosquitto_sub -d -t foo/#" in the command prompt.
36  Using Arduino / Project Guidance / Re: How to know if a Arduino has enough memory for a project on: February 07, 2013, 06:16:34 am
I have an old Mega and the last revision.
The reason i took the Nano for the bathroom was that i can solder him on a Stripboard.
Yes i saw there is a shield with screw terminals. But it costs money, you have to order
it and wait for another part and then i also have to solder the cables to the PCB/Stripboard
and then screw them to the shield.

Also with just plugging the cables in the shield headers (something i don't like for fixed installation)
it's a cable mess.
37  Using Arduino / Project Guidance / How to know if a Arduino has enough memory for a project on: February 06, 2013, 10:10:24 am
Hi,

I'm doing some home automation stuff and sensor reading with some Arduinos.
I bought a Nano for the bathroom and now I'm scared that the 32 KB are not enough.
I chose the Nano cause it's easy to integrate him into the PCB.

I have just the code i posted here now on it: http://arduino.cc/forum/index.php/topic,141240.msg1105074.html#msg1105074
So 14 KB are already gone.

But i want to see life data of my sensors on the web page, save data to the SD-Card and upload them to
the servers database.

This is what was planned:
D2 S0-Interface Electricity Meter Bathroom Underfloor heating
D3 S0-Interface Electricity Meter
D5 One Wire BUS (1 DS18B20)
D6 Flow Sensor Bathroom Sink Cold
D7 Flow Sensor Bathroom Sink Hot
D8 Flow Sensor Shower
D9 Flow Sensor Toilette

A0 (14) Reed Switch Bathroom Window
A1 (15) Push Switch 1 (Shower On)
A2 (16) Push Switch 2 (Shower Off)
A3 (17) Push Switch 3 (Bathroom On)
A4 (18) Push Switch 4 (Bathroom Off)
A5 (19) Push Switch 5 (Bathroom On)
A6 (20) Push Switch 6 (Bathroom Off)

The push switch stuff is send trough my Windows server with MQTT to another Arduino and
later i also want to change the code so that i can dime the lights.
Looks like 32 KB is not enough and i need a Mega?

PS:
Is it a good idea to start one thread for the whole project instead of spreading the stuff
where i need help and have problems over the forum? Cause this way all the code is
in one place and i do not need to copy it around and always need to explain what the
rest of the code does that has nothing to do with a particular thread.
38  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 05, 2013, 09:15:05 am
This is the code i have on my Nano 328 where the buttons are connected:
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>

/**************************************************************
*                         Connections                         *
**************************************************************/
#define ONE_WIRE_BUS 5 // oneWire pin is D5

#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty
// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {14, 15, 16, 17, 18, 19}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed'
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/**************************************************************
*                      Ethernet Settings                      *
**************************************************************/
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 132};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// MQTT Server
byte server[] = { 192, 168, 0, 1 };
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}
// Fire up our PubSub client
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

/**************************************************************
*                           Setup                             *
**************************************************************/
void setup()
{
  byte i;
  byte dsAddress[8];
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println( "Searching for DS18B20..." );
  oneWire.reset_search(); // Start the search with the first device
  if( !oneWire.search(dsAddress) )
  {
    Serial.println( "none found. Using default MAC address." );
  } else {
    Serial.println( "success. Setting MAC address:" );
    Serial.print( " DS18B20 ROM  =" );
    for( i = 0; i < 8; i++)
    {
      Serial.write(' ');
      Serial.print( dsAddress[i], HEX );
    }
    Serial.println();
   
    // Offset array to skip DS18B20 family code, and skip mac[0]
    mac[1] = dsAddress[3];
    mac[2] = dsAddress[4];
    mac[3] = dsAddress[5];
    mac[4] = dsAddress[6];
    mac[5] = dsAddress[7];
  }

  Serial.print( " Ethernet MAC =" );
  for( i = 0; i < 6; i++ )
  {
    Serial.write( ' ' );
    Serial.print( mac[i], HEX );
  }
  Serial.println();

  Ethernet.begin(mac, ip, subnet, gateway);
  Serial.print(" IPv4 address: ");
  Serial.println(Ethernet.localIP());

  // Start up the Dallas Temperature library
  sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
   
  // Connect to MQTT Broker, give it Bath-O-Matic as the name
  if (client.connect("Bath-O-Matic")) {
    // Publish a message to the status topic
    client.publish("status","Bath-O-Matic is now online");
  }

  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
 
  // Run timer2 interrupt every 15 ms
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;
}


SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() < lasttime) {
     // we wrapped around, lets just try again
     lasttime = millis();
  }
 
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return;
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
 
  for (index = 0; index < NUMBUTTONS; index++) {
     
    currentstate[index] = digitalRead(buttons[index]);   // read the button
   
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
   
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}

void loop() {
 
  for (byte i = 0; i < NUMBUTTONS; i++) {

    if (justreleased[0]) {
      justreleased[0] = 0;
      client.publish("foo/shower","1"); // Turn off Shower Light
    }

  }
}
At the moment the DS18B20's are just to set unique MAC addresses.
On my Windows 2012 Server i have Mosquitto running.
I think the code on the Mega is ok cause i can turn the light on and off many times with PHP
without problems:
Code:
<?php
require("phpMQTT.php");
$mqtt = new phpMQTT();
/* broker(broker address, broker port, client id); */
$mqtt->broker("192.168.0.1"1883"PHP MQTT Client");
$mqtt->connect();
/* publish( topic, message, qos); */
$mqtt->publish("foo/shower","0",0);
$mqtt->close();
?>
39  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: February 05, 2013, 09:14:44 am
Hm, I'm unsure how where i have to replace the "i" with the button number.
Code:
if (justreleased[i]) {
      justreleased[i] = 0;
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
It works a view times and then it stops working.
And how do i make it that the light goes on when i press and send my code just one time?

But here is the whole thing:
I want to switch the light from one Arduino on another Arduino via MQTT over my windows server.
This is the code that i have on my Mega 2560 where the light is connected:
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>

/**************************************************************
*                         Connections                         *
**************************************************************/
#define ONE_WIRE_BUS 5 // oneWire pin is D5
#define showerPin 6    // Shower Light is D6

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/**************************************************************
*                      Ethernet Settings                      *
**************************************************************/
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 131};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// MQTT Server
byte server[] = { 192, 168, 0, 1 };
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};

/**************************************************************
*                           MQTT                              *
**************************************************************/
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("New message from broker on topic:");
  Serial.println(topic);

  Serial.print("Payload:");
  Serial.write(payload, length);
  
  if (strcmp(topic,"foo/shower")==0) {
    if (payload[0] == '0') {
      digitalWrite(showerPin, LOW); // Turn off Shower Light
    } else if (payload[0] == '1') {
      digitalWrite(showerPin, HIGH); // Turn on Shower Light
   }
  }
}

/**************************************************************
*                           Setup                             *
**************************************************************/
void setup()
{
  byte i;
  byte dsAddress[8];
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println( "Searching for DS18B20..." );
  oneWire.reset_search(); // Start the search with the first device
  if( !oneWire.search(dsAddress) )
  {
    Serial.println( "none found. Using default MAC address." );
  } else {
    Serial.println( "success. Setting MAC address:" );
    Serial.print( " DS18B20 ROM  =" );
    for( i = 0; i < 8; i++)
    {
      Serial.write(' ');
      Serial.print( dsAddress[i], HEX );
    }
    Serial.println();
    
    // Offset array to skip DS18B20 family code, and skip mac[0]
    mac[1] = dsAddress[3];
    mac[2] = dsAddress[4];
    mac[3] = dsAddress[5];
    mac[4] = dsAddress[6];
    mac[5] = dsAddress[7];
  }

  Serial.print( " Ethernet MAC =" );
  for( i = 0; i < 6; i++ )
  {
    Serial.write( ' ' );
    Serial.print( mac[i], HEX );
  }
  Serial.println();

  Ethernet.begin(mac, ip, subnet, gateway);
  Serial.print(" IPv4 address: ");
  Serial.println(Ethernet.localIP());

  // Start up the Dallas Temperature library
  sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement

  // Connect to MQTT Broker, give it Switch-O-Matic as the name
  if (client.connect("Switch-O-Matic")) {
    // Publish a message to the status topic
    client.publish("status","Switch-O-Matic is now online");
    // Listen for messages on the control topic
    client.subscribe("foo/#");
  }

  pinMode(showerPin, OUTPUT);
  digitalWrite(showerPin, LOW);
  
}
 
void loop()
{
  client.loop();
}
40  Using Arduino / Installation & Troubleshooting / Re: Sketch working on Mega 2560 R3 but not on R1/2 on: January 11, 2013, 07:02:34 pm
Not exactly the reverse situation - cause mine are both Mega2560 smiley-razz
41  Using Arduino / Installation & Troubleshooting / Re: Sketch working on Mega 2560 R3 but not on R1/2 on: January 11, 2013, 12:20:18 am
No Uno.
As i wrote both are Megas - just different revisions.

What looks alien maybe is that i use a DS18B20 to generate a unique MAC address
on all my Ethernet Arduinos.
This way you always have unique MAC address's without taking care of them.
42  Using Arduino / Programming Questions / Re: Ladyada's multi-button checker - how to? on: January 10, 2013, 09:28:34 pm
so in
Code:
if (justreleased[i])
i have to replace the "i" with the button number.
This works.

But my code gets send to many times.
As long as i press the button if i use "pressed".
So how to make sure it gets send just one time?

Quote
The switches don't trigger the interrupt. The clock does.
So this interrupt has nothing to do with the Arduino hardware interrupts that i have only on
some pins like 2 and 3?
43  Using Arduino / Installation & Troubleshooting / Re: Sketch working on Mega 2560 R3 but not on R1/2 on: January 10, 2013, 07:01:26 pm
Nobody has a idea why it works on one and not the other smiley-confuse ?
44  Using Arduino / Programming Questions / Ladyada's multi-button checker - how to? on: January 07, 2013, 03:48:51 am
Hi,

i found the Ladyada's multi-button checker: http://www.adafruit.com/blog/2009/10/20/example-code-for-multi-button-checker-with-debouncing/
First thing i don't get is how i can trigger something with the code (for example turn on an LED).
And the second thing i don't understand is that interrupts are used on all the pins in the second example.
45  Using Arduino / Installation & Troubleshooting / Sketch working on Mega 2560 R3 but not on R1/2 on: January 06, 2013, 05:09:31 pm
Hi,

my Sketch is working on the Mega 2560 R3 but not on an older one.
I don't know if its an R1 or R2 cause there is no print on the PCB.
I also switched the ethernet shield to make sure its not the shield.

The MQTT Server does not receive the "hello world" from the older Mega 2560.

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>

/**************************************************************
*                         Connections                         *
**************************************************************/
#define ONE_WIRE_BUS 5 // oneWire pin is D5

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

/**************************************************************
*                      Ethernet Settings                      *
**************************************************************/
// Ethernet Card Mac Address
byte mac[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
// IPv4 address
byte ip[] = {192, 168, 0, 132};
// Subnet mask
byte subnet[] = {255, 255, 255, 0};
// Default gateway
byte gateway[] = {192, 168, 0, 1};
// MQTT Server
byte server[] = { 192, 168, 0, 1 };
// Preferred DNS sever
// byte dns[] = {192, 168, 0, 1};

/**************************************************************
*                           MQTT                              *
**************************************************************/
void callback(char* topic, byte* payload, unsigned int length)
{

}

// Fire up PubSub client
PubSubClient client(server, 1883, callback);

/**************************************************************
*                           Setup                             *
**************************************************************/
void setup()
{
  byte i;
  byte dsAddress[8];
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println( "Searching for DS18B20..." );
  oneWire.reset_search(); // Start the search with the first device
  if( !oneWire.search(dsAddress) )
  {
    Serial.println( "none found. Using default MAC address." );
  } else {
    Serial.println( "success. Setting MAC address:" );
    Serial.print( " DS18B20 ROM  =" );
    for( i = 0; i < 8; i++)
    {
      Serial.write(' ');
      Serial.print( dsAddress[i], HEX );
    }
    Serial.println();
   
    // Offset array to skip DS18B20 family code, and skip mac[0]
    mac[1] = dsAddress[3];
    mac[2] = dsAddress[4];
    mac[3] = dsAddress[5];
    mac[4] = dsAddress[6];
    mac[5] = dsAddress[7];
  }

  Serial.print( " Ethernet MAC =" );
  for( i = 0; i < 6; i++ )
  {
    Serial.write( ' ' );
    Serial.print( mac[i], HEX );
  }
  Serial.println();

  Ethernet.begin(mac, ip, subnet, gateway);
  Serial.print(" IPv4 address: ");
  Serial.println(Ethernet.localIP());

  // Start up the Dallas Temperature library
  sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement

   if (client.connect("arduinoClient", "testuser", "testpass")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  }
   
}
 
void loop()
{
  client.loop();
}
Pages: 1 2 [3] 4 5 ... 8