I2C köle olarak çalışan Uno, Mega'ya float değerler gönderiyor ve Mega'dan komut alıyor: değerler bozuk geliyor
Yapay zekâ tarafından English dilinden çevrildi ·
Yine ben. Bahçe projesi: bahçe kulübesindeki bir Uno toprak nemini, sıcaklığı ve bir su akış sayacını okuyor. Aynı kutudaki bir Mega, Ethernet-Wi-Fi köprüsünü barındırıyor ve her şeyi Hermetry’ye iletiyor. Mega’nın Uno’dan I2C üzerinden okumalarını istemesini ve bazen sulama vanasını açmasını söylemesini istiyorum (vana rölesi Uno üzerinde).Uno (köle, adres 8):
void requestEvent() {
Wire.write(String(moisture).c_str());
Wire.write(String(tempC).c_str());
Wire.write(String(flowLpm).c_str());
}
Mega:
Wire.requestFrom(8, 12);
while (Wire.available()) Serial.print((char)Wire.read());
Aldığım şey, karışık halde 4312.5 ve ÿÿÿ gibi değerler ve bazen yalnızca ilk değer. Vana kısmına henüz hiç başlamadım; köle cihazın bir komutu nasıl aldığını bilmiyorum.
Me again. Garden project: an Uno out in the shed reads soil moisture, temperature and a water flow meter. A Mega in the same box has the Ethernet-to-Wi-Fi bridge and sends everything on to Hermetry. I want the Mega to ask the Uno for its readings over I2C, and sometimes tell it to open the irrigation valve (the valve relay is on the Uno).
Uno (slave, address 8):
void requestEvent() {
Wire.write(String(moisture).c_str());
Wire.write(String(tempC).c_str());
Wire.write(String(flowLpm).c_str());
}
Mega:
Wire.requestFrom(8, 12);
while (Wire.available()) Serial.print((char)Wire.read());
What I get is things like 4312.5 and ÿÿÿ mixed together, and sometimes only the first value. For the valve I haven’t even started, I don’t know how the slave gets a command.
Yapay zekâ tarafından English dilinden çevrildi ·
Devamla, iki kartta tamamen aynı bozuk ÿÿÿ sorunu yaşıyorum.
Following, I have the exact same garbled ÿÿÿ thing with two boards.
Yapay zekâ tarafından English dilinden çevrildi ·
SDA ve SCL hatlarında pull-up dirençlerine ihtiyacın olabilir mi? I2C’nin bunlara ihtiyaç duyduğunu okumuştum.
Maybe you need pull-up resistors on SDA and SCL? I read I2C needs them.
Yapay zekâ tarafından English dilinden çevrildi ·
Her iki kart da 5 V ve Wire kütüphanesi AVR’nin dahili pull-up dirençlerini açıyor, dolayısıyla aynı kutudaki kısa kablolarda sorun bu değil. Asıl sorun requestEvent içindeki üç Wire.write çağrısı. AVR’de istek başına yalnızca bir yazma güvenilir çalışıyor ve uzunluğu bilinmeyen metin dizileri durumu daha da kötüleştiriyor. Değerleri tek bir tampona koyup bir kez yazın. Ayrıca işleyicilerin içinde Serial ile yazdırma yapmayın.
Both boards are 5 V and the Wire library turns on the AVR’s internal pull-ups, so on short wires in one box that’s not it. The bigger problem is three Wire.write calls in requestEvent. Only one write per request works reliably on AVR, and text strings of unknown length make it worse. Put the values in one buffer and write it once. And no Serial printing inside the handlers.
Yapay zekâ tarafından English dilinden çevrildi ·
Carlos’un dediği gibi, hat sayaçlarımız için kullandığım sürüm şu (sayımı bir Uno yapıyor, ağ işini daha büyük bir kart yapıyor). Tek bir struct içinde, sabit boyutlu ikili veri gönderin ve komutlar için onReceive kullanın.Her iki eskizde de ortak struct:
struct Reading {
int16_t moisture; // raw ADC
int16_t temp_x10; // 21.5 C -> 215
int16_t flow_x10; // L/min x10
};
Uno (köle):
volatile Reading r;
volatile bool openValve = false;
void requestEvent() { Wire.write((const uint8_t*)&r, sizeof(r)); } // one write
void receiveEvent(int n) { if (Wire.available()) openValve = Wire.read() == 1; }
void setup() { Wire.begin(8); Wire.onRequest(requestEvent); Wire.onReceive(receiveEvent); }
void loop() {
Reading tmp = { readMoisture(), (int16_t)(readTemp() * 10), (int16_t)(readFlow() * 10) };
noInterrupts(); r = tmp; interrupts();
digitalWrite(VALVE_PIN, openValve);
}
Mega (master):
Reading r;
if (Wire.requestFrom(8, (int)sizeof(r)) == sizeof(r)) Wire.readBytes((uint8_t*)&r, sizeof(r));
float tempC = r.temp_x10 / 10.0;
Wire.beginTransmission(8); Wire.write(1); Wire.endTransmission(); // open valve
Her ikisi de AVR olduğu için bayt sırası ve int boyutları eşleşiyor, struct olduğu gibi karşıya gidiyor. Gördüğünüz ÿ 0xFF’dir; köle gönderecek veri kalmadığında master’ın okuduğu değerdir. sizeof(r) kadar veri istemek bunu da çözer.
What Carlos said, here’s the version I use for our line counters (an Uno doing the counting, a bigger board doing the network). Send binary, fixed size, in one struct, and use onReceive for commands.
Shared struct, in both sketches:
struct Reading {
int16_t moisture; // raw ADC
int16_t temp_x10; // 21.5 C -> 215
int16_t flow_x10; // L/min x10
};
Uno (slave):
volatile Reading r;
volatile bool openValve = false;
void requestEvent() { Wire.write((const uint8_t*)&r, sizeof(r)); } // one write
void receiveEvent(int n) { if (Wire.available()) openValve = Wire.read() == 1; }
void setup() { Wire.begin(8); Wire.onRequest(requestEvent); Wire.onReceive(receiveEvent); }
void loop() {
Reading tmp = { readMoisture(), (int16_t)(readTemp() * 10), (int16_t)(readFlow() * 10) };
noInterrupts(); r = tmp; interrupts();
digitalWrite(VALVE_PIN, openValve);
}
Mega (master):
Reading r;
if (Wire.requestFrom(8, (int)sizeof(r)) == sizeof(r)) Wire.readBytes((uint8_t*)&r, sizeof(r));
float tempC = r.temp_x10 / 10.0;
Wire.beginTransmission(8); Wire.write(1); Wire.endTransmission(); // open valve
Both are AVR, so byte order and int sizes match and the struct goes across as-is. The ÿ you saw is 0xFF, what the master reads when the slave has nothing left to send. Asking for exactly sizeof(r) fixes that too.
Yapay zekâ tarafından English dilinden çevrildi ·
İlk denemede çalışıyor, bu benim başıma hiç gelmez. Okumalar temiz ve Mega 1 gönderdiğinde vana açılıyor. I2C’nin “Serial gibi” olduğunu düşündüğüm için metin gönderiyordum. Bu arada Hermetry payload’ımı da düzelttim: sıcaklık artık tek ondalıklı bir sayı olarak gidiyor.
Works first time, which never happens to me. Readings are clean and the valve opens when the Mega sends 1. I’d been sending text because I thought I2C was “like Serial”. Also fixed my Hermetry payload while I was at it: temperature now goes out as a number with one decimal.
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Sohbete katılın
Soru sormak ve yanıtlamak, yararlı gönderileri beğenmek ve sorununuzu çözen yanıtı işaretlemek için oturum açın. Her katkı puan ve seviye kazandırır.

