0

I've been working on this project, which consists in using a web page to control RPi's GPIOs and read sensor data via WebIOPI. Everything worked fine, at this point the project is very simple and the documentation on WebIOPi website really helped. Also, if anyone is having trouble with this, I recommend this tutorial. But I decided to raise the complexity level by doing the exact same thing, except controlling various ESP-01s connected to the server as clients. These ESP-01s have been updated with NodeMCU firmware, in order to be programmed directly and not only used as Wi-Fi modules for some other microcontroller. Also, they can be programmed straight from the Arduino IDE, which really helps.

The thing is: I have no experience at all working with HTTP protocols. There are several examples for both server and client applications on the ESP8266 side, but I can't figure out what to do on the RPi side. I've already been able to connect the ESP-01 to the server on the Pi, but I can't really exchange information between them.(Here is the code that does the connection on the client side, in case anyone asks)

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#define       LED0      2

char*         ssid = "SSID";
char*         password = "PASSWORD";

int             ESPServerPort  = 8000;
IPAddress       ESPServer(192,168,0,133);
WiFiClient      client;

void setup() {
  Serial.begin(115200);
  pinMode(LED0, OUTPUT);
  Serial.println("\nSetting I/O Pin Modes ... Done\n");

  if(WiFi.status() == WL_CONNECTED){
    WiFi.disconnect();
    WiFi.mode(WIFI_OFF);
    delay(50);
  }

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.println("!--- Connecting To " + WiFi.SSID() + " ---!");

  CheckWiFiConnectivity();

  digitalWrite(LED0, LOW);
  Serial.println("!-- Client Device Connected --!\n");

  Serial.println("Connected To      : " + String(WiFi.SSID()));
  Serial.println("Signal Strenght   : " + String(WiFi.RSSI()) + " dBm");
  Serial.print  ("Server IP Address : ");
  Serial.println(ESPServer);
  Serial.print  ("Server Port Num   : ");
  Serial.println(ESPServerPort);
  Serial.print  ("Device MC Address : ");
  Serial.println(String(WiFi.macAddress()));
  Serial.print  ("Device IP Address : ");
  Serial.println(WiFi.localIP());
  Serial.print  ("\n\n");

  ESPRequest();
}

void loop(){

}


void CheckWiFiConnectivity(){
  while(WiFi.status() != WL_CONNECTED){
    for(int i=0; i < 10; i++){
      digitalWrite(LED0, !HIGH);
      delay(250);
      digitalWrite(LED0, !LOW);
      delay(250);
      Serial.print(".");
    }
    Serial.println("");
  }
}

void ESPRequest(){

  client.stop();
  Serial.println("!---Connecting to server---!");

  client.connect(ESPServer, ESPServerPort);

  if(client.connected()){
    Serial.println    ("<CONNECTED>\n");
    client.println ("<CONNECTED>");
  }
}

I'm not sure if I need to tap into the WebIOPi code files, the ones that do the HTTP heavy-lifting, or if simple changes on my html/javascript code would do the trick. Well... How can I do this?

Help a newbie out guys. Thanks in advance!

1 Answer 1

0

Well, in case anyone has trouble with this in the future, I figured it out. It turns out the REST API on WebIOPi does all the work, really. You can check this out on WebIOPi Documentation. To put it simply: no need to mess around with server configurations, all you need is have your client make the request. In my case, it ended up like this:

void loop(){
    HTTPClient http;
    int state;

    Serial.println("!---[HTTP] begin---!\n");
    http.begin("http://192.168.0.133:8000/GPIO/27/value");

    Serial.print("[HTTP] GET...\n");
    int httpCode = http.GET();

    if(httpCode > 0) {
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        if(httpCode == HTTP_CODE_OK) {
            String payload = http.getString();
            state = payload.toInt();
            Serial.println(payload);
        }
    } 

    else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    if(state == 1)
        digitalWrite(LED0, HIGH);
    else
        digitalWrite(LED0, LOW);

    http.end();
    delay(200);
}

The http.begin() is set to request the value of GPIO 27. Then, given the state of this GPIO on RPi, the GPIO on ESP8266 is set accordingly.

Your Answer

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

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