Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //****************************************************************************************
- // vysilani na RaspberryPi - nepouziva struct, ktere nefunguje mezi ruznymi architekturami
- // ****************************************************************************************
- #define DEBUG 1
- #if DEBUG == 1
- #define dprint(expression) Serial.print("# "); Serial.print( #expression ); Serial.print( ": " ); Serial.println( expression )
- #define dshow(expression) Serial.println( expression )
- #else
- #define dprint(expression)
- #define dshow(expression)
- #endif
- #include <RF24Network.h>
- #include <RF24.h>
- #include <SPI.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "printf.h"
- #include <stdint.h>
- #include <avr/sleep.h>
- #include <avr/power.h>
- #define ONE_WIRE_BUS 6 // teplota
- #define CE 14 // CE
- #define CSN 10 // CSN
- #define pingPin 9 //
- #define pongPin 8
- #define LED 13
- #define napajeni 7
- long unsigned soundSpeed; // approx 340000 mm/s -> long is ok
- // long because of the operations involved.
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms, wdt_1s, wdt_2s, wdt_4s, wdt_8s } wdt_prescalar_e;
- unsigned long awakeTime = 500; // How long in ms the radio will stay awake after leaving sleep mode
- unsigned long sleepTimer = 0;
- uint16_t lost_packets = 0;
- // Radio with CE & CSN
- RF24 radio(CE, CSN);
- RF24Network network(radio);
- // Constants that identify this node and the node to send data to
- const uint16_t this_node = 02; // celkem je v siti 6 node tzn 00,01,02,03,04,05. 0 je master. Muzou byt sub node treba pro node 01 je to11,21,31,41,51
- const uint16_t parent_node = 00;
- // Time between packets (in ms)
- const unsigned long interval = 20000;
- // The network header initialized for this node
- RF24NetworkHeader header(parent_node);
- float dsTeplota;
- unsigned long pocitadlo=0;
- long napeti;
- bool vysilat = true;
- int vzdalenost;
- void setup(void)
- {
- pinMode(LED, OUTPUT); // blikani ledkou
- pinMode(pingPin, OUTPUT);
- pinMode(pongPin, INPUT);
- pinMode(napajeni, OUTPUT);
- SPI.begin();
- radio.begin();
- delay(5);
- network.begin(90, this_node);
- sensors.begin();
- printf_begin();
- analogReference(INTERNAL);
- Serial.begin(9600);
- network.setup_watchdog(wdt_8s);
- dshow("Posilani dat z Ardurina");
- }
- void loop() {
- // Update network data
- network.update();
- if (vysilat)
- {
- dallas();
- napeti=readVcc();
- pocitadlo = pocitadlo + 1;
- int tempAir = (int) dsTeplota; // Celsius - of course, can/should be changed!
- soundSpeed = 331300+606*tempAir; // mm/s; source: wikipedia
- digitalWrite(napajeni, HIGH); //zapneme ZAPNEME na cidlo vzdalenosti
- delay(10);
- vzdalenost = readVzdalenost();
- dprint(vzdalenost);
- digitalWrite(napajeni, LOW); ////zapneme VYPNEME na cidlo vzdalenosti
- // nelze pouzit stuct mezi rozdílnymi architekturami, proto serializujeme !!!!!!
- String str = "";
- dprint(pocitadlo);
- str+= String(pocitadlo);
- str+= ";";
- str+= String(dsTeplota,2);
- str+= ";";
- str+= String(napeti);
- str+= ";";
- str+= String(lost_packets);
- str+= ";";
- str+= String(vzdalenost);
- int str_len = str.length() + 1; // velikost dat + 1 na znak null
- char char_array[str_len]; //
- str.toCharArray(char_array, str_len); // přecedem string na pole znaku
- dshow("Data:");
- dprint(char_array);
- dshow("velikodst dat:");
- dshow(strlen(char_array));
- dprint(napeti);
- // header typ - muze byt jakykoliv. Slouzi k urceni toho, co posilame
- header.type = 'v';
- // Writing the message to the network means sending it
- if (network.write(header, char_array, sizeof(char_array))) {
- dshow("Zprava poslana\n");
- lost_packets=0;
- } else {
- dshow("Nemuzu odeslat zpravu\n");
- lost_packets++;
- }
- #if DEBUG == 1
- digitalWrite(LED, HIGH); //LEDka blikani
- #endif
- vysilat = false; // po odeslani dame false, protoze nemuzeme jit ihned spat a tak by se to odeslalo vicekrat
- }
- // Budeme spat, aby se snizila spotreba
- if(millis() - sleepTimer > awakeTime){ // nemuzeme spat ihned, musi se chvili pockat
- dshow("Sleep");
- sleepTimer = millis(); // Reset the timer value
- delay(100);
- #if DEBUG == 1
- digitalWrite(LED, LOW); //LEDka blikani
- #endif
- radio.stopListening();
- network.sleepNode(2,255); //900s budeme spat, 255 zakaze probuzeni od preruseni
- vysilat=true; // opet chceme vysilat
- dshow("Awake");
- }
- }
- // *********************************************************************
- // ************************ FUNKCE *************************************
- // *********************************************************************
- // cteni teploty
- void dallas()
- {
- // call sensors.requestTemperatures() to issue a global temperature
- // request to all devices on the bus
- sensors.requestTemperatures(); // Send the command to get temperatures
- dsTeplota = sensors.getTempCByIndex(0);
- dshow("Dallas");
- dshow(dsTeplota);
- }
- // finta na cteni napajeciho napeti bez externich soucastek
- long readVcc() {
- // Read 1.1V reference against AVcc
- // set the reference to Vcc and the measurement to the internal 1.1V reference
- #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
- ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
- ADMUX = _BV(MUX5) | _BV(MUX0);
- #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
- ADMUX = _BV(MUX3) | _BV(MUX2);
- #else
- ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
- #endif
- delay(2); // Wait for Vref to settle
- ADCSRA |= _BV(ADSC); // Start conversion
- while (bit_is_set(ADCSRA,ADSC)); // measuring
- uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
- uint8_t high = ADCH; // unlocks both
- long result = (high<<8) | low;
- result = 1080000L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
- return result; // Vcc in millivolts
- }
- unsigned long durCalc(int pinI, int pinO)
- {
- // The PING is triggered by a HIGH pulse of 10 or more microseconds.
- // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
- digitalWrite(pinI, LOW);
- delayMicroseconds(2);
- digitalWrite(pinI, HIGH);
- delayMicroseconds(12);
- digitalWrite(pinI, LOW);
- //delay(10);
- // The pongPin is used to read the signal from the PING))): a HIGH
- // pulse whose duration is the time (in microseconds) from the sending
- // of the ping to the reception of its echo off of an object.
- return(pulseIn(pinO, HIGH, 35000)); // microseconds of (total) sound travel;
- }
- #define NUM_READS 50
- int readVzdalenost(){
- // read multiple values and sort them to take the mode
- long unsigned sortedValues[NUM_READS];
- for(int i=0;i<NUM_READS;i++){
- long unsigned value = soundSpeed/100; // speed is in mm/s, duration in microseconds: 6 zeroes need to be removed, 2 go out here, the baA�ance later
- value = value*durCalc(pingPin, pongPin); // distance (mm) = time (total sound travelling distance, microseconds) * speed (mm/s)
- value = value/20000; // I remove the remaing 4 zeros here, plus the effect of two-way travel of the sound; this care with the zeros is because of size limitations of long
- int j;
- if(value<sortedValues[0] || i==0){
- j=0; //insert at first position
- }
- else{
- for(j=1;j<i;j++){
- if(sortedValues[j-1]<=value && sortedValues[j]>=value){
- // j is insert position
- break;
- }
- }
- }
- for(int k=i;k>j;k--){
- // move all values higher than current reading up one position
- sortedValues[k]=sortedValues[k-1];
- }
- sortedValues[j]=value; //insert current reading
- }
- //return scaled mode of 10 values
- float returnval = 0;
- for(int i=NUM_READS/2-5;i<(NUM_READS/2+5);i++){
- Serial.println(sortedValues[i]);
- returnval +=sortedValues[i];
- }
- returnval = returnval/10; // je 10 mereni !!!
- return returnval;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement