ESP8266 to ESP8266 i2C Communication - arduino

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

Related

Communication between Arduino Nano and HM-10 BLE controller is not working

I want to check if communication is working between my SerialMonitor in Arduino IDE and BLE controller.
I typed command AT to my SerialMonitor and it suppose to return OK response but nothing happened.
This is scheme what I used:
Code:
#include <SoftwareSerial.h>
SoftwareSerial bleSerial(2, 3); // RX, TX
void setup() {
//initialize serial port for logs
Serial.begin(9600);
while (!Serial) {
}
bleSerial.begin(9600);
}
void loop() {
if (bleSerial.available()) {
Serial.write(bleSerial.read());
}
if (Serial.available()) {
bleSerial.write(Serial.read());
}
}
UPDATE:
Changed values for SoftwareSerial bleSerial(3, 2); // RX, TX still doesnt work.
UPDATE2:
I've tried switching pins and code, nothing works. I should at least see HM-10 controller in my bluetooth devices on my Android phone, but I cant see anything.
UPDATE3:
I've used code from this Stackoverflow post and its working fine. I can finally see controller in my bluetooth devices on my Android phone also It returned name MLT-BT05 after AT+NAME? command. Looks like you have to read message per char and put delay 10ms between chars, otherwise it will not be possible to read message from BLE controller. That was the only problem.
You should connect RX-TX and TX-RX (not RX-RX and TX-TX like your graphic shows) so change the cables and the code from
SoftwareSerial bleSerial(2, 3); // RX, TX
to
SoftwareSerial bleSerial(3, 2); // RX, TX
Connect according to this graphic (incl voltage divider)
Abd use the following sketch to test (read comments for details):
// SerialIn_SerialOut_HM-10_01
//
// Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
// What ever is entered in the serial monitor is sent to the connected device
// Anything received from the connected device is copied to the serial monitor
// Does not send line endings to the HM-10
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// Arduino D8 (SS RX) - BT TX no need voltage divider
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//
#include <SoftwareSerial.h>
SoftwareSerial BTserial;
char c=' ';
bool NL = true;
void setup()
{
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
if (c!=10 & c!=13 )
{
BTserial.write(c);
}
// Echo the user input to the main window. The ">" character indicates the user entered text.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c==10) { NL = true; }
}
}

Android Things to Arduino using I2C - error 6: No such device or address

I try to connect an NXP i.MX7D running Android Things to a Arduino UNO using I2C. The slave code is fairly simple :
#include <Wire.h>
void setup() {
Wire.begin(0x08); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
The two devices are connected like this:
NPX SDA Pin ---> 3.3v level converter 5v ----> Arduino PIN A4
NPX SCL Pin ---> 3.3v level converter 5v ----> Arduino PIN A5
When I use the PIO tool I can't seem to connect or read the Arduino slave. There is two BUS (I2C1, I2C2) on the NPX I tried both with the same result:
imx7d_pico:/ $ pio i2c I2C1 0x08 read-reg-byte 0x00
[WARNING:client_errors.cc(35)] error 6: No such device or address
6|imx7d_pico:/ $ pio i2c I2C2 0x08 read-reg-byte 0x00
[WARNING:client_errors.cc(35)] error 6: No such device or address
I assume the level converter work properly I did have limited success with UART connection (Arduino was able to emit to NPX but not NPX to Arduino, will investigate this after)
Here are some pictures of the connection.
Try to add on Wire.onReceive callback(together with onRequest) to your arduino code.
Like this:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // 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) {
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
}

Unable to do Arduino Mega to Arduino Mega serial communication

Based on the circuit below, I tried hooking up two Arduino Mega for serial communication.
The code for sender:
char mystr[3] = "Hello"; //String data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.write(mystr, 5); //Write the serial data
delay(1000);
}
The code for receiver:
char mystr[5]; //Initialized variable to store received data
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}
void loop() {
Serial.readBytes(mystr, 5); //Read the serial data and store in var
delay(1000);
}
There is no output in the Serial console of Arduino. Could someone please inform me of the possible cause and solution for the same. If I've missed out anything, over- or under-emphasized a specific point let me know in the comments.
If I understood this right you have one Arduino connected to your pc and to another Arduino?
The problem is that you need to specify which Serial port to use:
That is rather easy, just type Serial1 or Serial2 instead of just Serial. That allows you to open 2 Serial ports: One to your other Arduino and one to your Computer for Displaying the results !
LINK: https://www.arduino.cc/en/Tutorial/MultiSerialMega
You need to check available data from serial:
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
Serial.readBytes(mystr, 5);
Serial.print("I received: ");
Serial.println(mystr, DEC);
}
}

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.

I2C Stops Transmitting After Exactly 7 Requests with Arduino

I am using an ATTiny85 running the TinyWire library to communicate with an Arduino Uno running the Wire library, from an I2C connection. I can transmit one byte at a time perfectly fine for as many requests as I want, however a problem arises when I try and send two bytes at a time. Below is the code I am using (note - I am using a popular modified version of the TinyWire library which has the OnRequest method implemented). Here's my code for the slave:
#include "TinyWireS.h" // wrapper class for I2C slave routines
#define I2C_SLAVE_ADDR 0x27 // i2c slave address
void setup()
{
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
TinyWireS.onRequest(requestEvent);
}
void loop()
{
}
void requestEvent()
{
int16_t bigNum = analogRead(3);
byte myArray[2];
myArray[0] = (bigNum >> 8) & 0xFF;
myArray[1] = bigNum & 0xFF;
TinyWireS.send(myArray[0]);
TinyWireS.send(myArray[1]);
}
and for the master:
#include <Wire.h>
#define DEVICE_2 0x27
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
delay(2000);
int16_t bigNum;
byte a,b;
// read 2 bytes, from address 0x27
Serial.println("Request Start");
Wire.requestFrom(DEVICE_2, 2);
a = Wire.read();
b = Wire.read();
bigNum = a;
bigNum = bigNum << 8 | b;
Serial.print(bigNum);
Serial.print("\n");
}
Regardless of the delay time, I can always only get exactly 7 requests. I've tried many values from no delay to 5 second delays in between calls. If I power off the ATTiny, then supply power again while the Uno is still connected to the Serial port, I can get 7 more requests before stopping again. The Serial monitor always shows that the Uno's main loop somehow gets paused directly after calling requestFrom(), which makes it look to me like it's waiting for something, but I can't figure out what. When I unplug the ATTiny, the Uno prints to the Serial port -28412. I have also tried putting the following before reading from the buffer:
if(Wire.available() > 0) {
a = Wire.read();
b = Wire.read();
}
Thanks for your help.
According to this Issue, you can only send one byte from the onRequest callback function. It is called from the ISR, so it really shouldn't do very much. He suggests remembering which byte has been sent, then send the next one when another request event happens. See this example.

Resources