nRF24 / RF24Network

OSI Layer 3 Networking for nRF24L01(+) and nRF52x on Arduino and Raspberry Pi
https://nrf24.github.io/RF24Network/
GNU General Public License v2.0
355 stars 163 forks source link

RF24Network and NodeMCU ESP8266 #107

Closed ciapamusche closed 7 years ago

ciapamusche commented 7 years ago

Im tryng to run my sketch in which include rf24network library non my node mcu esp8266 Usually i run it in an ATmega without problem i get this error message.

Arduino:1.6.13 (Mac OS X), Scheda:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

/Users/Damiano/Documents/Arduino/libraries/RF24Network-master/RF24Network.cpp:30:24: fatal error: avr/sleep.h: No such file or directory
  #include <avr/sleep.h>
                        ^
compilation terminated.
exit status 1

i know the avr/sleep.h is not a ESP library but i don't have this library in my sketch. with include RF24.h only it compile.

any advice ?thank you very much for this fantastic job... it works perfectly in my security system.

Avamander commented 7 years ago

Can you enable the compilation debug and post the output of the full compilation?

ciapamusche commented 7 years ago

Sure... here we are...

Arduino:1.6.13 (Mac OS X), Scheda:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

/Users/Damiano/Downloads/Arduino.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Users/Damiano/Downloads/Arduino.app/Contents/Java/hardware -hardware /Users/Damiano/Library/Arduino15/packages -hardware /Users/Damiano/Documents/Arduino/hardware -tools /Users/Damiano/Downloads/Arduino.app/Contents/Java/tools-builder -tools /Users/Damiano/Downloads/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/Damiano/Library/Arduino15/packages -built-in-libraries /Users/Damiano/Downloads/Arduino.app/Contents/Java/libraries -libraries /Users/Damiano/Documents/Arduino/libraries -fqbn=esp8266:esp8266:nodemcuv2:CpuFrequency=80,UploadSpeed=115200,FlashSize=4M3M -ide-version=10613 -build-path /var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666 -warnings=none -prefs=build.warn_datapercentage=75 -prefs=runtime.tools.xtensa-lx106-elf-gcc.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2 -prefs=runtime.tools.mkspiffs.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/mkspiffs/0.1.2 -prefs=runtime.tools.esptool.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/esptool/0.4.9 -verbose /var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_modified_sketch_572706/RX1.9.ino /Users/Damiano/Downloads/Arduino.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Users/Damiano/Downloads/Arduino.app/Contents/Java/hardware -hardware /Users/Damiano/Library/Arduino15/packages -hardware /Users/Damiano/Documents/Arduino/hardware -tools /Users/Damiano/Downloads/Arduino.app/Contents/Java/tools-builder -tools /Users/Damiano/Downloads/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/Damiano/Library/Arduino15/packages -built-in-libraries /Users/Damiano/Downloads/Arduino.app/Contents/Java/libraries -libraries /Users/Damiano/Documents/Arduino/libraries -fqbn=esp8266:esp8266:nodemcuv2:CpuFrequency=80,UploadSpeed=115200,FlashSize=4M3M -ide-version=10613 -build-path /var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666 -warnings=none -prefs=build.warn_datapercentage=75 -prefs=runtime.tools.xtensa-lx106-elf-gcc.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2 -prefs=runtime.tools.mkspiffs.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/mkspiffs/0.1.2 -prefs=runtime.tools.esptool.path=/Users/Damiano/Library/Arduino15/packages/esp8266/tools/esptool/0.4.9 -verbose /var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_modified_sketch_572706/RX_1.9.ino Using board 'nodemcuv2' from platform in folder: /Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0 Using core 'esp8266' from platform in folder: /Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0 Opzioni di compilazione cambiate, ricompilo tutto Detecting libraries used... "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets -DICACHE_FLASH -USTRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "/var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/sketch/RX_1.9.ino.cpp" -o "/dev/null" "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets__ -DICACHE_FLASH -USTRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "-I/Users/Damiano/Documents/Arduino/libraries/RF24Network-master" "/var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/sketch/RX_1.9.ino.cpp" -o "/dev/null" "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets -DICACHE_FLASH -U__STRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "-I/Users/Damiano/Documents/Arduino/libraries/RF24Network-master" "-I/Users/Damiano/Documents/Arduino/libraries/RF24-master-2" "/var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/sketch/RX_1.9.ino.cpp" -o "/dev/null" "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets -DICACHE_FLASH -USTRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "-I/Users/Damiano/Documents/Arduino/libraries/RF24Network-master" "-I/Users/Damiano/Documents/Arduino/libraries/RF24-master-2" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/SPI" "/var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/sketch/RX_1.9.ino.cpp" -o "/dev/null" "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets__ -DICACHE_FLASH -USTRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266_NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "-I/Users/Damiano/Documents/Arduino/libraries/RF24Network-master" "-I/Users/Damiano/Documents/Arduino/libraries/RF24-master-2" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/SPI" "/Users/Damiano/Documents/Arduino/libraries/RF24Network-master/RF24Network.cpp" -o "/dev/null" "/Users/Damiano/Library/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2/bin/xtensa-lx106-elf-g++" -Dets -DICACHE_FLASH -U__STRICT_ANSI "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/include" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/sdk/lwip/include" "-I/var/folders/s_/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/core" -c -w -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -ffunction-sections -fdata-sections -w -x c++ -E -CC -DF_CPU=80000000L -DLWIP_OPEN_SRC -DARDUINO=10613 -DARDUINO_ESP8266_NODEMCU -DARDUINO_ARCH_ESP8266 -DARDUINO_BOARD="ESP8266NODEMCU" -DESP8266 "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/variants/nodemcu" "-I/Users/Damiano/Documents/Arduino/libraries/RF24Network-master" "-I/Users/Damiano/Documents/Arduino/libraries/RF24-master-2" "-I/Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/SPI" "/Users/Damiano/Documents/Arduino/libraries/RF24Network-master/RF24Network.cpp" -o "/var/folders/s/2yq07rw150v5yfj7z23hv0r80000gn/T/arduino_build_160666/preproc/ctags_target_for_gcc_minus_e.cpp" /Users/Damiano/Documents/Arduino/libraries/RF24Network-master/RF24Network.cpp:30:24: fatal error: avr/sleep.h: No such file or directory

include <avr/sleep.h>

                    ^

compilation terminated. Uso la libreria RF24Network-master alla versione 1.0.7 nella cartella: /Users/Damiano/Documents/Arduino/libraries/RF24Network-master Uso la libreria RF24-master-2 alla versione 1.2.0 nella cartella: /Users/Damiano/Documents/Arduino/libraries/RF24-master-2 Uso la libreria SPI alla versione 1.0 nella cartella: /Users/Damiano/Library/Arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/SPI exit status 1 Errore durante la compilazione per la scheda NodeMCU 1.0 (ESP-12E Module).

Avamander commented 7 years ago

Update your library. Use the "Download as .zip" option, remove all other versions of the library.

ciapamusche commented 7 years ago

Thank you avamander for the reply. Now it compile and load... my silly fault.... sorry

Now have another problem with the sketch. that i had not before.

i try to explain my situation in few words

My project is a sensor mesh (atmega328 in stand alone 8mhz configuration) which send via RFnetwork a bool PIR state,a bool door contact state, a float battery state and a bool "battery low" flag...

all these message are in the message struct.

Usually the receiver was another ATmega328 in standalone configuration.

for debug i use serial.println on serial monitor for TX and RX which return me the part of payload of my interesting.

I noticed Which the value of battery (usually around 3.30...3.40 v in my case) is sent correctly by the transmitter but the receiver give me ever a completely wrong value... fixed to 0.50. and a "battery low flag" which normally can be 0 or 1 if the battery goes under 2,9 v) at 96

with 2 Arduinos the sketch work perfectly...

can you help me?

i don't know if this is the right place to ask this question. If not you can close the question because for me the problem i solved updating RFNetwork library

Avamander commented 7 years ago

Try multiplying the battery state by 100 (convert it to integer not float) and store it in a unsigned 8 bit variable (uint8_t), on all devices you have to update the struct with that change. I haven't messed around with ESP8266s so I don't exactly know the quirks.

ciapamusche commented 7 years ago

now the battery is seen 75 when its 315 and 0 when 0. The battery low flag seems work.

I cannot understand the problem...

i can to lets you see my sketches...

RX NODE ON ESP

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

const uint16_t pi_node = 0; // Base
const uint16_t sensor_node1 = 01; // Primo ramo (parla solamente con 0 e con i suoi figli)
const uint16_t sensor_node2 = 021; // Secondo ramo (parla solamente con 01 e con i suoi figli)
const uint16_t sensor_node3 = 011;  // Secondo ramo (parla solamente con 01 e con i suoi figli)
const uint16_t sensor_node4 = 0121; // Terzo ramo (parla solamente con 021 e con i suoi figli)
const uint16_t sensor_node5 = 031; // Secondo ramo (parla solamente con 01)

// CE Pin, CSN Pin, SPI Speed
RF24 radio(2, 15); //impostare CE e CSN  (in questo caso pin 9 e 10);
RF24Network network(radio); 
// Time between checking for packets (in ms)
const unsigned long interval = 1000;

// Structure of our messages
struct message_sensor { 
 bool motion;
 bool dooropen;
 uin8_t Vbatt;
 bool s;
};

message_sensor message;

void setup ()

{

// Initialize all radio related modules

  Serial.begin(115200);
  delay(10);
  radio.begin();

  delay(5);

  network.begin(90, pi_node);

  radio.setPALevel(RF24_PA_MIN); 
  Serial.println("Receiver Ready"); 
  // Print some radio details (for debug purposes)
  radio.printDetails();
  network.update(); 
}
void loop()

  {
 bool goodSignal = radio.testRPD();

   if(radio.available()){

      Serial.println(goodSignal ? "Signal good > 64dBm" : "Segnale weak< 64dBm" );

       radio.read(0,0);

  }

network.update();

while ( network.available() ) 
     {

      RF24NetworkHeader header; 

      message_sensor sensormessage;

      network.peek(header);

      if (header.type == '1')
        {

        // Read the message

        network.read(header, &sensormessage, sizeof(sensormessage)); 

        // Print it out in case someone's watchin

        Serial.print("change from node  ");
        Serial.println(header.from_node);
        Serial.print("Motion sensor: ");
        Serial.println(sensormessage.motion);
        Serial.print("Contact sensor: ");
        Serial.println(sensormessage.dooropen);
        Serial.print("Battery sensor ");
        Serial.print(sensormessage.Vbatt);
        Serial.print("Flag Battery low: ");
        Serial.print(sensormessage.s);

        char buffer [50];

        switch (header.from_node) 
              {

              case sensor_node1:

              if (sensormessage.motion==1)
                    printf ("PIR active on %i\n", header.from_node);
                    else
                    printf ("PIR deactive on node %i\n", header.from_node);

                    if (sensormessage.dooropen==1)
                    printf ("Contact open on node %i\n", header.from_node);
                    else
                    if (sensormessage.dooropen==0)
                    printf ("Contatto closed on node %i\n", header.from_node);
                    Serial.println("----------------------------------------------------------");
                    break;

               case sensor_node2:
                   if (sensormessage.motion==1)
                    printf ("PIR attivo su nodo %i\n", header.from_node);
                    else
                    printf ("PIR disattivo su nodo %i\n", header.from_node);

                    if (sensormessage.dooropen==1)
                    printf ("Contatto aperto su nodo %i\n", header.from_node);
                    else
                    if (sensormessage.dooropen==0)
                    printf ("Contatto chiuso su nodo %i\n", header.from_node);
                    Serial.println("----------------------------------------------------------");
                    break;

             case sensor_node3:

                     if (sensormessage.motion==1)
                    printf ("PIR attivo su nodo %i\n", header.from_node);
                    else
                    printf ("PIR disattivo su nodo %i\n", header.from_node);

                    if (sensormessage.dooropen==1)
                    printf ("Contatto aperto su nodo %i\n", header.from_node);
                    else
                    if (sensormessage.dooropen==0)
                    printf ("Contatto chiuso su nodo %i\n", header.from_node);
                    Serial.println("----------------------------------------------------------");
                    break;
               default:

                    printf ("Unknown node %i\n", header.from_node);

                    break; 
        }

      } else {

        // This is not a type we recognize

        network.read(header, &sensormessage, sizeof(sensormessage));

        printf("Unknown message received from node %i\n", header.from_node);

      }

    }

  }

i translate in english only the first switch case... so you can understand wath the sketch do

TRANSMITTER SKETCH ON AVRS

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <avr/sleep.h>
#include <avr/power.h>
#define interval 1000

// PIR variables
byte pirPin = 7;
int pirCalibrationTime = 30;
bool nocal;

int PIRWake = 3;
int ContactWake = 2;

// Magnetic Door Sensor variable
byte switchPin = 6;

//Battery sensor variabiles

int Vpin_perc;
float Vbatt;

// Radio with CE & CSN connected to pins 7 & 8
RF24 radio(9, 10);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 011;
const uint16_t parent_node = 0;

// Time between packets (in ms)
//const unsigned long interval = 1000;  // every sec
unsigned long timestart = millis() ;

bool d;
bool m;
bool s;
 };

struct message_1 {
  bool motion;  
  bool dooropen;
  uint8_t Vpin_perc;
  bool s;

};

message_1 message;
RF24NetworkHeader header(parent_node);

void setup(void)
{
  // Set up the Serial Monitor
  Serial.begin(115200);

  // Initialize all radio related modules
  SPI.begin();
  radio.begin();
  delay(5);
  network.begin(/*canale di comunicazione*/90, this_node); 

  radio.setPALevel(RF24_PA_MIN); //-18 dBm
 // radio.setPALevel(RF24_PA_LOW);  //-12 dBm
 // radio.setPALevel(RF24_PA_HIGH); //-6dBm
 //radio.setPALevel(RF24_PA_MAX);  //0 dBm

   radio.setRetries(15,15);

  // Activate the internal Pull-Up resistor for the door sensor
  pinMode(switchPin, INPUT);
  pinMode(ContactWake, INPUT_PULLUP);
  pinMode(PIRWake,INPUT);

  // Calibrate PIR
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  Serial.print("Calibrating PIR ");
 // for(int i = 0; i < pirCalibrationTime; i++)
 // {
 //   Serial.print(".");
 //    delay(1000);
 // }

  Serial.println("*** TX OPERATIVE PROGRAM v1.8 ***");
  delay(50);
  radio.printDetails();

}

void loop() {  
testbatt(); 

if (s == 1)
 Serial.println("Battery low... Throw it away and buy a new one... ");

if      (digitalRead(pirPin) == LOW && digitalRead (switchPin) == LOW) 
        {
        Serial.println("adormo");
        bool d = (digitalRead(switchPin) == HIGH); 
        bool m = (digitalRead(pirPin) == HIGH);
        message = (message_1){m, d ,Vpin_perc,s};
         if (network.write(header, &message, sizeof(message))) { 
                      Serial.print("Message sent\n"); 
                    } else {
                      Serial.print("Could not send message\n"); 
                    }
        delay (interval);
        radio.powerDown(); 
        funzioneSleep(); 
        }
else
       { 

      // Read door sensor: HIGH means door is open (the magnet is far enough from the switch)
      bool d = (digitalRead(switchPin) == HIGH);
      // Read motion: HIGH means motion is detected
      bool m = (digitalRead(pirPin) == HIGH);
       // Headers will always be type 1 for this node
      // We set it again each loop iteration because fragmentation of the messages might change this between loops
      header.type = '1';

      if (m != message.motion || 
          d != message.dooropen) 
                 {

                    message = (message_1){m, d,Vpin_perc,s};
                    // Writing the message to the network means sending it
                    if (network.write(header, &message, sizeof(message))) {
                      Serial.print("Message sent\n"); 
                    } else {
                      Serial.print("Could not send message\n"); 
                    }
                 }
      }            

 // Serial.println("Updating Network...");
 while (millis()-timestart < interval); 
       { network.update(); } 
  timestart = millis() ;

  }

  //////////////////////////////////////////////////////////////////////////////////////////////////////////////

void funzioneSleep(void) 
{

   //PRR = (1<<PRUSART0);
   ADCSRA = 0; // disabilito il sottosistema ADC
  // set_sleep_mode(SLEEP_MODE_IDLE);       // tipo di modalità usata
  // set_sleep_mode(SLEEP_MODE_ADC);      // tipo di modalità usata
  // set_sleep_mode(SLEEP_MODE_PWR_SAVE); // tipo di modalità usata
  // set_sleep_mode(SLEEP_MODE_STANDBY);  // tipo di modalità usata
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // tipo di modalità usata
pinMode(0,INPUT_PULLUP);
  noInterrupts();

  sleep_enable();  // abilito il bit sul registro MCUCR dell'AVR

  attachInterrupt(0,INT0, RISING);
  attachInterrupt(1,INT1, RISING);

  interrupts();

  // power_adc_disable();
  //  power_spi_disable();
  //  power_timer0_disable();
  //  power_timer2_disable();
  //  power_twi_disable();
  sleep_mode(); // mando la scheda in sleep

  // disabilito lo sleep
  sleep_disable();

    power_all_enable();

  detachInterrupt(0);
  radio.begin();
 nocal = true;

}

void testbatt() 

{
 int sensorValue = analogRead(A0); 

  Vpin_perc = map (sensorValue, 0, 1023, 0, 420); 
  Vbatt = Vpin_perc / 100 ;

  s=(Vpin_perc < 290); 
  Serial.println(s);
 Serial.println(Vpin_perc);
 // Serial.println(Vbatt);
 /* if (network.write(header, &messageo, sizeof(messageo))) {
      Serial.print("Battery Message sent\n"); 
    } else {
      Serial.print("Could not send Battery message\n"); 
    }

}

i can miss some ; in copy and paste....

the variable i send for battery is "Vpin_perc" and the bool is "s"

thank you for your help

Avamander commented 7 years ago

You can't store value higher than 255 (0-255) in uint8_t, change the map() in testbatt() to map it to maximum 255. Also use, instead of int, unsigned int, unsigned long int, bool and so on, these:

int8_t: signed 8-bit (has 2^8 values)
uint8_t: unsigned 8-bit
int16_t: signed 16-bit (has 2^16 values)
uint16_t: unsigned 16-bit
int32_t: signed 32-bit
uint32_t: unsigned 32-bit
int64_t: signed 64-bit
uint64_t: unsigned 64-bit

You'll in the end remember the ranges, they should compile into same sized variables on all platforms and you'll learn not to use larger types than you need (as you know the ranges).

ciapamusche commented 7 years ago

very thanks i tried to convert the float and sand but didn't work.. however with your advice transmitted an uint16_t to receiver with the value and once received i divided by 100 in the receiver..

Thank you for your help. Question closed....