Hi guys,
I a beginner and i have to transmite ultrasonic data from one place data to another place.
I have two UART LoRa module and Oled display(on receiver part) .
Need help please.
Thanks.
There is my TX and RX code
Tx:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); //--> RX, TX
#define TRIGPIN 7
#define ECHOPIN 6
#define LED 3
//définir la vitesse du son en cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_LITER 76
// Floats to calculate distance0
float duration, distance,liter,pourcent;
int data_length;
String data;
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
pinMode(TRIGPIN, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHOPIN, INPUT); // Sets the echoPin as an Input
pinMode(LED, OUTPUT);
}
void loop() {
// desactiver la broche trigpin pour 2 microseconde
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
// activer la broche trigpin pour 20 microseconde pour envoyer une impulsion
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(20);
// re-desactiver la broche trigpin
digitalWrite(TRIGPIN, LOW);
// Mesure de largeur de l'impulsion entrante
duration = pulseIn(ECHOPIN, HIGH);
// Déterminer la distance de la durée
// Utiliser 343 mètres par seconde comme vitesse de l'ultrason
distance = (duration / 2) * 0.0343;
// Convertion en litre
liter = -((distance-309)/0.01315068493) ;
//en pourcentage
pourcent=0.00456621005*liter;
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("distance (cm): ");
Serial.println(distance);
Serial.print("liter(L): ");
Serial.println(liter);
Serial.print("pourcent (%): ");
Serial.println(pourcent);
//----mamindra data
data = String(distance) + "%" + String(liter) + "%" + String(pourcent);
data_length = data.length();
String mymessage;
mymessage = mymessage + "AT+SEND=0" + "," + data_length + "," + data + "\r\n";
mySerial.print(mymessage); //--> Send Data
Serial.print("Send Data : ");
Serial.print(mymessage);
//-----
digitalWrite(LED, LOW);
// Wait a few seconds between measurements.
delay(5000);
}
Rx:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
#define SCREEN_WIDTH 128 //--> OLED display width, in pixels
#define SCREEN_HEIGHT 64 //--> OLED display height, in pixels
//----------------------------------------
//----------------------------------------Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial mySerial(10,11); //--> RX, TX
#define LED 3 //--> LED Pin
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
//----------------------------------------SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
// Address 0x3C for 128x32 and Address 0x3D for 128x64.
// But on my 128x64 module the 0x3D address doesn't work. What works is the 0x3C address.
// So please try which address works on your module.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); //--> Don't proceed, loop forever
}
//----------------------------------------Show initial display buffer contents on the screen
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000);
//----------------------------------------
mySerial.begin(9600);
pinMode(LED, OUTPUT);
delay(100);
display.clearDisplay();
display.setTextSize(1); //--> Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE);//--> Draw white text
display.setCursor(15,30); //--> display.setCursor(x,y)
display.print("Loading,please wait...");
display.display();
}
void loop() {
if (mySerial.available() > 0 ) {
String inString = mySerial.readString();
digitalWrite(LED, HIGH);
Serial.print("Receive Data : ");
Serial.print(inString);
String DataIn = getValue(inString, ',', 2); //--> data
display.clearDisplay();
display.setCursor(29,5); //--> display.setCursor(x,y)
display.print("Niveau d'eau");
display.drawLine(28, 16, 88, 16, SSD1306_WHITE);
String DataResult;
DataResult = getValue(DataIn, '%', 0);//--> Get distance data
display.setCursor(0,26); //--> display.setCursor(x,y)
display.print("distance : ");
display.print(DataResult);
display.print(" ");
display.print("cm");
DataResult = getValue(DataIn, '%', 1); //--> Get LITER data
display.setCursor(0,39); //--> display.setCursor(x,y)
display.print("liter : ");
display.print(DataResult);
display.print(" ");
display.print("L");
DataResult = getValue(DataIn, '%', 2); //--> Get pourcent data
display.setCursor(0,52); //--> display.setCursor(x,y)
display.print("pourcentage : ");
display.print(DataResult);
display.print(" ");
display.print("%");
display.display();
digitalWrite(LED, LOW);
}
//----------------------------------------
}
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}