esp32 lora RYLR890 – exp1 servo and button
simple send joystick to servo , battery , buttons
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
#include <HardwareSerial.h> HardwareSerial LoRaSerial(1); // Using UART 1 on ESP32 int messageCount = 0; // Initialize a message counter // Joystick and button pin definitions unsigned char jX = 34; // Analog pin for X-axis unsigned char jY = 35; // Analog pin for Y-axis unsigned char batV = 25; // Analog pin for Y-axis unsigned char btm1 = 32; // Digital pin for button 1 unsigned char btm2 = 33; // Digital pin for button 2 unsigned char btm3 = 25; // Digital pin for button 3 void setup() { Serial.begin(115200); // Start the Serial Monitor at 115200 baud while (!Serial); // Wait for the serial port to connect LoRaSerial.begin(115200, SERIAL_8N1, 16, 17); // Initialize LoRaSerial with RX=16, TX=17 analogReadResolution(8); // Initialize joystick and button pins pinMode(btm1, INPUT_PULLUP); pinMode(btm2, INPUT_PULLUP); pinMode(btm3, INPUT_PULLUP); // Initialize LoRa module sendCommand("AT+RESET"); // Reset the module sendCommand("AT+ADDRESS=1"); // Set device address sendCommand("AT+NETWORKID=4"); // Set network ID sendCommand("AT+BAND=868500000"); // Set frequency to 868.5 MHz sendCommand("AT+PARAMETER=7,8,1,7"); // Set parameters: Spreading factor, Bandwidth, Coding rate, Preamble length sendCommand("AT+CRFOP=15"); // Set power level sendCommand("AT+VER"); // Get firmware version sendCommand("AT+MODEL"); // Get model information sendCommand("AT+ADDRESS?"); // Get current device address sendCommand("AT+BAND?"); // Get current frequency band sendCommand("AT+NETWORKID?"); // Get current network ID Serial.println("Module information retrieval completed."); } void loop() { // Read joystick values (8-bit resolution) int jXValue = analogRead(jX); // Scale to 8-bit (0-255) int jYValue = analogRead(jY); // Scale to 8-bit (0-255) int batteryValue= analogRead(batV); // Read button states (0 = pressed, 1 = not pressed) int btm1State = digitalRead(btm1); int btm2State = digitalRead(btm2); int btm3State = digitalRead(btm3); // Prepare message with joystick and button data String message = "[" + String(jXValue) + ":" + String(jYValue) + ":" + String(batteryValue) + ":" + String(btm1State == LOW ? 1 : 0) + "" + String(btm2State == LOW ? 1 : 0) + "" + String(btm3State == LOW ? 1 : 0) + ":" + String(messageCount) + "]"; // Construct the full AT command to send the message String command = "AT+SEND=2," + String(message.length()) + "," + message; // Send the message sendCommand(command.c_str()); // Convert String object to C-string messageCount++; // Increment the message counter after sending delay(100); // Delay before the next loop iteration } void sendCommand(const char* command) { Serial.print("Sending Command: "); Serial.println(command); LoRaSerial.println(command); delay(400); // Delay to allow for command processing readLoRaResponse(); } void readLoRaResponse() { while (LoRaSerial.available()) { String response = LoRaSerial.readStringUntil('\n'); Serial.println(response); // Print the response to the Serial Monitor } } |
recive -lora – to change joystic and button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
#include <HardwareSerial.h> #include <ESP32Servo.h> // Include ESP32Servo library Servo myServo; // Create a Servo object HardwareSerial LoRaSerial(1); // Using UART 1 on ESP32 int position = 90; // Initial servo position (between 0 and 180 void setup() { myServo.attach(25); // Attach the servo to pin 25 on the ESP32 myServo.write(position); // Set the initial position of the servo Serial.begin(115200); // Start the Serial Monitor at 115200 baud while (!Serial); // Wait for the serial port to connect LoRaSerial.begin(115200, SERIAL_8N1, 16, 17); // Initialize LoRaSerial with RX=16, TX=17 analogReadResolution(8); // Initialize RYLR890 sendCommand("AT+RESET"); // Set device address delay(2500); sendCommand("AT+ADDRESS=2"); // Set receiver device address delay(2500); sendCommand("AT+NETWORKID=4"); // Set network ID delay(2500); sendCommand("AT+BAND=868500000"); // Set frequency to 868.5 MHz (adjust as needed) sendCommand("AT+PARAMETER=7,8,1,7"); sendCommand("AT+PARAMETER?"); // Set network ID delay(2500); // Check if any additional settings are specific to RYLR890 //sendCommand("AT+PARAMETER=7,125,1,8"); // Spreading factor, bandwidth, coding rate, preamble length (if supported) sendCommand("AT+VER"); // Get firmware version sendCommand("AT+MODEL"); // Get model information (if supported) sendCommand("AT+ADDRESS?"); // Get current device address sendCommand("AT+BAND?"); // Get current frequency band sendCommand("AT+NETWORKID?"); // Get current network ID Serial.println("Module information retrieval completed."); Serial.println("RYLR890 LoRa receiver..."); } void loop() { // Check if '[' and ']' exist in the string int startIndex; int endIndex; while (LoRaSerial.available()) { String receivedData = LoRaSerial.readStringUntil('\n'); if (receivedData.length() > 0) { Serial.print("Received Data: "); Serial.print(receivedData); if (receivedData.startsWith("+RCV=")) { // Remove the "+RCV=" prefix and split the string by commas receivedData = receivedData.substring(5); // Remove "+RCV=" String parts[5]; // Array to hold the split parts int partIndex = 0; while (receivedData.length() > 0 && partIndex < 5) { int commaIndex = receivedData.indexOf(','); if (commaIndex != -1) { parts[partIndex++] = receivedData.substring(0, commaIndex); receivedData = receivedData.substring(commaIndex + 1); } else { parts[partIndex++] = receivedData; break; } } // Print the array parts for real-time monitoring /* for (int i = 0; i < partIndex; i++) { Serial.print("Part "); Serial.print(i); Serial.print(": "); Serial.println(parts[i]); } */ // Check if '[' and ']' exist in the string startIndex =parts[2].indexOf('['); endIndex =parts[2].indexOf(']'); if (startIndex != -1 && endIndex != -1 && endIndex > startIndex) { // Extract the substring between the brackets String data =parts[2].substring(startIndex + 1, endIndex); // Split the string into an array by ':' String values[6]; int index = 0; int lastIndex = 0; // Serial.println("<" + data +">"); while (lastIndex < data.length()) { int colonIndex = data.indexOf(':', lastIndex); if (colonIndex == -1) { colonIndex = data.length(); // Set to the end of the string for the last element } values[index++] = data.substring(lastIndex, colonIndex); lastIndex = colonIndex + 1; } // Print the array to verify for (int i = 0; i < index; i++) { Serial.println(values[i]); } int y = values[0].toInt(); // Convert string to int // Check the value of y and adjust the position if (y >= 125) { position += 5; if (position > 180) { position = 180; // Ensure the position does not exceed 180 } myServo.write(position); // Update servo position Serial.println("Servo position increased to: " + String(position)); } else if (y < 110) { position -= 5; if (position < 0) { position = 0; // Ensure the position does not go below 0 } myServo.write(position); // Update servo position Serial.println("Servo position decreased to: " + String(position)); } } // Optional: Process specific parts, such as RSSI or message content // Serial.print("Message Content: "); // Serial.println(parts[2]); // Adjust index as needed for specific parts Serial.print("-->"+parts[2]); Serial.print(" RSSI: "); Serial.print(parts[3]); // RSSI value Serial.print( " SNR: "); Serial.println(parts[4]); // RSSI value } else { // Print other non-RCV responses for monitoring Serial.println(""); } } } } void sendCommand(const char* command) { Serial.print("Sending Command: "); Serial.println(command); LoRaSerial.println(command); delay(100); // Delay to allow for command processing readLoRaResponse(); } void readLoRaResponse() { while (LoRaSerial.available()) { String response = LoRaSerial.readStringUntil('\n'); Serial.println(response); // Print the response to the Serial Monitor } } |