Hello guys,
i have a little problem with my Projekt.
I'm using an Attiny85 and an ESP8266-01. The ESP get a code from my browser and send data to the Attiny which switch the LED on or off. If it gets the 150 at the end of the code it should request the status of the LED from the Attiny and send it to my browser.
The Problem is that the Attiny can receive data and switch the LED but it does not send data back to the ESP.
The answer i get is every time "GPIO is now changed 255". I dont understand why i get 255 and not the status of the LED (1 or 0).
Code for the ESP
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
const char* ssid = "Wifi";
const char* password = "passwd";
byte LEDstatus; //Status of the LED
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
volatile int wDogCount = 0;
Ticker wDog;
int i2cSlave = 0x27; //Attiny85
void wDogISR(){
wDogCount++;
if (wDogCount == 3) {
ESP.reset();
}
}
void setup() {
Wire.begin(0,2);
wDog.attach(1, wDogISR);
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(IPAddress(192,168,100,150), IPAddress(192,168,100,1), IPAddress(255,255,255,0), IPAddress(192,168,100,1));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
wDogCount = 0;
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("/favicon.ico") != -1) {return;}
if (req.indexOf("/000040970659-0") != -1){ //Turn of the LED
Wire.beginTransmission(i2cSlave);
Wire.write(0);
Wire.endTransmission();
}else if (req.indexOf("/000040970659-150") != -1) { //request the status of the LED
Wire.requestFrom(i2cSlave, 1);
while(Wire.available()) {
LEDstatus = Wire.read();
Serial.println(LEDstatus);
}
}else if (req.indexOf("/000040970659-1") != -1) { //Turn on the LED
Wire.beginTransmission(i2cSlave);
Wire.write(1);
Wire.endTransmission();
}else if (req.indexOf("/000040970659-2") != -1) { //Switch the LED on if its off /or off if its on
Wire.beginTransmission(i2cSlave);
Wire.write(2);
Wire.endTransmission();
}else {
Serial.println("invalid request");
client.stop();
return;
}
client.flush();
// Prepare the response
String ans = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now changed";
ans += "</html>\n";
// Send the response to the client
client.print(ans);
client.print(LEDstatus);
delay(1);
Serial.println("Client disonnected");
Serial.println(LEDstatus);
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
The Attiny Code
#include <TinyWireS.h>
boolean status = false;
byte order; //Code from ESP
void setup() {
pinMode(3, OUTPUT);
TinyWireS.begin(0x27);
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(requestEvent);
}
void receiveEvent(uint8_t howMany) {
byte order = TinyWireS.receive();
if (howMany < 1) {
return;
}else if (howMany > 16) {
return;
}else if(order == 0) { //Turn off
digitalWrite(3, 0);
status = false;
}else if(order == 1) { //turn on
digitalWrite(3, 1);
status = true;
}else if(order == 2) {
if(status) { //change
digitalWrite(3, 0);
status = false;
}else {
digitalWrite(3, 1);
status = true;
}
}
}
void loop() {
TinyWireS_stop_check();
}
void requestEvent() {
byte stat =0;
if(digitalRead(3) == HIGH) {
stat = 1;
}else {
stat = 0;
}
TinyWireS.send(stat);
}