Transmitting data between arduino's using hm-10 BLE - arduino

I am working on a project whereby I have an arduino recording the humidity and temperature levels of a room (using a DHT11 sensor), and a second arduino that recieve this data via bluetooth.
I am using the hm-10 BLE modules.
So far the data-collector can transmit data over BLE, and the reciever can recieve BLE data from my phone, but I can't figure out how to pair the two modules so that the receiver can receive data from the data-collector.
All of the solutions I have found online involve using the AT instruction set, whereas I am using the SoftwareSerial.h library.
The code for my data gatherer is as follows:
//Include the DHT (humidity and temperature sensor) library, and the serial library
#include <dht.h>
#include <SoftwareSerial.h>
//Define the constants for the input (DHT11) and output pins
#define RXpin 7
#define TXpin 8
#define DHT11_PIN 9
//Initialise a Serial channel as softSerial
SoftwareSerial softSerial(RXpin, TXpin);
//Initialise DHT object
dht DHT;
//Set initial measurement to be temperature (not humidity)
bool humidity = false;
void setup() {
//Start the serial function
Serial.begin(9600);
//Start the softSerial channel
softSerial.begin(9600);
}//void setup()
void loop() {
//Reset the reading variable
float(reading);
//Take in the values recorded by the DHT11
int chk = DHT.read11(DHT11_PIN);
//Store the necessary measurement in the reading variable
if (!humidity) {
reading = DHT.temperature;
} else {
reading = DHT.humidity;
}
//Output the reading on the softSerial channel
softSerial.print(reading);
//The DHT11 can only take one measurement per second, so waiting two seconds ensures there will be no null readings
delay(2000);
//Swap current measurement
humidity = !humidity;
}//void loop()
Any ideas on how I can connect it with another hm10 module so they can exchange information without having to re-write everything to the AT instruction set would be greatly appreciated.

You need to learn AT commands to configure the HM-10 in master or slave mode, all these can still be done using the SoftSerial library, get the HM-10 datasheet for AT commands and check these site for help.
http://www.instructables.com/id/How-to-Use-Bluetooth-40-HM10/
https://www.hackster.io/achindra/bluetooth-le-using-cc-41a-hm-10-clone-d8708e
See sample code below:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4, 5);
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
BTSerial.write("AT+DEFAULT\r\n");
BTSerial.write("AT+RESET\r\n");
BTSerial.write("AT+NAME=Controller\r\n");
BTSerial.write("AT+ROLE1\r\n");
BTSerial.write("AT+TYPE1"); //Simple pairing
}
void loop()
{
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}

Related

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.

How to use SPI with ESP32 and Arduino

I'm trying to send out data from SPI, but can't get it to work. There appear no data on the SPI ports (D12, 13, 14; checked with an oscilloscope) and the ESP32 seems to hang. I would like to use the HSPI port.
I am also wondering whether I need a special driver for SPI to work on ESP32 and if so, how can I check if I already have that and how do I install it. When I look in the library manager, I see no special SPI driver.
I have tried using this program (copied from https://diyi0t.com/spi-tutorial-for-arduino-and-esp8266/). It's apparently intended for esp8266. Should it work out of the box also for ESP32?
#include "SPI.h"
char buff[]="Hello Slave\n";
void setup() {
SPI.begin();
}
void loop() {
for(int i=0; i<sizeof buff; i++)
{
SPI.transfer(buff[i]);
}
delay(1000);
}
and also with this program:
#include "SPI.h"
char buff[]="Hello Slave\n";
SPIClass SPI1(HSPI);
void setup() {
SPI1.begin();
SPI1.setClockDivider(80);
}
void loop() {
for(int i=0; i<sizeof buff; i++)
{
SPI1.transfer(buff[i]);
}
delay(1000);
}
I am using a 30 pin ESP32 dev board, Arduino version 1.8.13. In preferences-->more board managers, it says:
http://arduino.esp8266.com/stable/package_esp8266com_index.json, https://dl.espressif.com/dl/package_esp32_index.json
For ESP32, you need to declare which SPI instance you want to use, like so:
#include <SPI.h>
SPIClass SPI1(HSPI);
SPI1.begin();
// Optional
// SPI1.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE2));
The rest is the same as ESP8266
#include <SPI.h>
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_CS 15
static const int spiClk = 240000000; // 1 MHz
SPIClass * hspi = NULL;
char buff[]="Hello Slave\n";
//byte buff[] = {0xAA, 0xBB, 0xAA, 0x01,
0x89, 0xAB, 0xCD, 0xEF};
void setup() {
Serial.begin(9600);
hspi = new SPIClass(HSPI);
hspi->begin();
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, SS
pinMode(HSPI_CS, OUTPUT); //HSPI SS
}
void loop() {
for(int i=0; i<sizeof buff; i++)
{
SPI.transfer(buff[i]);
Serial.println(buff[i]);
}
delay(1000);
}
Assuming that you use the ESP32 Arduino Core, under the docs it is written that SPI is has a suppported Arduino API implementation. Thus using the Arduino SPI API, it should work, like all other devices (the ESP32 Arduino Core implementation conforms to the API defined by Arduino, of course I would check if your board's pinout corresponds to the Espressif defined ESP32 pinout).
If you want to see some examples, you can find one here. All supported APIs have examples on the ESP32 Arduino Core repo on GitHub.
I also want to point out that in the Arduino IDE (or the VSCode plugin) you can find examples for SPI, I would take a look as well for that.

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();

ESP8266 to ESP8266 i2C Communication

I am trying to get my ESP8266's connect and send messages over an i2c bus. I am using a NodeMcu Development Board. Pins D1,D2 and GND are connected to each other.
The code on my master is :
#include <Wire.h>
void setup() {
Wire.begin(D1,D2); // join i2c bus (address optional for master)
Serial.begin(115200);
}
byte x = 0;
void loop() {
Wire.beginTransmission(8);
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
Serial.println("Transmitted");
x++;
delay(500);
}
And the code on my slave ESP is:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
Serial.println("Received..");
/*
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
*/
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
Running this gives no output on the receiver chip.
As mentioned in the comments it doesn't look like I2C is supported, but you could use PJON
You just need to connect a single wire to enable communication between the two devices
I'm not sure but I would expect the Wire library from Arduino to use the hardware I2C controller for ATMega. The I2C driver in the firmware from Espressif seems to be doing I2C over GPIO, that would hint there is no hw controller on ESP (what are the odds they would be the same anyway). So you need to use something else than Wire.h, thus I would suggest - try downloading something that fakes I2C over GPIO for your Arduino IDE. Like this .. maybe, I haven't tried that out. I know not a complete solution, but maybe at least this helps.. good luck!
ESP8266(I2C Master) to ESP8266(I2C Slave) works from version 2.5.0. Check out my comments on the ESP8266 GitHub

PC to Arduino RS-485 connection via converters

I'm trying to make modbus RS-485 connection between PC (Windows) and Arduino.
On the PC side I use USB-to-RS485 converter like this one http://ru.aliexpress.com/item/USB-to-485-RS485-converter-Adapter-Support-Windows-7-8-for-Arduino/1577970568.html
On the Arduino side I use TTL-to-RS-485 like this one http://ru.aliexpress.com/item/5pcs-lot-MAX485-Module-RS-485-TTL-to-RS485-Module-for-Arduino/32262338107.html
Problem1:
When I send byte from PC to Arduino. Nothing happens. Arduino does not receive anything. In this case i upload to Arduino this code:
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
const byte ENABLE_PIN = 3;
void setup()
{
Serial.begin(9600);
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte buf [1];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
Serial.println(buf[0]);
} else {
Serial.println("--");
}
} // end of loop
int fAvailable ()
{
return rs485.available ();
}
int fRead ()
{
return rs485.read ();
}
And open Serial Monitor in Arduino IDE to show received data.
Then I open new instance of Arduino IDE, chose proper USB-to-RS485 COM port and opens it Serial Monitor to send some data.
So in Arduino side Serial Monitor I see only "--" lines. Even when I'm trying to send something in PC side Serial Monitor.
The other problem is:
Vice versa. When i sending some byte from Arduino to PC I receive some odd symbols instead sent byte.
The Arduino code in this case is:
#include <SoftwareSerial.h>
#include "RS485_protocol.h"
SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
const byte ENABLE_PIN = 3;
void setup()
{
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte msg[1] = {3};
sendMsg(msg);
} // end of loop
void sendMsg(byte msg[])
{
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, 1);
digitalWrite (ENABLE_PIN, LOW); // disable sending
}
void fWrite (const byte what)
{
rs485.write (what);
}
int fAvailable ()
{
return rs485.available ();
}
On the PC side (in the Arduino IDEs Serial Monitor) I receive odd symbols instead "3" symbol.
=======
RS485 converters wired with twisted pare A to A and B to B.
RS485 to TTL converter connected to Arduino properly. (Communication between two arduinos works fine).
Please help.
Example of RS485 :
Using a SN75176 IC.
RE (pin 2) connect to ground (for always reading )
Arduino control only DE PIN for writing data
But PC side problems:
Where bridged DE pins ? (CTS,DTR,RTD if supported)
What is your flow control ? (related #1)
Did you connect any resistor to A, B pin ?<+5V>----[560R]-----(A)----[120R]-----(B)------[560R]------<-5V> So mean line end
Did you filter DE Pin 2 signal (for noise)
A tricks : Use SN75176 on arduino side because this IC a RS232(UART) to (RS485) converter.
Edit : Modbus got 2 type on serial (RTU, ASCII). Dont care RS232/485 differences because different signal, same protocol.
Example packet type:
ASCII : :010300200004+CRC+FE(crc =packet check code, fe=end delimeter)
RTU : \x01\x03\x00\x20\x00\x04\x +CRC
On rtu : every byte need 1.5 char space and without start and end delimeter.
And Modbus node type meaning master and slave.
I hope helpfull.

Resources