{"id":3063,"date":"2019-08-23T01:48:42","date_gmt":"2019-08-22T23:48:42","guid":{"rendered":"http:\/\/arduino.net.pl\/?p=3063"},"modified":"2021-02-25T15:50:37","modified_gmt":"2021-02-25T14:50:37","slug":"nrf24-network-i-przesylanie-struktur","status":"publish","type":"post","link":"https:\/\/arduino.net.pl\/index.php\/nrf24-network-i-przesylanie-struktur\/","title":{"rendered":"Sie\u0107 nRF24 i przesy\u0142anie struktur"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/*\nNadajnik\n*\/\n\n\/*\nNadajnik\n*\/\n#include &lt;RF24.h&gt;\n#include &lt;RF24Network.h&gt;\n\n\nRF24 radio(7, 8);               \/\/ nRF24L01 (CE,CSN)\nRF24Network network(radio);      \/\/ Include the radio in the network\nconst uint16_t this_node = 00;   \/\/ Address of this node in Octal format ( 04,031, etc)\nconst uint16_t node01 = 01; \/\/ (Address where the data is going)\n\nstruct package\n  {\n    float temp = 11.11;\n    float hum = 22.22;\n    uint16_t light = 33;\n    uint16_t batt = 44; \n  };\ntypedef struct package Package;\nPackage data;\n\nvoid setup() {\n\n  Serial.begin(9600);\n\n\/\/  SPI.begin();\n  radio.begin();\n  radio.setPALevel(RF24_PA_HIGH);\n  network.begin(32, this_node);  \/\/(channel, node address)\n}\n\nvoid loop() {\n\n  network.update();\n\n  RF24NetworkHeader header(node01);     \/\/ (Address where the data is going)\n  bool ok = network.write(header, &amp;data, sizeof(data)); \/\/ Send the data\n  Serial.println(&quot;sending array...&quot;);\n  delay(1000);\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/*\n * Odbiornik\n * \n *\/\n\n#include &lt;RF24.h&gt;\n#include &lt;RF24Network.h&gt;\n\nRF24 radio(7, 8); \nRF24Network network(radio);      \/\/ Include the radio in the network\nconst uint16_t this_node = 01;   \/\/ Address of this node in Octal format ( 04,031, etc)\n\/\/const uint16_t node00 = 00;      \/\/ (Address where the data is going)\n\nstruct package\n  {\n    float temp = 11.11;\n    float hum = 22.22;\n    uint16_t light = 33;\n    uint16_t batt = 44; \n  };\ntypedef struct package Package;\nPackage incomingData;\n\nvoid setup() {\n  Serial.begin(9600);\n  radio.begin();\n  radio.setPALevel(RF24_PA_HIGH);\n  network.begin(32, this_node); \/\/(channel, node address)\n}\n\nvoid loop() {\n  \n  network.update();\n  while ( network.available() ) {     \/\/ Is there any incoming data?\n    RF24NetworkHeader header;\n\n    network.read(header, &amp;incomingData, sizeof(incomingData)); \/\/ Read the incoming data\n    \n      Serial.println(&quot;Struktura, 4 elementy:&quot;);\n      Serial.println(incomingData.temp);\n      Serial.println(incomingData.hum);\n      Serial.println(incomingData.light);\n      Serial.println(incomingData.batt);\n      Serial.println(&quot;Koniec&quot;);\n\n  }\n\n}\n<\/pre><\/div>\n\n\n<p>Po\u017cyteczna funkcja, kt\u00f3ra wysy\u0142a dane, w tym przypadku struktur\u0119 <strong>package<\/strong> sk\u0142adaj\u0105c\u0105 si\u0119 z dw\u00f3ch element\u00f3w: koloru i jasno\u015bci diody RGB, do w\u0119z\u0142a <strong>nodeNumber<\/strong>. <\/p>\n\n\n\n<p>deklaracja struktury<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\nstruct package \/\/ paczka danych wychodz\u0105cych: kolor i jasno\u015b\u0107\n{\n  uint32_t c = 0xFF3333;\n  uint16_t b = 100;\n};\ntypedef struct package Package;\nPackage sendData;\n...\n<\/pre><\/div>\n\n\n<p>funkcja<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\nRF24SendData(to_node, sendData);\n<\/pre><\/div>\n\n\n<p>deklaracja funkcji<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/\/ funkcja wysy\u0142a struktur\u0119 do konkretnego w\u0119z\u0142a nRF24\nvoid RF24SendData(uint16_t nodeNumber, package dataValue)\n{\n  RF24NetworkHeader header(nodeNumber);     \/\/ (adres w\u0119z\u0142a do kt\u00f3rego wysy\u0142amy dane)\n  bool ok = network.write(header, &amp;amp;dataValue, sizeof(dataValue)); \/\/ wysy\u0142anie danych\n  if (ok) {\n    Serial.println(&quot;Posz\u0142o!&quot;);\n    Serial.println(dataValue.c, HEX);\n    Serial.println(dataValue.b);\n  }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Struktura, przyk\u0142ad dodawania zmiennych typu string, funkcja strcpy<\/h2>\n\n\n\n<p>Je\u015bli w strukturze mamy tablic\u0119 typu char dodanie lub zamian\u0119 elementu mo\u017cna wykona\u0107 u\u017cywaj\u0105c funkcji <strong>strcpy<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/\/ Arduino Uno\n\nstruct buttons\n{\n  int color&#x5B;3];\n  int positions&#x5B;4];\n  char stringi&#x5B;20];\n  void (*fn)();\n};\n\n\/\/ Declare the functions here, or use prototyping\nvoid func1();\nvoid func2();\n\nbuttons test&#x5B;] =\n{\n  { {0, 0, 0},    {0, 0, 100, 100}, &quot;Oryginalny tekst&quot;,  func1 },\n  { {40, 40, 40}, {50, 50, 10, 10}, &quot;Drugi tekst&quot;, func2 },\n};\n\nvoid setup()\n{\n  Serial.begin(115200);\n  Serial.println();\n  Serial.println(__FILE__);\n  Serial.println(__DATE__);\n  printf(&quot;\\n\\tNazwa pliku: %s\\n\\tData kompilacji: %s\\n&quot;, __FILE__, __DATE__);\n  Serial.println(&quot;Calling the functions:&quot;);\n  Serial.println( test&#x5B;0].stringi);\n  test&#x5B;0].fn();\n  test&#x5B;1].fn();\n}\n\nvoid loop()\n{\n}\n\nvoid func1()\n{\n\n  Serial.println(&quot;Wykonywana func1&quot;);\n  strcpy(test&#x5B;0].stringi, &quot;Skopiowany tekst&quot;);\n\n}\n\nvoid func2()\n{\n  Serial.println(&quot;Wykonywana func2&quot;);\n  Serial.println( test&#x5B;0].stringi);\n}\n<\/pre><\/div>\n\n\n<h5 class=\"wp-block-heading\">LINKI<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/arduino.stackexchange.com\/questions\/35516\/help-with-struct-variable\">Help with struct variable<\/a>  \/\/ \u015bwietny przyk\u0142ad u\u017cycia skomplikowanej struktury<\/li><li><a href=\"https:\/\/github.com\/nRF24\/RF24Network\">RF24Network library<\/a><\/li><li><a href=\"https:\/\/tmrh20.github.io\/RF24\/\">Newly Optimized RF24Network Layer<\/a><\/li><li><a href=\"https:\/\/howtomechatronics.com\/tutorials\/arduino\/how-to-build-an-arduino-wireless-network-with-multiple-nrf24l01-modules\/\">How To Build an Arduino Wireless Network with Multiple NRF24L01 Modules<\/a><\/li><li><a href=\"https:\/\/www.norwegiancreations.com\/2017\/10\/getting-started-with-programming-part-8-typedef-and-structs\/\">Getting Started with Programming \u2013 Part 8: Typedef and Structs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Po\u017cyteczna funkcja, kt\u00f3ra wysy\u0142a dane, w tym przypadku struktur\u0119 package sk\u0142adaj\u0105c\u0105 si\u0119 z dw\u00f3ch element\u00f3w: koloru i jasno\u015bci diody RGB, do w\u0119z\u0142a nodeNumber. deklaracja struktury funkcja deklaracja funkcji Struktura, przyk\u0142ad&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[72,120],"class_list":["post-3063","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-nrf24l01","tag-string"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=3063"}],"version-history":[{"count":11,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3063\/revisions"}],"predecessor-version":[{"id":4199,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3063\/revisions\/4199"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=3063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/categories?post=3063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/tags?post=3063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}