Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Vzdálenost

Simonik Feb 28th, 2016 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  //****************************************************************************************
  2. // vysilani na RaspberryPi - nepouziva struct, ktere nefunguje mezi ruznymi architekturami
  3. // ****************************************************************************************
  4.  
  5.  #define DEBUG 1
  6.  
  7.  #if DEBUG == 1
  8.  #define dprint(expression) Serial.print("# "); Serial.print( #expression ); Serial.print( ": " ); Serial.println( expression )
  9.  #define dshow(expression) Serial.println( expression )
  10.  #else
  11.  #define dprint(expression)
  12.  #define dshow(expression)
  13.  #endif
  14.  
  15.  
  16. #include <RF24Network.h>
  17. #include <RF24.h>
  18. #include <SPI.h>
  19. #include <OneWire.h>
  20. #include <DallasTemperature.h>
  21. #include "printf.h"
  22. #include <stdint.h>
  23. #include <avr/sleep.h>
  24. #include <avr/power.h>
  25.  
  26. #define ONE_WIRE_BUS        6 // teplota
  27. #define           CE       14 // CE
  28. #define           CSN      10 // CSN
  29. #define           pingPin   9 //
  30. #define           pongPin   8
  31. #define           LED      13
  32. #define           napajeni  7
  33.  
  34.  
  35.  
  36. long unsigned soundSpeed; // approx 340000 mm/s -> long is ok
  37.                             // long because of the operations involved.
  38.  
  39.  
  40.  
  41. OneWire oneWire(ONE_WIRE_BUS);
  42. DallasTemperature sensors(&oneWire);
  43.  
  44. 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;
  45. unsigned long awakeTime = 500;                          // How long in ms the radio will stay awake after leaving sleep mode
  46. unsigned long sleepTimer = 0;
  47. uint16_t lost_packets = 0;
  48.  
  49.  
  50. // Radio with CE & CSN
  51. RF24 radio(CE, CSN);
  52. RF24Network network(radio);
  53.  
  54. // Constants that identify this node and the node to send data to
  55. 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
  56. const uint16_t parent_node = 00;
  57.  
  58. // Time between packets (in ms)
  59. const unsigned long interval = 20000;
  60.  
  61.  
  62. // The network header initialized for this node
  63. RF24NetworkHeader header(parent_node);
  64.  
  65.  
  66. float dsTeplota;
  67. unsigned long pocitadlo=0;
  68. long napeti;
  69. bool vysilat = true;
  70. int vzdalenost;
  71. void setup(void)
  72. {
  73.  
  74.   pinMode(LED, OUTPUT); // blikani ledkou
  75.   pinMode(pingPin, OUTPUT);
  76.   pinMode(pongPin, INPUT);
  77.   pinMode(napajeni, OUTPUT);
  78.  
  79.  
  80.  
  81.   SPI.begin();
  82.   radio.begin();
  83.   delay(5);
  84.   network.begin(90, this_node);
  85.   sensors.begin();
  86.   printf_begin();
  87.   analogReference(INTERNAL);
  88.   Serial.begin(9600);
  89.   network.setup_watchdog(wdt_8s);  
  90.   dshow("Posilani dat z Ardurina");
  91. }
  92.  
  93. void loop() {
  94.  
  95.  // Update network data
  96.  network.update();
  97.  if (vysilat)
  98.  {
  99.  
  100.   dallas();
  101.   napeti=readVcc();
  102.   pocitadlo = pocitadlo + 1;
  103.  
  104.   int tempAir = (int) dsTeplota; // Celsius - of course, can/should be changed!
  105.   soundSpeed = 331300+606*tempAir; // mm/s; source: wikipedia
  106.   digitalWrite(napajeni, HIGH); //zapneme ZAPNEME na cidlo vzdalenosti
  107.   delay(10);
  108.   vzdalenost = readVzdalenost();
  109.   dprint(vzdalenost);
  110.   digitalWrite(napajeni, LOW); ////zapneme VYPNEME na cidlo vzdalenosti
  111.  
  112.  // nelze pouzit stuct mezi rozdílnymi architekturami, proto serializujeme !!!!!!
  113.  String str = "";
  114.  dprint(pocitadlo);
  115.  str+= String(pocitadlo);
  116.  str+= ";";
  117.  str+= String(dsTeplota,2);
  118.  str+= ";";
  119.  str+= String(napeti);
  120.  str+= ";";
  121.  str+= String(lost_packets);
  122.  str+= ";";
  123.  str+= String(vzdalenost);
  124.  
  125.  int str_len = str.length() + 1; // velikost dat + 1 na znak null
  126.  char char_array[str_len]; //
  127.  str.toCharArray(char_array, str_len);   // přecedem string na pole znaku
  128.    
  129.  dshow("Data:");
  130.  dprint(char_array);
  131.  dshow("velikodst dat:");
  132.  dshow(strlen(char_array));
  133.  dprint(napeti);
  134.  
  135.   // header typ - muze byt jakykoliv. Slouzi k urceni toho, co posilame
  136.   header.type = 'v';
  137.  
  138.   // Writing the message to the network means sending it
  139.   if (network.write(header, char_array, sizeof(char_array))) {
  140.     dshow("Zprava poslana\n");
  141.     lost_packets=0;
  142.   } else {
  143.     dshow("Nemuzu odeslat zpravu\n");
  144.     lost_packets++;
  145.   }
  146.   #if DEBUG == 1
  147.   digitalWrite(LED, HIGH); //LEDka blikani
  148.   #endif
  149.  
  150.   vysilat = false; // po odeslani dame false, protoze nemuzeme jit ihned spat a tak by se to odeslalo vicekrat
  151.  }
  152.   // Budeme spat, aby se snizila spotreba
  153.      if(millis() - sleepTimer > awakeTime){ // nemuzeme spat ihned, musi se chvili pockat
  154.       dshow("Sleep");
  155.       sleepTimer = millis();                           // Reset the timer value
  156.       delay(100);            
  157.      #if DEBUG == 1
  158.      digitalWrite(LED, LOW); //LEDka blikani
  159.      #endif
  160.       radio.stopListening();
  161.       network.sleepNode(2,255); //900s budeme spat, 255 zakaze probuzeni od preruseni
  162.    
  163.      
  164.      
  165.       vysilat=true; // opet chceme vysilat
  166.       dshow("Awake");
  167. }
  168.  
  169. }
  170.  
  171.  
  172. // *********************************************************************
  173. // ************************ FUNKCE *************************************
  174. // *********************************************************************
  175. // cteni teploty
  176. void dallas()
  177. {
  178.   // call sensors.requestTemperatures() to issue a global temperature
  179.   // request to all devices on the bus
  180.  
  181.   sensors.requestTemperatures(); // Send the command to get temperatures
  182.   dsTeplota = sensors.getTempCByIndex(0);
  183.   dshow("Dallas");
  184.   dshow(dsTeplota);  
  185. }
  186.  
  187. // finta na cteni napajeciho napeti bez externich soucastek
  188. long readVcc() {
  189.   // Read 1.1V reference against AVcc
  190.   // set the reference to Vcc and the measurement to the internal 1.1V reference
  191.   #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  192.     ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  193.   #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  194.     ADMUX = _BV(MUX5) | _BV(MUX0);
  195.   #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  196.     ADMUX = _BV(MUX3) | _BV(MUX2);
  197.   #else
  198.     ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  199.   #endif  
  200.  
  201.   delay(2); // Wait for Vref to settle
  202.   ADCSRA |= _BV(ADSC); // Start conversion
  203.   while (bit_is_set(ADCSRA,ADSC)); // measuring
  204.  
  205.   uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
  206.   uint8_t high = ADCH; // unlocks both
  207.  
  208.   long result = (high<<8) | low;
  209.  
  210.   result = 1080000L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  211.   return result; // Vcc in millivolts
  212. }
  213.  
  214.  
  215. unsigned long durCalc(int pinI, int pinO)
  216. {
  217.   // The PING is triggered by a HIGH pulse of 10 or more microseconds.
  218.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  219.   digitalWrite(pinI, LOW);
  220.   delayMicroseconds(2);
  221.   digitalWrite(pinI, HIGH);
  222.   delayMicroseconds(12);
  223.   digitalWrite(pinI, LOW);
  224.   //delay(10);
  225.   // The pongPin is used to read the signal from the PING))): a HIGH
  226.   // pulse whose duration is the time (in microseconds) from the sending
  227.   // of the ping to the reception of its echo off of an object.
  228.   return(pulseIn(pinO, HIGH, 35000)); // microseconds of (total) sound travel;
  229. }
  230.  
  231. #define NUM_READS 50
  232. int readVzdalenost(){
  233.    // read multiple values and sort them to take the mode
  234.    long unsigned sortedValues[NUM_READS];
  235.    for(int i=0;i<NUM_READS;i++){
  236.  
  237.      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
  238.      value = value*durCalc(pingPin, pongPin); // distance (mm) = time (total sound travelling distance, microseconds) * speed (mm/s)
  239.      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
  240.  
  241.      
  242.      int j;
  243.      if(value<sortedValues[0] || i==0){
  244.         j=0; //insert at first position
  245.      }
  246.      else{
  247.        for(j=1;j<i;j++){
  248.           if(sortedValues[j-1]<=value && sortedValues[j]>=value){
  249.             // j is insert position
  250.             break;
  251.           }
  252.        }
  253.      }
  254.      for(int k=i;k>j;k--){
  255.        // move all values higher than current reading up one position
  256.        sortedValues[k]=sortedValues[k-1];
  257.      }
  258.      sortedValues[j]=value; //insert current reading
  259.    }
  260.    //return scaled mode of 10 values
  261.    float returnval = 0;
  262.    for(int i=NUM_READS/2-5;i<(NUM_READS/2+5);i++){
  263.      Serial.println(sortedValues[i]);
  264.      returnval +=sortedValues[i];
  265.    }
  266.    returnval = returnval/10; // je 10 mereni !!!
  267.    return returnval;
  268. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top