5

I've got a huge problem using an ESP8266 from a Wemos D1 mini.

I am using an STM32L073RZ to send a request to an ESP8266 to send me the time it has acquired from a WiFi connection.

ESP code:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <Wire.h>

// Zastąp poniższe wartości danymi Twojej sieci WiFi const char* ssid = ":>>>>>>>>>>>"; const char* password = "NOPE";

#define Status D4

WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);

void setup() { pinMode(D4, OUTPUT); // Pin odpowiedzialny za gotowość urządzenia do komunikacji przez I2C digitalWrite(D4, LOW); // Ustawiamy go na początku na "0".

Serial.begin(9600); WiFi.begin(ssid, password);

while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); }

timeClient.begin();

Wire.begin(8); // Odpala D2 jako SDA i D1 jako SCL jako slave Wire.onRequest(sendTime); // Rejestracja funkcji zwrotnej, która zostanie wywołana, gdy urządzenie nadrzędne zażąda danych

digitalWrite(D4, HIGH); // Tutaj konfiguracja z siecią jak i też konfiguracja z pinami i ustawienie I2C jest zakończone // i sygnalizowane stanem wysokim na D4 }

void loop() { timeClient.update(); Serial.println(timeClient.getFormattedTime()); delay(1000); }

void sendTime() { String time = timeClient.getFormattedTime(); Wire.write(time.c_str()); // Wysłanie czasu do urządzenia nadrzędnego będzie to razem 8 bajtów bo jeszcze znaki oddzielające. }

STM simple code :

xD = HAL_I2C_Master_Receive(&hi2c2, 0x08<<1, czas, 8, 1000);

I tried everything. Adding pull ups, changing the GPIO speeds, etc.

Here is what my logic analyser gave me:

Logic analyser output

Still nothing. I've also checked which pins are the SDA and SCL which are D2 and D1. I've checked is the connection was correct and it should be, but still no response.

I don't know anymore what is wrong or what is not.

Schematic :

Schematic

It is already powered so I didn't need to add 5 V and G pins to it, but I also tested with adding G pin to ground of the STM32, and also with 5 V to the 5 V of the STM32, but it still didn't work.

ocrdu
  • 1,775
  • 3
  • 11
  • 24
Jackob2001
  • 53
  • 5
  • 1
    you must have a common ground – jsotola Jan 17 '24 at 04:59
  • 2
    I've mentioned in the Edit, that I've connected G with GND of the STM32, didn't work – Jackob2001 Jan 17 '24 at 05:05
  • 1
    Is D4 high - to confirm you initialized ok? – Nick Gammon Jan 17 '24 at 05:47
  • 1
    Yup D4 is high. – Jackob2001 Jan 17 '24 at 05:49
  • What are the voltage levels? The STM32L073RZ seems to be a 3.3V device, and the ESP8266 also, however you mentioned 5V. I'm no expert on either, but that could be an issue. – Nick Gammon Jan 17 '24 at 05:50
  • Oh I have mentioned that I only connected 5V STM32 to 5V ESP8266 a lot later after so many attempts to connect with I2C. But welp still sitting 5h fixing it. – Jackob2001 Jan 17 '24 at 05:52
  • But welp still sitting 5h fixing it. - I don't understand that. – Nick Gammon Jan 17 '24 at 05:54
  • nothing just saying that it is exhausting. But I've burned now the WiFi connected it accidently GND from STM32 to 5V ESP8266. But still I don't understand why my I2C didn't work – Jackob2001 Jan 17 '24 at 05:58
  • Are you powering both devices from 3.3V or 5V? – Nick Gammon Jan 17 '24 at 06:11
  • Both of them I power using simple USB port. Which is as I know 5V and is lowered to 3.3V. – Jackob2001 Jan 17 '24 at 06:15
  • From this: https://github.com/esp8266/Arduino/issues/5762 it appears that the esp8266 is not a good I2C slave device. – 6v6gt Jan 17 '24 at 06:22
  • Hmmmm interesting. I thought it was an old post so maybe they've fixed it. I saw some tutorials that they have used the I2C on ESP8266 so I thought maybe it works. – Jackob2001 Jan 17 '24 at 06:37
  • 2
    @Jakob2001 The esp8266 works as a master and, if you are lucky, works as a slave but only with another esp8266 as master. You could try reversing the roles. The esp8266 as master sending a time stamp occasionally to your STM device as an I2C slave. – 6v6gt Jan 17 '24 at 06:59

3 Answers3

5

I2C slave doesn't work on esp8266 because it doesn't have hardware support for I2C.

From the esp8266 Wire library documentation:

Wire library currently supports master mode ...

https://arduino-esp8266.readthedocs.io/en/3.1.2/libraries.html#i2c-wire-library

Juraj
  • 18,037
  • 4
  • 29
  • 49
2

The devices must share a common ground, and also you may need 4.7k pull-up resistors on both SDA and SCL.

See http://gammon.com.au/i2c for a whole lot more detail.

Nick Gammon
  • 38,184
  • 13
  • 65
  • 124
  • 1
    I've mentioned in the post that I've added pull ups in STM32 using software and in the edit I've mentioned I have connected G pin with GND pin of the STM32, still didn't work. – Jackob2001 Jan 17 '24 at 05:06
  • 1
    I was looking at your circuit diagram. You can't expect to post incorrect information and get a good answer. – Nick Gammon Jan 17 '24 at 05:25
  • Okey, my bad. But still the solution to add pull ups didn't work as well as connecting G pin to GND pin of stm32. – Jackob2001 Jan 17 '24 at 05:26
0

Both other answers are correct, but there is yet another problem with your design.

As can be clearly seen from the logic analyzer output, the master runs its clock at 100kb/sec (the standard i2c rate). That leaves the slave ca. 5us to react. For a 80 MHz chip this might be quite a tight timing by itself and requires thorough inspection and optimization of code. But a UDP request into the big net? Seriously?!

The only way it might work is if both parties could agree on a clock stretching. However, the i2c spec never defines precise timings for the stretched clock, so, the chances are, any master would probably break the session assuming the slave hung up long before the UDP request could be served.

Therefore, such requests need to be split into two parts. First, the master asks the slave to initiate the process of measurement/exchange/computation/whatever. And second, after an appropriate delay, the master issues another request for the buffered result from slave.

psmears
  • 103
  • 2
Matt
  • 137
  • 3
  • I am not that good at speeds etc. So 80 MHz and 100kb/s doesn't say a lot to me if context of if it makes in time etc. I just wanted to get time, so I checked some tutorials how to do it. I've checked the I2C frequency speed and it was 100 kHz.

    Let's say I don't know how to imagine yet these speeds and timings because 5 us itself is so small for me. Sorry for my incompetence. Although I would like to learn something how to judge whether the clock is enough or not, or it must be always the same or it can be a bit different.

    – Jackob2001 Jan 17 '24 at 07:48
  • @Jackob2001 The timings are written on the chart. 100khz is the smallest i2c speed by spec and by h/w implementations. 100000 cycles per second gives 10/1000000 sec per whole cycle and 5/1000000 for one half of it (when clock is low). Pretty trivial. – Matt Jan 17 '24 at 08:10
  • Sorry for pinging. h/w ? 10/100000 ? I just wondered how timing works here though because as you said there are two now. One is with 100kHz and the other one is with 80Mhz. Shouldn't the timing be the same for them to work ? If one of the clock is faster than the other then after one cycle the same bit can change mamy time ??? I don't know. Just saying after reading your comment about timing I want to learn more though – Jackob2001 Jan 17 '24 at 11:14
  • @Jackob2001 10/1000000 = 1/100000. Divide by two and get 5/1000000. If mcu runs at 80 mhz then it has ca. 400 internal clocks per 5 us (1/2 of i2c clock). This is at most 400 machine instructions even for RISC arch. – Matt Jan 17 '24 at 11:47
  • @Jackob2001 You probably need to start with master mode. Don't use libraries but implement it in software. This is pretty easy as master runs the scl clock the way it wants. Then you'll understand how i2c works completely. – Matt Jan 17 '24 at 11:56
  • 1
    I still don't quite get the calculations. Maybe this is not the best topic, but with timing things I don't quite get what you've tried to say. And implement it in software ? Hmmm I am quite new just happened to learn some stuff so I don't know how to get to it. WiFi code was made from yt videos :D – Jackob2001 Jan 17 '24 at 16:35