Page 1 of 1

Connecting a Raspberry Pi 3 with ESP8266 connected on Arduino Uno via Wifi

Posted: Sat Aug 26, 2017 5:31 am
by mrGandhi
Hello, I'm new in this forum and I don't have much experience with Arduino or Raspberry Pi.

But I need to connect my Arduino Uno with my Raspberry Pi 3, and I'm using a ESP8266 - ESP01 as my WiFi module for the Arduino. The objective now is to turn a LED on with Arduino with a command from RPi.

Does anyone knows how can I make this connection?

Thanks!

Re: Connecting a Raspberry Pi 3 with ESP8266 connected on Arduino Uno via Wifi

Posted: Sat Aug 26, 2017 4:21 pm
by KLL
+ if your ESP8266 is a wifi server ?
+ + in STA mode ( then it must be login into same router as your RPI )
+ + in AP mode ( then the RPI need to connect to that network ( ESP SSID password )
+++ in that case you can use a browser ( also from RPI ) to bring up that web page.


+ if your ESP8266 is a wifi client ? and want send data to a webserver page
+ + and it is connected to your network ( router SSID password )
and you want that data to be send to your RPI you need to make that webserver / site / data collection script in the RPI.

++ if you want configure the Arduino via RPI you can connect it
by USB cable and install Arduino IDE on RPI

but possibly you want talk about a
MQTT network with RPI as broker and WIFI Arduino as MQTT client
search for: Mosquitto on the Raspberry Pi

but sorry, i never used the arduino ( with ESP8266 as WIFI arduino )
only Arduino or ESP
so possibly i am far away from your question.

Re: Connecting a Raspberry Pi 3 with ESP8266 connected on Arduino Uno via Wifi

Posted: Sun Aug 27, 2017 12:05 pm
by jbudd
but possibly you want talk about a
MQTT network with RPI as broker and WIFI Arduino as MQTT client
search for: Mosquitto on the Raspberry Pi
Yes. You can do it with an MQTT broker on your Pi and MQTT client on the Arduino.

You don't actually need the Arduino for switching an LED, just an ESP board can run MQTT and switch the LED.

For a web based drag and drop control setup on the Pi I recommend Node-Red.

Re: Connecting a Raspberry Pi 3 with ESP8266 connected on Arduino Uno via Wifi

Posted: Sun Aug 27, 2017 10:39 pm
by mrGandhi
First of all, thank you for the replies.
but possibly you want talk about a
MQTT network with RPI as broker and WIFI Arduino as MQTT client
Yes, I'm trying to do something like that. I'm following this tutorial: https://sonyarouje.com/2016/03/15/mqtt- ... 66-esp-01/ to connect the esp8266 and the arduino to a MQTT broker. But I still have a problem, my esp8266 keeps connecting and reconnecting to the Broker. I'll but my code below if anyone is interested to help.

But my final objective isn't this, what I want to do is to make a small IoT network with the RPi and the arduino, and test some security systems and protocols. And I think that the esp8266 won't have the computational power necessary for that.

Code: Select all

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
//#include <WiFiEspUdp.h>
//#include <WiFiClient.h>
#include <SoftwareSerial.h>
#include <PubSubClient.h>

#define DEBUG true

//Connecting to the Wifi

const char* ssid = "********";
const char* pass = "*******";
const char* mqtt_server = "iot.eclipse.org";
const char* mqtt_topic = "ArdUnoLED";

int status = WL_IDLE_STATUS; 

IPAddress ip(192, 168, 115, 102);
IPAddress gateway(192, 168, 115, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiEspClient espClient;
PubSubClient client(espClient);


SoftwareSerial esp8266(3,2);

const byte ledPin = 8;

void callback(char* topic, byte* payload, unsigned int length){

  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("]");

//  for (int i=0; i<length; i++)
//  {
//    char receivedChar = (char)payload[i];
//    Serial.print(receivedChar);
//
//    if (receivedChar == '0');
//    digitalWrite(ledPin, LOW);
//
//    if (receivedChar == '1');
//    digitalWrite(ledPin, HIGH);
//  }
  Serial.println();
}


void reconnect(){
    // Loop until we're reconnected

    while(!client.connected())
    {
      Serial.print("Attempting MQTT connection...");

      // Attempt to connect
      if(client.connect("ESP8266 Client"))
      {
        Serial.println("connected");
        client.subscribe(mqtt_topic); //Connecting to the topic
        client.publish(mqtt_topic, "Arduino Test");
      }
      else
      {
        Serial.print("failed, rc=");
        Serial.print(client.state());
        Serial.println(" try again in 5 seconds");
        delay(5000); // wait for 5 seconds
      
      }
    }
    Serial.println("I'm connected and heading out from the reconnect function");
  }



  void setup()
  {
    Serial.begin(9600);
    esp8266.begin(9600);

    WiFi.init(&esp8266);

    // check for the presence of the shield
    if (WiFi.status() == WL_NO_SHIELD) {
      Serial.println("WiFi shield not present");
      // don't continue
      while (true);
    }

    // attempt to connect to WiFi network
    while ( status != WL_CONNECTED) {
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network
      status = WiFi.begin(ssid, pass);
    }

    Serial.println("You're connected to WiFi now");

    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);

    pinMode(ledPin, OUTPUT);
  }

  void loop()
  {
    if (!client.connected())
    {
      reconnect();  
    }

    
    client.loop();
  }

Re: Connecting a Raspberry Pi 3 with ESP8266 connected on Arduino Uno via Wifi

Posted: Tue Aug 29, 2017 3:32 pm
by KLL
mrGandhi wrote:
Sun Aug 27, 2017 10:39 pm
But my final objective isn't this, what I want to do is to make a small IoT network with the RPi and the arduino, and test some security systems and protocols. And I think that the esp8266 won't have the computational power necessary for that.
ahm, i understand that you use the ESP8266 ESP-01 board,
as a WIFI modem used from Arduino by serial AT commands.

so that actually tells you nothing about the "esp8266 computational power"

i used a Arduino as PID controller with USB cable from RPI, where the RPI has the python flask webserver
to operate the controller via browser.

and now i stuffed the PID control ( just copy ) AND the webserver ( ported from flask to ESP wifi ) into a ESP8266
( and using the RPI just for develop and upload the code ( via Arduino IDE ) and give 5V USB power )
Image
not bad for a 3$ board ha?