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.

13
  • 1
    you must have a common ground
    – jsotola
    Jan 17 at 4:59
  • 2
    I've mentioned in the Edit, that I've connected G with GND of the STM32, didn't work
    – Jackob2001
    Jan 17 at 5:05
  • 1
    Is D4 high - to confirm you initialized ok?
    – Nick Gammon
    Jan 17 at 5:47
  • 1
    Yup D4 is high.
    – Jackob2001
    Jan 17 at 5:49
  • 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 at 6:59

3 Answers 3

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

2
  • Even though the wire library has this "onRequest" the ESP8266 cannot use it ?
    – Jackob2001
    Jan 17 at 6:50
  • 4
    the code for slave is there. it just doesn't work.
    – Juraj
    Jan 17 at 7:57
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.

3
  • 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 at 5: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 at 5: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 at 5: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.

6
  • 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 at 7: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 at 8: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 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 at 11:47
  • 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 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.