I2C 2 Boards -> Hängt ständig
hallo zusammen.
ich habe mir mal zusammen kopiert um zwischen nem due und nem esp befehle per i2c hinunher zu schaufeln.
aber leider hängt sich entweder der due oder der esp ab und bei einer i2c anfrage auf.
könnt ihr mir da mal weiterhelfen?
esp(master)
due(slave)
könnt ihr mir sagen ich falsch mache?
ich habe mir mal zusammen kopiert um zwischen nem due und nem esp befehle per i2c hinunher zu schaufeln.
aber leider hängt sich entweder der due oder der esp ab und bei einer i2c anfrage auf.
könnt ihr mir da mal weiterhelfen?
esp(master)
code: [select]
#include <esp8266wifi.h>
#include <wire.h> //declare i2c library function
#define i2caddressespwifi 6
const char* ssid = "blub";
const char* password = "blub";
string answer = ""; // initialize answer;
string humidity = "";
string temp1 = "";
string temp2 = "";
string ldr = "";
string uhrzeit = "";
int ledpin = 12; // gpio12 of esp8266
wifiserver server(80);
string sendmessage(string msg)
{
answer = "";
//serial.println(msg);
if (msg.length() > 32)
{
msg = msg.substring(0, 32);
}
char buf[33]; // should not send more 32-characters
unsigned int len = msg.length() + 1; // 1 null-terminator
msg.tochararray(buf, len); // convert string buf
wire.begintransmission(i2caddressespwifi); // begin transmission slave device 6
//serial.println(buf);
//serial.println(len);
wire.write(buf, len); // send character string
wire.endtransmission(); // end transmission slave
char c;
delay(800); // delay 1000 milliseconds = 1 second
wire.requestfrom(i2caddressespwifi, 15); // request 15 bytes slave 6
while (wire.available()) // when data received slave master
{
c = wire.read(); // receive 1 byte slave , assign variable c
delaymicroseconds(500); // delay, otherwise won't work
// serial.println(c);
answer += c;
}
if (answer == "012345678901234") return "err";
return answer;
}
void setup() {
serial.begin(115200);
delay(10);
pinmode(ledpin, output);
digitalwrite(ledpin, low);
// connect wifi network
serial.println();
serial.println();
serial.print("connecting ");
serial.println(ssid);
wifi.begin(ssid, password);
while (wifi.status() != wl_connected) {
delay(500);
serial.print(".");
}
serial.println("");
serial.println("wifi connected");
wire.begin(0, 2);
wire.setclockstretchlimit(1500); // in µs
// start server
server.begin();
serial.println("server started");
// print ip address
serial.print("use url connect: ");
serial.print("http://");
serial.print(wifi.localip());
serial.println("/");
}
void loop() {
// check if client has connected
wificlient client = server.available();
if (!client) {
return;
}
// wait until client sends data
serial.println("new client");
while (!client.available()) {
delay(1);
}
// read first line of request
string request = client.readstringuntil('\r');
serial.println(request);
client.flush();
// match request
int value = low;
int sensoren = low;
if (request.indexof("/led=on") != -1) {
digitalwrite(ledpin, high);
answer = sendmessage("/w<1>");
value = high;
}
if (request.indexof("/led=off") != -1) {
digitalwrite(ledpin, low);
answer = sendmessage("/w<0>");
value = low;
}
if (request.indexof("/sensoren") != -1) {
humidity = "";
temp1 = "";
temp2 = "";
ldr = "";
uhrzeit = "";
humidity = sendmessage("/r<1>");
temp1 = sendmessage("/r<2>");
temp2 = sendmessage("/r<3>");
ldr = sendmessage("/r<4>");
uhrzeit = sendmessage("/r<5>");
sensoren = high;
}
// set ledpin according request
//digitalwrite(ledpin, value);
// return response
client.println("http/1.1 200 ok");
client.println("content-type: text/html");
client.println(""); // not forget one
client.println("<!doctype html>");
client.println("<html>");
client.print("led pin now: ");
if (value == high) {
client.print("on");
} else {
client.print("off");
}
if (sensoren == high) {
client.println("<br><br>");
client.println("humidity: ");
client.println(humidity);
client.println("<br>");
client.println("temp1: ");
client.println(temp1);
client.println("<br>");
client.println("temp2: ");
client.println(temp2);
client.println("<br>");
client.println("ldr: ");
client.println(ldr);
client.println("<br>");
client.println("uhrzeit: ");
client.println(uhrzeit);
}
client.println("<br><br>");
client.println("click <a href=\"/led=on\">here</a> turn led on pin 2 on<br>");
client.println("click <a href=\"/led=off\">here</a> turn led on pin 2 off<br>");
client.println("click <a href=\"/sensoren\">sensoren</a> turn led on pin 2 off<br>");
client.println("</html>");
delay(1);
serial.println("client disonnected");
serial.println("");
}
due(slave)
code: [select]
#include <wire.h>
#define i2caddressespwifi 6
#define numberofbytes 15
char charbuffer[] = "012345678901234";
unsigned long counter = 0;
float dhttemp = 25.20;
float dhthum = 55;
float dstemp0 = 30.10;
char buftime[10] = "15:25:59";
void setup() {
serial.begin(9600);
wire1.begin(i2caddressespwifi); // join i2cbus slave identified address
wire1.onreceive(receiveevent); // register function called when slave receive transmission master//
wire1.onrequest(requestevent); // register function when master request data slave device//
}
void loop() {
counter++;
if (counter > 4294967294)
{
counter = 0;
}
}
// when slave receives string master, trigger event.
void receiveevent(int howmany)
{
serial.println("start auslesen");
string message = "";
while ( wire1.available() ) // execute repeatedly , read bytes in data packet master//
{
char c = wire1.read(); // receive data master , assign char c
serial.println(c);
message += c;
}
serial.println("ende auslesen");
string answer = "";
answer = getstrparamvalue(message);
pushmessagetocharbuffer("ok" + getstringfixlengthfromunsignedlong(numberofbytes - 2, answer), charbuffer);
}
// trigger event when master requests data slave,
void requestevent()
{
// important! code needs executed quickly, not write other code in here!
// buffer containing message (answer) must have fixed length of 15 because esp expects so...
wire1.write(charbuffer);
}
// function read value of parameter
string getstrparamvalue(string s)
{
serial.println(s);
if (s.startswith("/r", 0))
{
int indexstart = s.indexof('<');
int indexend = s.indexof('>');
if (indexstart > 0 , indexend > indexstart)
{
int = s.substring(indexstart + 1 , indexend).toint();
if (i == 2) return (string)dhttemp;
if (i == 1) return (string)dhthum;
if (i == 3) return (string)dstemp0;
if (i == 4) return (string)analogread(a2);
if (i == 5) return (string)buftime;
} else {
serial.println("i2c daten fehlerhaft");
}
} else if (s.startswith("/w", 0)) {
int indexstart = s.indexof('<');
int indexend = s.indexof('>');
if (indexstart > 0 , indexend > indexstart)
{
int = s.substring(indexstart + 1 , indexend).toint();
if (i == 1) serial.println("set 1");
if (i == 0) serial.println("set 0");
} else {
serial.println("i2c daten fehlerhaft");
}
} else {
serial.println("i2c daten fehlerhaft");
}
}
// convert message char-buffer
void pushmessagetocharbuffer(string s, char *chararray)
{
unsigned int len = s.length() + 1; // 1 null-terminator
s.tochararray(chararray, len);
}
// fill string hashes in front of value
string getstringfixlengthfromunsignedlong(int stringsize, string value)
{
string s = string(value);
string f = "";
int spacers = stringsize - s.length();
(int = 0; < spacers; i++)
{
f += "#";
}
return (f + s).substring(0, stringsize);
}
könnt ihr mir sagen ich falsch mache?
ich würde das mal ohne strings probieren, vielleicht müllen die den speicher voll bis nichts mehr geht.
ansonsten jeden i2c aufruf auf fehler prüfen.
ansonsten jeden i2c aufruf auf fehler prüfen.
Arduino Forum > International > Deutsch (Moderator: uwefed) > I2C 2 Boards -> Hängt ständig
arduino
Comments
Post a Comment