ESP8266 and Arduino connected by SPI interface


#1

Hi,
I have just uploaded two projects to github that in my oppinion solve the problem with using ESP8266 modules as slaves to Arduino MCUs.
Instead of the serial interface the SPI interface is used here. Therefore the ESP module must be reflashed to a custom firmware.

The WiFiSpiESP project is in fact an application and must be flashed to ESP module while the WiFiSpi project is an Arduino library. I tried to keep the library objects and functions as close as possible in its behaviour to the default Arduino’s WiFi library in order to make migration to the WiFiSpi library easy.

I know that flashing ESP module may not be a solution for beginners, but I am afraid there is no chance to make a robust solution on AT command protocol and SoftwareSerial class.

The projects are:



#2

Hi @JiriBilek
This is great! Only today I saw this.
I will try it, I hope in the next days.
If I understand well I need to flash SPI Slave for ESP8266 module and use the SPI library for Arduino AVR to connect with ESP8266, it seems easy :slight_smile:
I will give a feedback for sure.
Thanks!


#3

Hi @Andrei_Sousa,
exactly. Just be careful and do not connect 5V to ESP module. A simple solution is to power the Arduino with 3.3V :slight_smile: .


#4

Hi @JiriBilek!
Thanks for remeber these important details.
I hope have time to do this in the next days.


#5

Hi @JiriBilek!
Everything works great and easly.
At this moment I only want to make a simple example to test thinger.io.
I´m using the last lybrary from here https://github.com/thinger-io/Arduino-Library

#include <WiFiSpi.h>
#include <ThingerWifi.h>


#define username "****"
#define deviceId "****"
#define deviceCredential "****"

#define ssid "****"
#define pass "****"
//int status = WL_IDLE_STATUS; //I´m not sure if I need this here

ThingerWifi thing("username", "deviceId", "deviceCredential"); // I get an error here

WiFiSpiClient client;

void setup() {
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Initialize the WifiSpi library
  WiFiSpi.init();

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

  String fv = WiFiSpi.firmwareVersion();
  if (fv != "0.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFiSpi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  // configure wifi network
  thing.add_wifi(ssid, pass);

  pinMode(5, OUTPUT);

  // pin control example (i.e. turning on/off a light, a relay, etc)
  thing["led"] << digitalPin(5);

  // resource output example (i.e. reading a sensor value, a variable, etc)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFiSpi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFiSpi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFiSpi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

I need to change in “ThingerWifi.h” where is “WiFi” to “WiFiSpi”?
The error is:
C:\Users\admin\Documents\Arduino\libraries\Arduino-Library-master\src/ThingerWifi.h:46:17: error: 'WiFi' was not declared in this scope

I need your help to make the first example.
Thanks!
AS


#6

Exactly, you have to change WiFi to WiFiSpi because each belongs to a different library.


#7

Hi @JiriBilek!
I was doing several errors in the example I sent before.
First I need include the file ThingerESP8266AT.h and now I have a difrent error:
ThingerESP8266AT.h:64:5: error: 'TinyGsm' does not name a type

What I should I do?
Thanks!
AS


#8

Hi @Andrei_Sousa,
you have to change Thinger header as well, sorry, I forgot to mention it.
J.

ThingerWifiEspSpi.h (2.8 KB)


#9

Hi @JiriBilek

Now I can do the upload to the Arduino with no errors.
But for some reason the STATE of the device is always Disconnected, I don´t understand what I´m doing wrong.

The issue is in the code I upload?

The code I upload now is:

#include <WiFiSpi.h>

#include <ThingerWifiEspSpi.h>


#define username "***"
#define deviceId "***"
#define deviceCredential "***"

#define ssid "***"
#define pass "***"
int status = WL_IDLE_STATUS;

ThingerWifiEspSpi thing("username", "deviceId", "deviceCredential");

WiFiSpiClient client;

void setup() {
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Initialize the WifiSpi library
  WiFiSpi.init();

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

  String fv = WiFiSpi.firmwareVersion();
  if (fv != "0.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFiSpi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  // configure wifi network
  thing.add_wifi(ssid, pass);

  pinMode(5, OUTPUT);

  // pin control example (i.e. turning on/off a light, a relay, etc)
  thing["led"] << digitalPin(5);

  // resource output example (i.e. reading a sensor value, a variable, etc)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFiSpi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFiSpi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFiSpi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Any sugestion?

Thanks a lot!
AS


#10

Andrei,
try to simplify the setup function:

void setup() {
  
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Initialize the WifiSpi library
  WiFiSpi.init();

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

  String fv = WiFiSpi.firmwareVersion();
  if (fv != "0.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  // configure wifi network
  thing.add_wifi(ssid, pass);

  ... etc. etc. ...

Don’t connect the wifi manually, let thinger do the work. If it doesn’t help, connect serial monitor to ESP and look at the output. The serial output from Arduino may help, too.

Jiri


#11

i used your library

but when i build in my IDE. had some prroblem

C:\Program Files (x86)\Arduino\libraries\wifi3/ESP8266WiFiGeneric.h:27:22: fatal error: functional: No such file or directory

#include functional

i can not include functianal

can you help me?


#12

Hi,
what library are to refering to? The one for Arduino or are you trying to compile the part for ESP8266?
I tried to find the ESP8266WiFiGeneric.h file on my computer and I didn’t find it. So could it be you have more wifi libraries installed and the error comes from another library?


#13

Hi @JiriBilek
I’ve tried using your library to connect between NodeMCU and Arduino Nano. When i installs the WifiSpi for arduino, it is straight forward by adding Zip library on Arduino IDE. But when i tried to installs WifiIspEsp using same method, it show error : Specified folder/zip file does not contain a valid library.
I also tried manual install for WifiIspEsp, unzip the folder, and copy all of the file into library folder of arduino. And still got same error message.

How to install those library into Arduino IDE?


#14

Hi @astonix ,
the ESP part is not a library. It is an application instead. You must load ESP8266 toolchain into arduino, see https://github.com/esp8266/Arduino. Then open the .ino file, compile it and load it into the MCU.


#15

Hi,

Thanks for your answer, it helps me a lot!
Now, i can see my device on thinger dashboard using Nano and NodeMCU.


#16

Hello @JiriBilek,
I’ve been using your library for analog monitoring and the result is good, the thingerio dashboard is showing measurement data as expected. But, when i tried to use GPIO control over dashboard, there is no action on output. I already measure the output voltage pin when i press the button on dashboard and it is still 0V. It should be around 3,3V - 5V.
I am using ATmega328 (Nano) as main controller, and NodeMCU (ESP8266) as wifi controller.

Is there any clue to solve this?


#17

Hi, could you please post the relevant code?


#18

Hi,

Here it is :

#define DEBUG
#define DISABLE_TLS

#include <WiFiSpi.h>
#include <ThingerWifiEspSpi.h>

//platform credentials
#define USERNAME “"
#define DEVICE_ID “**************”
#define DEVICE_CREDENTIAL "
******”

//gateway modem/router
#define SSID “******************”
#define SSID_PASSWORD “*********”

//Output definition
#define machine_on 5
#define machine_start 6

ThingerWifiEspSpi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
Serial.begin(9600);
Serial.println();
//Set output mode for all IO pins

pinMode(machine_on, OUTPUT);
pinMode(machine_start, OUTPUT);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// Initialize the WifiSpi library
WiFiSpi.init();
delay(10);

thing.add_wifi(SSID, SSID_PASSWORD);

thing[“Machine_On”]<< [](pson& in) {
if(in.is_empty()){
in=(bool)digitalRead(5);
}else{
digitalWrite(5, in ? LOW : HIGH); //Here is the control of the GPIO5
digitalWrite(machine_on, HIGH); // Get start
delay(100); // sending delay 100ms
digitalWrite(machine_on, LOW); // Get low
}
};

thing[“Machine_Start”]<< digitalPin(6);

}

void loop() {
thing.handle();
}


#19

As I understand, the problem is in not entering the “else” part of the “Machine_On” function. Just to be sure, place please a Serial.print there and print the in variable value. Maybe the in variable is always empty.


#20

Hi,
The output of “machine start” is also low (around 0V) when i press the button on thinger dashboard.