Unable to communicate: Arduino with Bluetooth HC-06 - arduino

I wrote a program that can capture moisture through a DHT11 sensor connected to an Arduino Uno. The captured value will be sent to an Android application using a Bluetooth HC-06 module. Everything works fine except that the latter (HC-06) does not send the captured moisture.
Can someone help me and thank you
The code is as follows:
#include <SoftwareSerial.h>
/* to communicate with the Bluetooth module's TXD pin */
#define BT_SERIAL_TX 10
/* to communicate with the Bluetooth module's RXD pin */
#define BT_SERIAL_RX 11
/* Initialise the software serial port */
SoftwareSerial BluetoothSerial(BT_SERIAL_RX, BT_SERIAL_TX);
// DHT-11 Configuration
#include <DHT.h> // lightweight DHT sensor library
#define DHTTYPE DHT11 // DHT 11
#define TEMPTYPE 0 // Use 0 for Celsius, 1 for Fahrenheit
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE); // Define Temp Sensor
void setup() {
BluetoothSerial.begin(115200); // Initialise BlueTooth
Serial.begin(9600);
//delay(1000);
dht.begin(); // Initialize DHT Teperature Sensor
BluetoothSerial.print("Starting ...");
}
void loop() {
// Take readings
float h = dht.readHumidity(); // Read humidity
BluetoothSerial.println(h);
delay(500);
}

Related

Thinger.IO client setup for GPRS enabled ESP32 project

I've been using the Thinger.io platform for some of my IoT projects (mostly ESP8266 modules) for quite a long time now. The way I implemented it is something similar to that:
#include <ThingerESP8266.h>
#include <ESP8266WIFI.h>
#define USERNAME "username"
#define DEVICE_ID "deviceid"
#define DEVICE_CREDENTIAL "devicecredential"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void connectToWifi() {
...
}
void setup() {
connectToWifi();
}
void loop() {
thing.handle();
}
and it just works. It is good to also mention that I've been using WiFi all the way.
Now I am trying to achieve the same by taking advantage of a controller called TTGO T-Call ESP32. It is GPRS enabled (using the TinyGsmClient.h) and I have inserted a SIM card inside of it which successfully connects to the internet. The issue is that I can not really establish a connection to the Thinger.io platform where my devices are hosted. This is what my code looks like (making a reference to this library example)
// Your GPRS credentials (leave empty, if not needed)
const char apn[] = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password
// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
#define uS_TO_S_FACTOR 1000000UL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 3600 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
#define IP5306_ADDR 0x75
#define IP5306_REG_SYS_CTL0 0x00
bool setPowerBoostKeepOn(int en){
I2CPower.beginTransmission(IP5306_ADDR);
I2CPower.write(IP5306_REG_SYS_CTL0);
if (en) {
I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
} else {
I2CPower.write(0x35); // 0x37 is default reg value
}
return I2CPower.endTransmission() == 0;
}
void connectToApn(){
SerialMon.println("Connecting to: internet.vivacom.bg ... ");
while(!modem.gprsConnect(apn, gprsUser, gprsPass))
delay(500);
SerialMon.println("Successfully connected to: internet.vivacom.bg");
}
// #include <ThingerCore32.h> => ArduinoJson.h: No such file or directory
// #include <ThingerESP8266.h> => ESP8266WiFi.h : No such file or directory
#define USERNAME ""
#define DEVICE_ID ""
#define DEVICE_CREDENTIAL ""
#include <ThingerESP32.h>
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
//#include "arduino_secrets.h"
// Server details
const char server[] = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
const int port = 80;
#include <ArduinoHttpClient.h>
HttpClient http(client, server, port);
void setup() {
// Set serial monitor debugging window baud rate to 115200
SerialMon.begin(115200);
// Start I2C communication
I2CPower.begin(I2C_SDA, I2C_SCL, 400000);
// Keep power when running from battery
bool isOk = setPowerBoostKeepOn(1);
SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
// Configure the wake up source as timer wake up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Connect to APN
connectToApn();
}
void loop() {
thing.handle();
SerialMon.println("In the loop ...");
delay(3000);
SerialMon.print(F("Performing HTTP GET request... "));
int err = http.get(resource);
if (err != 0) {
SerialMon.println(F("failed to connect"));
delay(10000);
return;
}
int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
delay(10000);
return;
}
SerialMon.println(F("Response Headers:"));
while (http.headerAvailable()) {
String headerName = http.readHeaderName();
String headerValue = http.readHeaderValue();
SerialMon.println(" " + headerName + " : " + headerValue);
}
int length = http.contentLength();
if (length >= 0) {
SerialMon.print(F("Content length is: "));
SerialMon.println(length);
}
if (http.isResponseChunked()) {
SerialMon.println(F("The response is chunked"));
}
String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);
SerialMon.print(F("Body length is: "));
SerialMon.println(body.length());
// Put ESP32 into deep sleep mode (with timer wake up)
// esp_deep_sleep_start();
}
NOTE: The board I've picked from the Arduino IDE is called ESP32 Wrover Module
It would be better if you ask this question on the thinger community, the thinger.io https://community.thinger.io/ where the thinger devs or community will be listening.
I have some working code, see below, this works with SIM7000E, but it should work OK with SIM800 the code should work the same. I have noticed that you are not using the thinger library (ThingerTinyGSM.h) and this is probably why the device isn't connecting to thinger.
#define THINGER_SERIAL_DEBUG //This will provide debug messages of what thinger
code is trying to do
#define _DISABLE_TLS_ //TLS needs to be disabled if using ESP32 (not sure why, this is a known bug)
// Select your modem:
//#define TINY_GSM_MODEM_SIM800 //Note SimCom docs state that SIM7000e used same commands as SIM800
#define TINY_GSM_MODEM_SIM7000 //Note SimCom docs state that SIM7000e used same commands as SIM800
#define APN_NAME "..."
#define APN_USER "..."
#define APN_PSWD "..."
//Pins for connecting to SIM module using 2nd Serial connection
#define RXD1 16
#define TXD1 17
#include <TinyGsmClient.h>
#include <ThingerTinyGSM.h>
//Thinger credentials
#define USERNAME "...." //Thinger Account User Name
#define DEVICE_ID "...." //Thinger device IC
#define DEVICE_CREDENTIAL "...." //Thinger device credential (password)
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, Serial2);
/*******************************
**** SET-UP **** SET-UP ****
********************************/
void setup() {
// open serial for debugging
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD1, TXD1);
delay(1000);
Serial.println(); Serial.println();
Serial.println("Starting Thinger GSM Test");
delay(1000);
// set APN, you can remove user and password from call if your apn does not require them
thing.setAPN(APN_NAME, APN_USER, APN_PSWD);
////// Thinger resource output example (i.e. reading a sensor value)
thing["Status"] >> [](pson & out) {
out["Timer(ms)"] = millis();
out["device"] = String(DEVICE_ID);
};
}
void loop() {
thing.handle();
}
This is the code which worked for me:
const char apn[] = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password
// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <Wire.h>
#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
void connectToApn(){
SerialMon.println("Connecting to: internet.vivacom.bg ... ");
while(!modem.gprsConnect(apn, gprsUser, gprsPass))
delay(500);
SerialMon.println("Successfully connected to: internet.vivacom.bg");
}
#define USERNAME ""
#define DEVICE_ID ""
#define DEVICE_CREDENTIAL ""
#include <ThingerTinyGSM.h>
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, Serial1);
void setup() {
// Set serial monitor debugging window baud rate to 115200
SerialMon.begin(115200);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
// Connect to APN
connectToApn();
}
void loop() {
thing.handle();
}

How to connect MAX30100 pulse sensor to a different i2c pins of ESP32 and read data?

I'm using a ESP32 30 pin board, MAX30100 pulse sensor for my project.
I can interface this sensor to ESP32's different i2c pins i.e. not default pins(21,22).
But I don't know how to read data from the MAX30100 if it connected to different pins - (Let's say 32, 33)
This is the program I used for default i2c pins to read data from MAX30100
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define REPORTING_PERIOD_MS 1000
char auth[] = "*******************"; // You should get Auth Token in the Blynk App.
// Connections : SCL PIN - D1 , SDA PIN - D2 , INT PIN - D0
PulseOximeter pox;
float BPM, SpO2;
uint32_t tsLastReport = 0;
void onBeatDetected()
{
Serial.println("Beat Detected!");
}
void setup()
{
Serial.begin(115200);
pinMode(19, OUTPUT);
Blynk.begin(auth,"************", "**********");
Serial.print("Initializing Pulse Oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
}
else
{
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
// The default current for the IR LED is 50mA and it could be changed by uncommenting the following line.
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
}
void loop()
{
pox.update();
Blynk.run();
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(BPM);
Serial.print(" bpm / SpO2:");
Serial.print(SpO2);
Serial.println(" %");
Blynk.virtualWrite(V3, BPM);
Blynk.virtualWrite(V4, SpO2);
tsLastReport = millis();
}
}
How do I interface MAX30100 to other pins? What should be the instructions?
PulseOximeter pox;
What does this instruction mean?
You should change direction in library:
Link to library file in github
Line 29: change that for
Wire.begin(SDA_PIN, SCL_PIN);
Where SDA_PIN and SCL_PIN are defines of your own routing.
NOTE: if you change the library you will need always to use that pins for all of your developments so maybe is better if you import that library locally or even better if you change library so if you don't pass any pin at argument it default normally I2C pins but, if defined use the defined ones.

Sending data from one arduino to another via bluetooth hc05 modules

I have to do a project for Uni using 2 arduino uno, 2 HC 05 bluetooth module and 2 sensors for each arduino. I have set a module to be the slave, and the master connects to the slave only.
When I am trying to send the data from slave to be read by master, it keeps reading 0.
I am using Arduino IDE, I have no errors and everything runs.
This is the code I have so far.
Also, I want to send data from 2 sensors from the slave to the master, but I do not know how to do that.
Slave code
#include <SoftwareSerial.h>
#define TILT 7 // tilt sensor pin
#define LDR 8 //light intensity sensor pin
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
int sentBytes; ////// SLAVE CODE
byte data[2];
void setup() {
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(TILT, INPUT);
pinMode(LDR, INPUT);
Serial.begin(9600);
nodeCommunication.begin(9600);
delay(2000);
}
void loop() {
//int LDRval = analogRead(LDR); // light intensity sensor (candela)
//int Tilt_Sensed = digitalRead(TILT);
data[0] = analogRead(LDR);
data[1] = digitalRead(TILT);
if(nodeCommunication.available()){
sentBytes = nodeCommunication.write(data[0]);
Serial.println("I reached this if");
}
Serial.println(analogRead(LDR));
Serial.println(sentBytes);
delay(2000);
}
Master Code
#include "dht.h"
#include <SoftwareSerial.h>
#define dht_apin A0 // Analog Pin sensor is connected to A0
dht DHT; // DHT.humidity // DHT.
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
byte data = 1;
///// MASTER CODE
void setup(){
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
delay(2000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT.read11(dht_apin); // reading the data from the sensor
if(nodeCommunication.available()){
data = nodeCommunication.read();
Serial.println("I reached this if");
}
Serial.print(data); Serial.println(" candela;");
Serial.print(DHT.temperature); Serial.println(" C");
Serial.print(DHT.humidity); Serial.println(" % humidity");
delay(2000);//Wait 2 seconds before accessing sensor again.
//Fastest should be once every two seconds.
}// end loop()
Please tell me what I am doing wrong. Thank you so much!
byte data = 1; - should be just byte data;
Have you checked the TX/RX connections? The TX from the master has to connect to the RX of the slave and visa versa. Als GND has to be connected to eachother.
After that, let the master (the transmitter) just Serial.write (in your case nodeCommunication.write() ) to the slave (the receiver).
So something like this:
Master:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte data = 0x01; //Hex value for ''1''
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
nodeCommunication.write(data); //Sends byte data to slave
delay(1000);
}
Slave:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte incoming
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
if(nodeCommunication.available > 0) //Only if data is available
{
byte incoming = nodeCommunication.read(); //read byte from master
Serial.println("Incoming = ");
Serial.println(incoming);
}
else
{
Serial.println("No data available....");
}
}
After your Serial communication is established, implementing the DHT value will be the next step.

ESP32: dsb1820 temperature sensor giving constant negative 127 reading

I am trying to get a temperature reading using a single dsb1820 temperature sensor attached to an esp32 micro controller. The sensor is attached to GPIO-4 of the esp32. I intend to send the temperature reading to a cloud.
The problem i am facing is that the temperature reading always gives the value -127.
I read somewhere online that when the dsb1820 returns -127 it means that the sensor is not connected.
Am I using the wrong pin to connect the sensor?
#include "OneWire.h"
#include "DallasTemperature.h"
#include <WiFi.h>
#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWORD"
OneWire oneWire(4);
DallasTemperature tempSensor(&oneWire);
void setup(void)
{
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
tempSensor.begin();
}
void loop(void)
{
tempSensor.requestTemperaturesByIndex(0);
Serial.print("Temperature: ");
Serial.print(tempSensor.getTempCByIndex(0));
Serial.println(" C");
delay(2000);
}
Check your cables and:
const int oneWireBus = 32; // on pin 32 /GPIO7/D0 on pcb (a 4.7K resistor is necessary)
OneWire oneWire(oneWireBus);
and it should be the middle pin of the sensor (see my graphic)
EDIT
The DevKit has no pin 4 either you use GPIO4 (4 on the pcb) which is in Arduino 24 BUT
The following strapping pins: 0, 2, 4, 5 (HIGH during boot), 12 (LOW
during boot) and 15 (HIGH during boot) are used to put the ESP32 into
bootloader or flashing mode. Don't connect peripherals to those pins!
If you do, you may have trouble trying to upload code, flash or reset
the board.
Connect to 32 (GPIO7 or D0 on the pcb) as this is safe for testing
If you have that wrong or no/wrong resistor it will give you -127 (or you killed the sensor/it was DOA).

How can I activate a 24V electric lock with Arduino?

Good! I have a circuit in which I connect an Arduino to an NFC Shield and it is to a protoboard. My goal is to activate an electric lock that works with 10-24V AC and DC, for this I have placed a transistor 2N2222 and a resistance of 330 Ohms. The problem that I have esque when I connect the plate protoboard to the lock, the LED is turned on, but the lock is immovable.
This is the scheme on which I have based myself to assemble everything.
And this is the image of how I have it armed:
And finally I have the code that I am using:
#include <PN532.h>
#include <SPI.h>
/*Chip select pin can be connected to D10 or D9 which is hareware optional*/
/*if you the version of NFC Shield from SeeedStudio is v2.0.*/
#define PN532_CS 10
PN532 nfc(PN532_CS);
#define NFC_DEMO_DEBUG 1
const int control = 7 ;
const int speed = 200;
void setup(void) {
pinMode(control, OUTPUT) ;}
void loop(void) {
digitalWrite(control, HIGH);
delay(2000);
digitalWrite(control, LOW);
delay(1000);
}
it doesn't happends because your supply in +5v (from USB) and can not change state of +24v equipment.
you should connect a +12v supply to the collector of your transistor.
do like this :

Resources