در این آموزش، من به شما نشان خواهم داد که چگونه با استفاده از ماژول HC-12، که از 433،4-473 مگاهرتز کار می کند، به ارتباط بی سیم متغیرها بین 2 آردوینو بین 2 آردوینو برسید.
![](https://duino4projects.com/wp-content/uploads/2022/07/Wireless-Communication-of-Multiple-Variables-Between-2-Arduinos-Using-HC-12.jpg)
HC-12 ماژولهای بیسیم برای ارتباط آردوینو به آردوینو هستند، زیرا میتوانند چندین متغیر را در مسافتهای طولانی ارسال کنند - تا 1 کیلومتر - تنها با استفاده از 2 پین دیجیتال، نه حتی پایههای PWM، که بسیار بهتر از پینآت NRF24 است و آنها را ایدهآل میکند. برای پروژه های بی سیم
من نتوانستم هیچ آموزشی در آنجا پیدا کنم که این را نشان دهد، بنابراین تصمیم گرفتم آن را بسازم.
الهامبخش من برای کد این پروژه توسط RootSaid است ، اما من آن را به گونهای تغییر دادهام که درک آن آسان و ادغام در هر پروژهای باشد.
تدارکات
-2 ماژول HC 12
-2 برد آردوینو به انتخاب شما
-سیم ها
-هر چیز دیگری که می خواهید به پروژه بی سیم خود اضافه کنید
مرحله 1: این کدها را درک کرده و در آردوینو خود آپلود کنید
این هم کد فرستنده:
#include <SoftwareSerial.h> #include <Wire.h> SoftwareSerial HC12(12, 13);//tx,rx int variable1;//variables that will get sent int variable2; int variable3; int variable4; int variable5; void setup() { HC12.begin(9600);//Sets the hc 12 communication to 9600 baud, which is their factory default Serial.begin(9600); pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT); pinMode(A4, INPUT); } void loop() { variable1 = analogRead(A0);//assigns the variables that will be sent the value read on the analog pins variable2 = analogRead(A1);//you can change this to other data, such as a digital pin's read variable3 = analogRead(A2); variable4 = analogRead(A3); variable5 = analogRead(A4); //Uncomment the following lines if you want the remote to also send the analog input data to the serial monitor, to check if the data is being read properly //If you do, It will look the same as it should on the other end's serial monitor //Serial.print(variable1); //Serial.print(","); //Serial.print(variable2); //Serial.print(","); //Serial.print(variable3); //Serial.print(","); //Serial.print(variable4); //Serial.print(","); //Serial.print(variable5); //Serial.println(""); HC12.print(variable1);//sends the variables HC12.print(","); HC12.print(variable2); HC12.print(","); HC12.print(variable3);//if you just need to send 2 variables,simply change this value and the following to 0 HC12.print(","); HC12.print(variable4);//if you just need to send 3 variables,simply change this value and the next to 0 HC12.print(","); HC12.print(variable5);//if you just need to send 4 variables,simply change this value to 0 //if you need 5 or less variables, disregard comments below, but keep the next line of code, it is essential HC12.println("");//you can add even numbers of variables, ex add 2, 4, 6 ...(so 3, 5, 7, 9... variables in total) before this line by adding a variable line, then a comma line, another variable line then a second comma line under the 5th variable line but before the println. //for the purpose of simplicity, it is easier to add in groups of 2, so that less modification is necessary on the other end //if you have more variables than you are using, change the variable to 0, as above explained, again for the purpose of simplicity // This change must be reflected on receiver end: see below delay(100); }
در اینجا کد مربوط به گیرنده است:
#include <SoftwareSerial.h> #include <Wire.h> SoftwareSerial HC12(12, 13);//tx,rx int variable1=0; int variable2=0; int variable3=0; int variable4=0; int variable5; String input; int boundLow; int boundHigh; const char delimiter = ','; void setup() { Serial.begin(9600); HC12.begin(9600); } void loop() { if(HC12.available()) { input = HC12.readStringUntil('\n'); if (input.length() > 0) { Serial.println(input); boundLow = input.indexOf(delimiter); variable1 = input.substring(0, boundLow).toInt(); boundHigh = input.indexOf(delimiter, boundLow+1); variable2 = input.substring(boundLow+1, boundHigh).toInt(); boundLow = input.indexOf(delimiter);//you can add 2 variables, or 4... by adding these 4 lines variable3 = input.substring(0, boundLow).toInt();//you can add 2 variables, or 4... by adding these 4 lines boundHigh = input.indexOf(delimiter, boundLow+1);//you can add 2 variables, or 4... by adding these 4 lines variable4 = input.substring(boundLow+1, boundHigh).toInt();// under this line variable5 = input.substring(boundHigh+1).toInt();//but before this one //if you change the number of variables, follow instructions in comments above //duplicating the lines as explained adds 2 variables, then you can duplicate them again, and then change the variable names so they are in the right order delay(10); }//after this you can choose what to do with each variable, which will now have the same value as it's value on the transmitter end } }
اساساً روش کار به این صورت است که فرستنده اول متغیرها را به ترتیب با کاما در بین آنها ارسال می کند و یک رشته تشکیل می دهد و گیرنده ترتیب را می داند (چون شما آن را با ترتیب برنامه ریزی می کنید) و رشته را جدا می کند. مقادیر را به متغیرهای مربوطه آنها اختصاص دهید.
بنابراین، اولین عدد/متغیر ارسال شده، اولین عدد در رشته ای خواهد بود که دریافت می شود، و دومین عدد، دومین…
مرحله 2: کدها را مطابق میل خود تغییر دهید تا با پروژه شما مطابقت داشته باشد
اکنون که هر دو آردوینو در حال ارتباط هستند، کدها را تغییر دهید تا گیرنده بتواند از متغیرها برای کنترل چراغها، موتورها و موارد دیگر، مخصوص پروژه شما استفاده کند.