Go Down

Topic: Arduino Uno with serial ESP8266 connecting to Homebridge (Read 502 times) previous topic - next topic

szeitz

Hello everyone!
I am having some problems, wondering if anyone here could help.
I spent the last 5 days (around 30 hours) to set up Homebridge on Linux and get my Arduino to properly work with it.

I've got as far as having 2 LEDs connected. I can turn them on with Siri or the Home app in IOS10 and even get the status updated. So good so far.

BUT, my problem is if I turn or lets say the RED LED, it turns on (status updates OK), then I go to turn on my GREEN LED then it turns on, but the RED LED turns off as well.

My coding is not the nicest, and I am still just just learning Arduino,

but if enyone could help I'd really appreciate it!


My code is below:
Code: [Select]

/* Arduino UNO based homebridge accessories
 It is very basic coding

Has 2 leds connected one to PIN 9 and one to PIN 10
ESP8266 -05 modul is communicating on HW serial port

Was having some problems with homebridge so some unnecesary delays might be in place

*/


#include "WiFiEsp.h" //I found this library to be working with my module
#include <ESP8266_TCP.h>

// ESP8266 Class
ESP8266_TCP wifi;

// Target Access Point
const char* ssid  = "XXX XXX"; //Input your Wifi's SSID here
const char* pass  = "XXX XXX"; // Input your Wifi's password here

const int redPin = 9; // RED LED connected to PIN 9
const int greenPin = 10; // GREEN LED connected to PIN 10

String readString;
WiFiEspServer server(80); // Start server on port 80


void setup(){

 
  Serial.begin(9600); // my ESP8266 is now running this speed
  pinMode(redPin, OUTPUT); // Set this pin to output
  pinMode(greenPin, OUTPUT); // Set this pin to output
  WiFi.init(&Serial);    // initialize ESP module
  delay(1000);

  WiFi.begin(ssid, pass); // Start ESP8266-05 module and log on to network
 delay(1000);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
   
  }
 
  // Start the server
  server.begin();
  delay(1000);
}
 


void loop(){
  // Create a client connection
  WiFiEspClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        // Read the first line of the request
  String req = client.readStringUntil('\r');

         client.flush();
  delay(100);
     
      //control Arduino pins
        int val;   
        if (req.indexOf("redon") != -1) 
         val = 1; // if "redon" command is received change "val" to "1"
        else if (req.indexOf("redoff") != 1)
         val = 0; // if "redoff" command is received change "val" to "0"
       
       int val1;   
       if (req.indexOf("greennon") != -1)
         val1 = 1; // if "greennon" command is received change "val1" to "1"
       else if (req.indexOf("greenoff") != 1)
         val1 = 0; // if "greenoff" command is received change "val1" to "0"
       
      else {
    client.stop();
    return;
  }
        { //update the PINS
          digitalWrite(redPin, val);
          digitalWrite(greenPin, val1);
          //Respond to homebrdige server
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          delay(1);
          //stopping client
          client.stop();

         
          //clearing string for next read
         // readString="";
       
      }
    }
  }
}
}



Since there don't seem to be  good instructible, I will probably make a good one with all the codes once I get it working so others will benefit of all of this :)

Anyways thanks for all the help in advance!

AWOL

Could your problem be related to this?
Code: [Select]
  // readString="";
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

szeitz

I've just uncommented that part, but still the same effect.


ieee488

I am guessing that you send separate commands for the red LED and the green LED.
In other words, you send
redon
greennon



So what happens to the following code, when the second command greennon is received?
Code: [Select]

        int val;   
        if (req.indexOf("redon") != -1)
         val = 1; // if "redon" command is received change "val" to "1"
        else if (req.indexOf("redoff") != 1)
         val = 0; // if "redoff" command is received change "val" to "0"




szeitz

Yes, two commands as you stated. What happens to the redPin command set I don't know. Can't figure out what is going on.

Also tried moving the digitalWrite commands under the ON and OFF commands, also manually writing them HIGH or LOW as per the request. But still no luck. That way homebridge updated the status, but the LEDs didn't turn on. Actually they flashed on for like 1/10th of a second and that is it. 

I have no idea what is going on :)

szeitz

Also, the same thing happens if I have the greenPin ON and I turn ON the redPin then red gets HIGH but greenPin turns LOW.

changed the code like this
Code: [Select]
  if (req.indexOf("redon") != -1) {
  digitalWrite(redPin, HIGH);
 } else if (req.indexOf("redoff") != 1) {
  digitalWrite(redPin, LOW);
 } else if (req.indexOf("greennon") != -1) {
        digitalWrite(greenPin, HIGH);   
 } else if (req.indexOf("greenoff") != 1) {
        digitalWrite(greenPin, LOW);
 } else {
    client.stop();
    return;
  }


Now the greenPin doesn't get switched to HIGH. redPin works as espected. But if redPin is ON and I try turning greenPin on, it updates homebridge as if it was ON, and the red LED turns off

Go Up