Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Problems understanding Struct in this example ! on: February 26, 2014, 08:47:01 am
Hi,

Thanks for that info AWOL. its starting to make some sense.

1) define a structure (a template if you like) ... does this reserve a space in memory for the ms and counter ?

2) make an instance of the structure and put some values in it. ... put the values in above, do they have to be in order, 'ms' and 'millis' are both the first item in each case and so on.

3) take the address of the structure. Go to this address find its length and and send all data out.

regards

Gary
2  Using Arduino / Programming Questions / Problems understanding Struct in this example ! on: February 26, 2014, 08:01:01 am
Hi,

Link http://maniacbug.github.io/RF24Network/

I am trying to understand how struct works in this example found below. I have tried to find out using the internet but find the explanations very confusing.My questions relate  to the lines marked with (1)  (2)  (3) respectively.

This is what I think may be happening ?

(1) Assigns members  unsigned long ms;  unsigned long counter;, but they are not put into memory or take any space.

(2) payload seems to become a variable here ?  also   millis(),   packets_sent    are used here but how are they related to (1)
 
(3) What does  &payload  do ?

It seems  unsigned long ms;   unsigned long counter; are never used ?



i would welcome a clear and easy to understand reply as I have never used structs before.

Thanks

Regards  Gary


Code:
/*
 Copyright (C) 2012 James Coliz, Jr. <maniacbug@ymail.com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */

/**
 * Simplest possible example of using RF24Network
 *
 * TRANSMITTER NODE
 * Every 2 seconds, send a payload to the receiver node.
 */

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 1;

// Address of the other node
const uint16_t other_node = 0;

// How often to send 'hello world to the other unit
const unsigned long interval = 2000; //ms

// When did we last send?
unsigned long last_sent;

// How many have we sent already
unsigned long packets_sent;

// Structure of our payload
struct payload_t                              // (1)
{
  unsigned long ms;
  unsigned long counter;
};

void setup(void)
{
  Serial.begin(57600);
  Serial.println("RF24Network/examples/helloworld_tx/");
 
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

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  )
  {
    last_sent = now;

    Serial.print("Sending...");
    payload_t   payload = { millis(), packets_sent++ };                                // (2)
    RF24NetworkHeader header(/*to node*/ other_node);
    bool ok = network.write(header,&payload,sizeof(payload));                   // (3)
    if (ok)
      Serial.println("ok.");
    else
      Serial.println("failed.");
  }
}
// vim:ai:cin:sts=2 sw=2 ft=cpp

3  Using Arduino / Programming Questions / Re: Read arduino supply voltage, giving garbage on sleep? on: February 22, 2014, 05:20:19 pm
Thanks to you all for your help, funny thing is I just did a search and found Serial.flush() ; and it does work.

Then came on here to let you know and PaulS has just written about it  smiley-eek

Thanks

regards

Gary
4  Using Arduino / Programming Questions / Read arduino supply voltage, giving garbage on sleep? on: February 22, 2014, 02:00:31 pm
Hi,

Have tried all afternoon but cannot find out why, if I add a sleep function, the supply voltage reads as garbage, its ok if I don't use the sleep function ?  It maybe the ADC is being turned off ?

Any help would be appreciated.  smiley

Regards

Gary

Code:

/*
  Blink
  Read arduino battery
  Print to serial port the voltage
  Sleep for 4 seconds
*/


  #include <avr/sleep.h>
  #include <avr/wdt.h>
  //#include <avr/power.h>
  int led = 13;

 
  ISR(WDT_vect) // watchdog interrupt
  {
  wdt_disable();  // disable watchdog
  }
 
void setup()
  {               
  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  }
 

void loop()
  {
  Serial.println(" LED ON");
  digitalWrite(led, HIGH);   
  delay(200);             
  digitalWrite(led, LOW);   
  Serial.println(" LED OFF");
  delay(1000);             
  readVcc() ; // Read Arduino  battery voltage
  Serial.print(" ");
  Serial.println( readVcc(), DEC );
  myWatchdogEnable (B0100000);  // Sleep 4 seconds //   *** Goes wrong when I add this line  ***
  }


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 = 1126400L / result; // Back-calculate AVcc in mV
  return result;
  }


void myWatchdogEnable(const byte interval)
  {
  MCUSR = 0;                          // reset various flags
  WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay

  wdt_reset();
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  sleep_mode();            // now goes to Sleep and waits for the interrupt
  }


5  Using Arduino / Sensors / Dallas Temperature libary non blocking question on: March 29, 2013, 06:33:20 am
Hi,

Just a quick question on using the DallasTemperature libary.

It has the command, sensors.setWaitForConversion(true);  // or this can be false

Obviously if I set it to true it will block for 750ms as per the data sheet.
If i set this to false, it is no longer waits for a conversion and is not blocking.

The library states that  'The programmer is responsible to wait long enough before reading the temperature values' 

I can set a timer to wait until 750ms has passed then get temperature reading, but I am wondering is there a 'flag'
that I can test in the libary, that informs me that the conversion is complete and therefore I can now read the temperature ?


Thanks

Regards


Gary
6  Using Arduino / Programming Questions / Re: dtostrf what does the width do, in my example? on: March 15, 2013, 06:52:26 am
Hi PaulS,

Thanks for that, I now understand how to use it.

regards

gary
7  Using Arduino / Programming Questions / dtostrf what does the width do, in my example? on: March 14, 2013, 06:05:02 pm
Hi,

Have been trying to work this out for last hour and a half ?

I can trim the result after the decimal point, as in this example will output 1234.8

But Width part of dtostrf has no effect, (in this example it set to 2) what does it do ? as it seems to be to small for the whole number including decimal point , but it prints out correctly?

Thanks

Regards

Gary

Code:
char  buffer[12]; 
float number = 1234.84;
     
dtostrf(number,2,1,buffer);  // dtostrf=(x,W,P,x) Width= Characters including dot & minus sign. Precision= Characters after decimal point.     
   
     Serial.println("");
     Serial.print(F("buffer = "));
     Serial.println(buffer);

 
8  Using Arduino / Programming Questions / Using Cosm, How can I insert #define text into a string ? on: February 19, 2013, 07:07:30 pm
Hi,

I want to add some text that I have previously defined into a string, I have tried all evening, but cannot work out how do it ?

Original string 
Cosm_client.publish("Vt7C1Sjuz9elWSAKxZdmhHTVl0alFuVT0g/v2/feeds/00077/datastreams/Outside_Temperature.csv ",buffer);


Now my defines:
#define Cosm_key                Vt7C1Sjuz9elWSAKxZdmhHTVl0alFuVT0g
#define Cosm_feed               00077


I know this isn't correct but just shows what I am trying to do:
Cosm_client.publish("Cosm_key/v2/feeds/Cosm_feed/datastreams/Outside_Temperature.csv ",buffer);

How can i insert the defines to be the same as the long string, as the original ?


Thanks


Regards


Gary
9  Using Arduino / Networking, Protocols, and Devices / Re: How do I check I have ethernet connection without delay ? on: February 18, 2013, 10:25:11 am
Hi Paul,

Brief overview: I am monitoring my energy usage in my home, which consists of  a UK gas meter that outputs a pulse for every 0.01 unit of gas used to heat my home. Also I collect outside air temperature and indoor temperature. Eventually it will monitor and action more. The temperatures and gas used are sent to my browser and displayed for m
y reference and also sending commands to the arduino.

The arduino's are situated in various rooms and garages, these are a long way from my PC, hence using ethernet. Hope that explains what I am trying to do.

The arduino is collecting data and then sends it to COSM (a place for storing data as well as displaying in a graphical format) https://cosm.com/
The arduino also sends data to my PC that gives me temperatures and gas usage.

The Cosm data does not need my PC to be ON as the arduino ethernet board sends this via my router to COSM, but the MQTT part does need my PC. (http://mqtt.org/) Eventually the PC will be on all day and turn of at 12.00 am and back on at 6.00am.

But at the moment I am still testing it.


Hope that helps

Regards

Gary

10  Using Arduino / Networking, Protocols, and Devices / Re: How do I check I have ethernet connection without delay ? on: February 18, 2013, 08:33:22 am
Hi Paul S,

Thanks for that, I always avoiding using interrupts as I thought they were hard to use, but that was so easy to setup, seems to be working now, just running checks on it for the rest of today.


Just thinking about the future, as I plan to make other devices and I may run into some other software problem, my original idea was to have a 'heartbeat' sent from my pc say every 5 seconds to the ethernet on the arduino and when the Arduino did not receive this it would then omit checking the void Intialise_MQTT(void) until the Arduino once again received the heartbeat.

I do have  code that runs on my PC, when the PC is ON it sends the text 'heartbeat' to the arduino ethernet every 5 seconds.

As far as I know the Wiznet ethernet board will look for a ethernet connection (without the arduino doing anything)and when it finds the correct address of my PC the link will once again be established.

If this is correct then when it gets the 'heartbeat' text again it would then know to call the void Intialise_MQTT(void) again.

Is this correct ?

Any help gratefully received !


Regards

Gary
11  Using Arduino / Networking, Protocols, and Devices / How do I check I have ethernet connection without delay ? on: February 18, 2013, 06:37:28 am
Hi,

My project reads a reed switch connected to a meter,  the switch activates every time a 0.01 measurement has been used, i can therefore tell home much i am using in a day. This is then uploaded to Cosm and to my local MQTT server, this is then processed further.

Although I am using in this instance a MQTT server this in actual fact uses the underlying Arduino ethernet library. So any suggestions you may give me will also work in my MQTT server (so I have been told).

I check about every .1 second if the switch  has changed state and debounce it, if it has, i increment a counter.

The switch will be high for about 3/4 second then returns to a low this repeats every 20 seconds and continually repeats this pattern while the meter is running.

This all works well until I turn off my PC which has the MQTT server on it, in my code the Arduino then keeps trying to reconnect, until the PC is next switched back On. When it is it reconnects it works normally again.

PROBLEM
-------
The problem is the Arduino library takes just over 1 second to try and reconnect, and  it seems to be a blocking routine, so I then miss about 2 out of every 3 pulses.

Is there a quicker way of checking if ethernet is connected or something else I can try ?

Regards

Gary


Code:
Here is part of the code:

EthernetClient MQTT_ethclient;
Ethernet.begin(mac, ip, dns_server, gateway);     


void loop(void)                                                                                                                                                 

   
    Check_MQTT_Ethernet_Connected();
    Check_Cosm_Ethernet_Connected();
   
    Check_reed_switch();                                                                    // Detect a pulse via Reed Switch
 
    Check_timers();
    Check_for_any_uploads(); 
   
    Cosm_client.loop();
    MQTT_client.loop(); 
}



void Check_MQTT_Ethernet_Connected()                                                        // Needs improving as 'LCD Menu' gets sluggish do to the Ethernet trying to reconnect
{
 
    if  ( !MQTT_client.connected())
    {
        Serial.println("MQTT disconnected ...");
        Intialise_MQTT();                                                                   // Setup Publish and Subscribe.
        MQTT_ethernet_connected_flag = false;
    }


    if  (MQTT_client.connected())
    {
        if (MQTT_ethernet_connected_flag == false)
        {
            Serial.println("MQTT Connected");
            MQTT_ethernet_connected_flag = true;
        }
    }
}


void Intialise_MQTT(void)
{
    if (MQTT_client.connect("arduinoClient_MQTT_02"))                                       // Arduino NAME shown on MQTT Broker
    {
        Serial.println("MQTT_client Intialised");
        MQTT_client.publish("sensor/002/mqtt_server_in","MQTT OK");                         // This is Data to be SENT from the Arduino to the MQTT MQTT_server
        MQTT_client.subscribe("sensor/002/mqtt_server_out");                                // This is RECEIVED from MQTT MQTT_server to this Arduino.
    }
}

12  Using Arduino / Programming Questions / Re: Complier window saying "expected primary-expression before ')' token" on: January 13, 2013, 04:59:25 pm
Hi PaulS,

I have solved that last problem, I remeber I had it before, if the following line was not at the end of the routine it would become corrupt,
but have no idea why, but putting it at the end solves the text error.

 client.publish("outTopic", payload, length);
See the code below as it has a note about this.

Thanks for your time PaulS, it was most helpful.

Regards
Gary

Added the working code here, for anyone that may read this in the future.

Code:
//My addition to original code:

    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
     
    byte copyOfPayload[80];
    int payloadSize;

    byte server[] = {192,168,1,64};                                                       
    byte ip[]     = {192,168,1,1};                                                         
    byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };                                 

    // Callback function header
    void callback(char* topic, byte* payload, unsigned int length);

    EthernetClient ethClient;
    PubSubClient client(server, 1883, callback, ethClient);


    // Callback function = only received at random intervals could be seconds hours or days.
    void callback(char* topic, byte* payload, unsigned int length) {
    //

      for(byte i=0; i<length; i++)
        {
           copyOfPayload[i] = payload[i];
        }
     
       payloadSize = length;
           
       client.publish("outTopic", payload, length); // NOTE must be placed at end of routine or else it gets corrupted
    }


void setup()
{
     Serial.begin(9600);   
     Ethernet.begin(mac, ip);
     
  if (client.connect("arduinoClient"))
    {
      client.publish("outTopic","hello world");
      delay (1000);
      client.subscribe("inTopic");
    }
}


void update()
{
  client.publish("outTopic", copyOfPayload , payloadSize);
}


void update1()
{
  char *payload = "blank";
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
}


void loop()
{
  update();       // Update after 1 seconds
  delay (1000);
   
  update1();       // Update after 1 seconds
  delay (1000);
   
  client.loop();
}


13  Using Arduino / Programming Questions / Re: Complier window saying "expected primary-expression before ')' token" on: January 13, 2013, 03:05:01 pm
Hi PaulS,

Thanks for that, I can see how its done now, not as complicated as I thought, Will come in handy for the future now.

Just one problem left I pass "test" into the callback, it sends it back to the MQTT server but it comes back as "estt"

I have added update1 so that I can see the text changing.

Code:
//My addition to original code:

    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
     
    byte copyOfPayload[80];
    int payloadSize;

    byte server[] = {192,168,1,64};                                                       
    byte ip[]     = {192,168,1,1};                                                         
    byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };                                 

    // Callback function header
    void callback(char* topic, byte* payload, unsigned int length);

    EthernetClient ethClient;
    PubSubClient client(server, 1883, callback, ethClient);


    // Callback function = only received at random intervals could be seconds hours or days.
    void callback(char* topic, byte* payload, unsigned int length) {
    client.publish("outTopic", payload, length);

      for(byte i=0; i<length; i++)
        {
           copyOfPayload[i] = payload[i];
        }
      payloadSize = length;
    }


void setup()
{
     Serial.begin(9600);   
     Ethernet.begin(mac, ip);
     
  if (client.connect("arduinoClient"))
    {
      client.publish("outTopic","hello world");
      client.subscribe("inTopic");
    }
}


void update()
{
  client.publish("outTopic", copyOfPayload , payloadSize);
}


void update1()
{
  char *payload = "blank";
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
  Serial.println (payload);
}


void loop()
{
  update();       // Update after 15 seconds
  delay (1000);
   
  update1();       // Update after 15 seconds
  delay (1000);
   
  client.loop();
}

14  Using Arduino / Programming Questions / Re: Complier window saying "expected primary-expression before ')' token" on: January 13, 2013, 01:32:15 pm
Hi,

I have tried various ways of trying to do what PaulS said, but cannot figure out how to do it.
I have googled arrays, pointers, chars and Strings, I do understand more but not yet enough to make the code work.
And am now a bit overwhelmed by it all  smiley-confuse

If someone could help that would be appreciated, then when I have the answer I will google again to find out how it works.

My code is still that same as my last reponse.

Thanks

regards

Gary
15  Using Arduino / Programming Questions / Re: Complier window saying "expected primary-expression before ')' token" on: January 12, 2013, 09:52:29 am
Hi PaulS,

Thanks that compiles and now works sending the string to my MQTT server.

Now all i need is to get the variable from:
void callback() 'byte* payload'

in place of the dummy "my payload"
Please find highlighted with *** below in routine 'update()'

Any ideas ?

regards

Gary



Code:
//My addition to original code:


#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xEF };
byte server[] = {192,168,1,64};     
byte ip[]     = {192,168,1,1};

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);


// Callback function = only received at random intervals could be seconds hours or days.
void callback(char* topic, byte* payload, unsigned int length)
{
  client.publish("outTopic", payload, length);
}

void setup()
{
  Ethernet.begin(mac, ip);
 
  if (client.connect("arduinoClient")) {
     client.publish("outTopic","hello world");
     client.subscribe("inTopic");
  }
}


void update()
{
  char *payload = "my payload"; // *** Need this to get variable from byte* payload in 'void callback' code above ***
  int length = strlen(payload);
  client.publish("outTopic", (byte *)payload , length);
}

void loop()
{
  client.loop();
  delay (1000);
  update();       // Update after 1 second
}


Pages: [1] 2 3 4