ASCII of integer to integer in Arduino - arduino

I am working on a project with LoRa and Arduino and I am facing a weird issue where when I transmit a integer the receiver receives at ASCII value which is not good at my case because I wanted to transmit sensor data(3 digits) which is not possible by ASCII. I will also attach my code(converted into a basic integer code for testing) I need a Solution to fix this BTW I am using Arduino UNO for transmitting and Arduino Mega for receiving and SX1278 LoRa Module for both transmitting and receiving.
Transmitter Code(Arduino UNO):
#include <SPI.h>
#include <LoRa.h>
int val = 5;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
Serial.print("Sending packet: ");
// send packet
LoRa.beginPacket();
LoRa.print(val);
LoRa.endPacket();
delay(500);
}
Receiver Code(Arduino Mega):
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS 53
#define LORA_RST 9
#define LORA_DIO0 8
int val;
void setup() {
pinMode(LORA_SS, OUTPUT);
digitalWrite(LORA_SS, HIGH);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
//Serial.print((char)LoRa.read());
int val = LoRa.read();
}
Serial.print(val);
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Output of the receiver :
Received packet '53' with RSSI -5

To Fix this issue just send the value as an integer, since LoRa receives as char/string same it in a string variable in the receiver and convert it into integer with toInt(); and that will fix it!

Related

How to send orders from Arduino to Esp32 and make it keypress

I need to send orders from Arduino to ESP32.
I have one joystick button to test.
Arduino nano is sender
Esp32 is receiver
Esp32 receives the joystick button information from Arduino (each time I push the button).
I need the Esp32 to Serial.write according to the data, for example:
If I press the button in Arduino: Send the data to Esp32 and turn bluetooth on (or turn a led on).
These are my codes:
//Arduino NANO sender
byte j = 45;
#define boton_joystick A1
void setup() {
Serial.begin(9600);
pinMode(boton_joystick, INPUT_PULLUP);
}
int boton_joystick_state;
void loop() {
//Serial.println("100");
//Serial.write("BOTON EN GRANDE");
//delay(1500);
if(!digitalRead(boton_joystick)) {
boton_joystick_state = 1;
delay(170);
} else {
boton_joystick_state = 0;
}
if (boton_joystick_state) {
Serial.println(j);
Serial.print(" ");
Serial.write(j);
Serial.println();
}
//ESP-32 receiver
#define RXD2 16
#define TXD2 17
byte j = 45;
int comdata;
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
//Serial.print("LEYENDO ARDUINO");
Serial.println(Serial2.readString());
if (Serial2.available() >0) {
char comdata = char(Serial2.read());
if (comdata == 'j') {
Serial.println("joystick activado");
}
}
}
am not sure
but am using Nano 33 BLE with UART and Nano has also Serial1 no need to serial2 no need to Softwearserial. Sensd on serial 1 and recive in Serial 1 but also you have to connect it Via USB. so your serial is USB and your serial 1 is TX RX.
for me it work so you can try it.

Sending GPS values via XBee and Arduino (Float problem!!!)

I have XBEE S2C's. I have 2 Arduino MEGA. One part of them will be receiver and other is transmitter. I communicated and configured XBee's.I tested it That's okey. But the problem is I can't send float value like longtitude= 38,45682469 latitude= 85,24698534. When I try to do this I receive only latitude = 85
longtitude = 38. How can I change my code to receive healthy data.
TRANSMITTER
#include "SoftwareSerial.h"
#include <TinyGPS.h>
float lat,lon;
TinyGPS gps;
SoftwareSerial XBee(10, 11);
void setup(){
Serial.begin(9600);
XBee.begin(9600);
Serial.println("The GPS Received Signal:");
Serial3.begin(9600);
}
void loop(){
while(Serial3.available()){
if(gps.encode(Serial3.read()))
{
gps.f_get_position(&lat,&lon);
Serial.print("Position: ");
Serial.print("Latitude: ");
Serial.print(lat,8);
Serial.print(",");
Serial.print("Longitude: ");
Serial.println(lon,8);
}
}
XBee.write("l");
XBee.write(lat);
delay(100);
XBee.write("k");
XBee.write(lon);
delay(100);
}
Most robust is to convert float to string, send it over serial, and let the receiver convert back to float.
Sender:
XBee.write(String(lat,5)); // keep 5 decimals
Receiver:
String inString = "";
while (Serial.available()) {
char c = Serial.read();
inString += c;
}
float myFloat = inString.toFloat();
Further reading:
https://www.arduino.cc/en/Tutorial/StringToFloatExample
https://www.arduino.cc/en/Reference.StringConstructor

serial communication between Arduino and Nodemcu

I'm trying to read voltage value using Analog pin A0 in arduino uno and transmit the read voltage to Nodemcu but not getting same voltage at NodeMcu as on Arduino side for Ex. for 5 volt at Arduino i get only 4 volt at Nodemcu.
i have made the delay of both the sketches equal even tried without any delay
also tried connecting the ground pin of both device
ARDUINO CODE
#include <SoftwareSerial.h>
SoftwareSerial s(5,6);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int ADCdata = analogRead(A0);
float voltage = (ADCdata * 0.0048828125);
Serial.println(ADCdata);
Serial.println(voltage);
if(s.available()>0)
{
s.write(voltage);
}
delay(1000);
}
NODEMCU CODE
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
void setup() {
s.begin(9600);
Serial.begin(9600);
}
void loop() {
s.write("s");
if (s.available()>0)
{
data=s.read();
Serial.println(data);
}
delay(1000);
}
I would send the float data as a string:
s.println(value)
This will append a newline to mark the end of the string.
On the receiving side, read the line and convert to float.
float value = s.parseFloat();

Working a temprature sensor (LM35) with a GSM module (Sim800L)

I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading
void setup()
{
pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> fona.sendSMS(sendto, message)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
char sendto[21], message[141];
:
:
//initialize sendto and message
:
:
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
fonaSS.begin(9600);
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
another way to send sms
you could test the hayes command: for example
void sendsms(){
Serial.println("Sending text message...");
fonaSS.print("AT+CMGF=1\r"); // SMS MODE
delay(100);
// phone number
fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number
delay(100);
// message here
fonaSS.print("Message test \r");
// CTR+Z in mode ASCII, to indicate the end of message
fonaSS.print(char(26));
delay(100);
fonaSS.println();
Serial.println("Text send");
}
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}

Unable to receive correct data using i2c serial communication between two Arduino

I'm using an i2c serial bus for communication between two Arduino (Uno = Master, Due = Slave) and I'm currently experiencing problems while reading data received by the slave.
The master sends some data using Wire.write(command). The slave receives it and the handler function receiveEvent(int howMany) is called thanks the instruction Wire.onReceive(receiveEvent).
Here is the simplified code for the serial communication:
Master's Sketch
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(8);
byte command[] = {2, 5, 3};
Wire.write(command, 3);
Wire.endTransmission();
Serial.println("command sent...");
delay(1000);
}
Slave's Sketch
#include <Wire.h>
int c = 0;
void setup() {
Serial.begin(9600);
Wire.begin(8);
Wire.onReceive(receiveEvent);
}
void loop() {
delay(1000);
}
void receiveEvent(int howManyBytes){
for(int iter=0; iter<howMany; iter++){
c = Serial.read();
Serial.print("c : ");
Serial.println(c);
}
}
Slave's Output
c : -1
c : -1
c : -1
It appears that three bytes are received but the data are not transmitted correctly. Any idea were there could be a mistake or a bug? Thanks!
Since you expect the data from the Wire, I think your slave should receive the data via Wire.read() instead of Serial.read().

Resources