I am trying to connect my MSP432 to my HC-05 Bluetooth module however when I connect to the module using a Bluetooth terminal, I receive no feedback. I've attached my code as well. Is the RX pin P3.2 and TX P3.3?
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
pinMode(12, OUTPUT);
}
void loop() {
if (Serial.available()) /* If data is available on serial port */
{
char data_received;
data_received = Serial.read(); /* Data received from bluetooth */
if (data_received == '1')
{
digitalWrite(12, HIGH);
Serial.write("LED turned ON");
}
else if (data_received == '2')
{
digitalWrite(12, LOW);
Serial.write("LED turned OFF");
}
else
{
Serial.write("Select either 1 or 2");
}
}
}
Related
I'm trying make an mqtt co2 sensor for home assistant. And I got it to work, but only for a couple of hours. At some point it starts giving me -1 values instead of the proper co2 value. What could this be?
Below my code:
#include <Arduino.h>
#include <Mhz19.h>
#include <SoftwareSerial.h>
SoftwareSerial softwareSerial(D3, D4);
Mhz19 sensor;
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "ID";
const char* password = "pw";
const char* MQTT_BROKER = "192.168.1.25";//"test.mosquitto.org";
int mqttPort = 1883;
const char* mqttUser ="mqtt-user";
const char* mqttPassword ="pw";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// Connect to Wi-Fi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("Connected to Wi-Fi");
}
void reconnect() {
// Loop until connected
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("Test1", mqttUser, mqttPassword )) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed MQTT connection with state: ");
Serial.println(client.state());
// Re-try in 3 seconds
delay(3000);
}
}
// Once connected, publish an announcement, and subscribe
client.publish("sensor/test", "Hello from ...");
client.subscribe("sensor/test");
}
void setup() {
Serial.begin(115200);
setup_wifi(); // Connect to Wi-Fi network
client.setServer(MQTT_BROKER, mqttPort);
//client.setCallback(callback);
reconnect(); // Connect to MQTT broker
softwareSerial.begin(9600);
sensor.begin(&softwareSerial);
sensor.setMeasuringRange(Mhz19MeasuringRange::Ppm_5000);
sensor.enableAutoBaseCalibration();
Serial.println("Preheating..."); // Preheating, 3 minutes
while (!sensor.isReady()) {
delay(50);
}
Serial.println("Sensor Ready...");
}
void loop() {
if (!client.connected()) { reconnect(); }
//client.publish("Time", ctime(&now));
auto carbonDioxide = sensor.getCarbonDioxide();
if (carbonDioxide >= 0) {
Serial.println(String(carbonDioxide) + " ppm");
}
char co2String[8];
dtostrf(carbonDioxide, 1,0, co2String);
//Serial.println(co2String);
client.publish("Floating/climate/CO2", co2String);
delay(2000);
}
I also tried flashing it via ESPHome, but then I run into issues from UART. The chip doesn't work propperly as long as the sensor is connected to it.
The sensor may not be receiving enough current. Try connecting the sensor directly to a separate breadboard power supply. The sensor seems to have a warm up time. Try waiting for longer with the independent power supply connected. I hope this will solve your issue.
Thank You,
Naveen PS
I have a serial barcode scanner and Arduino Uno.
I need to configuration..one Correct barcode scanning led on.
my code is
void setup ()
{
Serial.begin (9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if(Serial.available())
{
String command =Serial.readStringUntil('\n');
Serial.println(command);
if(command =="1010007")
{
digitalWrite(3, HIGH);
digitalWrite(4,LOW);}
else
if(command !="1010007")
{digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(3,LOW);}
}}
im scanning barcode 1010007 it showing serial monitor g⸮⸮⸮⸮ like this
how to fix it
plz help
and i try another code it showing correctly barcode,,,,,i scan 1010007 serial monitor also same 1010007
but led not on...code is below,,,
#include <SoftwareSerial.h>
SoftwareSerial barcode = SoftwareSerial(2, 3, true); // RX, TX
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
barcode.begin(9600);
delay(1000);
}
void loop()
{
if(barcode.available())
{
char data = barcode.read();
Serial.write(data);
if(data=="101007")
{
digitalWrite(3, HIGH);
}
}}
I'm trying to save my received SMS into a string but it doesn't work. This is my error message :
recive_0_1:50:28: error: invalid conversion from 'int' to 'char' [-fpermissive] exit status 1 invalid conversion from 'int' to 'char'
[-fpermissive] This report would have more information with "Show
verbose output during compilation" option enabled in File ->
Preferences.**
#include <SoftwareSerial.h>
#include <string.h>
char message[160]; // max size of an SMS
char* text;
String data;
//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(4, 5); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
mySerial.begin(9600);
Serial.println("Initializing...");
delay(1000);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
mySerial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
}
void updateSerial()
{
// char c;
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
//char data=Serial.read();
//Serial.println(data,DEC);
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
text=mySerial.read();
Serial.print(text);
//}
}
}
It looks like you're running into problems from trying to convert integers to characters. I use the below code to read from my SIM card to my SIM900 device:
1st: my software serial is defined and my setup script run:
SoftwareSerial gprsSerial(7, 8);
void setup()
{
pinMode(13, OUTPUT);
gprsSerial.begin(19200);
Serial.begin(19200);
delay(200);
// set up your AT COMMANDS HERE - I HAVE NOT INCLUDED THESE
}
Then I run my loop function as follows, calling the additional functions specified thereafter, :
void loop() {
readSMS();
delay(2000);
}
void toSerial(){
delay(200);
if(gprsSerial.available()>0){
textMessage = gprsSerial.readString();
delay(100);
Serial.print(textMessage);
}
}
void readSMS() {
textMessage = "";
gprsSerial.print("AT+CSQ\r");
toSerial();
gprsSerial.print("AT+CMGF=1\r");
toSerial();
gprsSerial.println("AT+CNMI=2,2,0,0,0\r");
delay(2000);
toSerial();
}
With the above you should not get any data type conversion hassles.
I'm a new bie on arduino projects. I would like to ask you for some help. I bought a BLE Shield for Arduino from ( http://imall.iteadstudio.com/development-platform/arduino/shields/im130704001.html ). They made this shield using Hm-10 Bluetooth module(http://www.jnhuamao.cn/bluetooth.asp?ID=1). Itead Studio has no sample codes using this shield. I have no idea on how to program it or send AT commands from Arduino.
I read the “AT commands” at the data sheet (ftp://imall.iteadstudio.com/Shield/IM130704001_ITEAD_BLE_Shield/DS_IM130704001_ITEAD_BLE_Shield.pdf) and I tried to send "AT commands” from arduino to BLE shield using this code ( http://arduino.cc/en/Reference/SoftwareSerial ) but I only received the commands back.
Did anybody here ever use this HM-10 bluetooth module ?
I need some arduino sketch for help !
#include <SoftwareSerial.h>
int led = 13;
int bluetoothTx = 2;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
int baudrate[8] ={4800,9600,14400,19200,28800,38400,57600,115200};
int i = 1;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
while(!Serial){}
Serial.write("AT sent");
delay(500);
bluetooth.write("AT+NAME?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
bluetooth.write("AT+POWE3");
delay(500);
while(bluetooth.available())
{
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+CHAR?");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
delay(100);
Serial.println("");
delay(500);
bluetooth.write("AT+NAMEFlightline"); //Check Status
delay(500);
while (bluetooth.available()) {
Serial.write((char)bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+CHAR0x2901"); //add charicteristic
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+RELI0");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
bluetooth.write("AT+SHOW1");
delay(100);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
}
Serial.println("");
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
}
void testAllBaudRates(){
for(int j=0; j<8; j++)
{
bluetooth.begin(baudrate[j]);
delay(100);
Serial.println("boud rate " + String(baudrate[j],DEC) +" i-> "+String(j,DEC));
// Serial.println("");
bluetooth.write("AT");
delay(500);
while (bluetooth.available()) {
Serial.write(bluetooth.read());
Serial.println();
}
delay(100);
}
}
// and now a few blinks of the LED,
// so that we know the program is running
void loop()
{
//Read from bluetooth and write to usb serial
while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend == 'x'){
digitalWrite(led,HIGH);
Serial.println("set high");
bluetooth.write("RXOK");
}else if(toSend == 'y'){
digitalWrite(led,LOW);
Serial.println("set low");
bluetooth.write("RXOK");
}
Serial.print(toSend);
}
//Read from usb serial to bluetooth
while(Serial.available())
{
char toSend = (char)Serial.read();
bluetooth.write(toSend);
Serial.print(toSend);
}
}
Have a look at my sketch above I have a few things to point out that I wasted time on.
make sure you have the line
while(!Serial){}
Or you may get have a working shield but miss the responses as the serial monitor is no ready.
remember that you wont get a response from the blue-tooth module, with a command from the Serial Monitor if it is connected to a device. It is connected to the device when the light stops flashing.
if you run this sketch you should get this output
AT sent
OK+Set:3
OK+Get:0x2901 <- this may be blank the first time you run it
OK+Set:Flightline
OK+Set:0x2901
OK+Set:0
OK+Set:1
the most comprehensive list of AT commands can be found here
[All the AT commands and a good explanation][1]
You will need to at Characteristics to the device as I have done here
bluetooth.write("AT+CHAR?");
or you will find it to connect to iOS and Android
If you are connecting to Android use the BluetoothLE Classes not Bluetooth ones.
You can use this sketch with baud rate autodetect to control your HM-10. This is a part of Apploader project that allows to upload to Arduino board over BLE.
This is a little late too, but try the following code, if you send it "AT" it should give you back an "OK":
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.
#include <SoftwareSerial.h>
#include <String.h>
char inchar[255];
SoftwareSerial cell(2,3);
int led1 = 22;
#define powerOn 4
int i;
//char comparestring[160];
char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command ended with null terminator
void setup()
{
// ilagay sa loob ng setup
digitalWrite(powerOn, HIGH);
delay(1500);
digitalWrite(powerOn, LOW);
delay(5000);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.begin(9600);
cell.b-egin(9600);
delay(30000);
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("GSM SHIELD IS NOW OK AND READY");
}
void loop()
{
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
{
digitalWrite(led1, HIGH);
cell.write("AT+CMGS=\"");
cell.write("09267955775");
cell.write("\"\r");
delay(1000);
cell.write("\nTerminal Monitoring System");
delay(1000);
cell.write(0x1A); // End the SMS with a control-z
}
else
{
Serial.println("\nInvalid Keyword! Type ?");
dig-italWrite(led1, LOW);
}
}
}
these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
inchar is a string so try using strcmp(inchar,command) for comparing the two.