ESP32 HSPI and W5500 class issue - esp32

Im struggling with "bounding" ethernet controller W5500 to secon SPI bus (hspi) on ESP32.
On primary bus (vSPI) it works without a problem, but when i try to connet it to second bus it stops to respond.
Below is the code that I'm using. So when i switch the SS pin to hspi (hspi default 15 and vspi default 5) and also MISO, MOSI, SCK ethernet controller does not work, when go go back to default spi it works flawlessly.
I tried to declare spi class in Ethernet.init but it's only ment for SS pin assigment.
is there a way to assign hSPI class to default Arduinos Ethernet library?
#include <SPI.h>
#include <Ethernet.h> // Ethernet library v2 is required
#include <ModbusEthernet.h>
SPIClass spiV(VSPI);
SPIClass spiH(HSPI);
// Enter a MAC address and IP address for your controller below.
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE
};
// Set your Static IP address
IPAddress local_IP(192, 168, 0, 6);
// Set your Gateway IP address
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200); // Open serial communications and wait for port to open
spiV.begin();
spiH.begin();
Ethernet.init(15); // SS pin for spiV
Ethernet.begin(mac, local_IP); // start the Ethernet connection
delay(1000); // give the Ethernet shield a second to initialize
mb.server(); // Act as Modbus TCP server
mb.addReg(HREG(100), 0xABCD);
}
void loop() {
mb.task(); // Server Modbus TCP queries
delay(50);
}
I tried to declare spi class in Ethernet.init but it's only ment for SS pin assigment.

Related

ESP32 How to change default port of AsyncWebServer from 80 to any other port number

I have application that I need to run multiple ESP32s in same network which publishes webserver, in my case multi device is broadcasting in same port as 80. This causing port issue I guess because only one device at a time is working on browser. When I change port number to 8080, I cannot make it work. Is there any configuration that I need to do before working with multi webserver?
I shared the code below which working, my purpose is make it work with multi device in same network.
#include <ESPmDNS.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "XXXX";
const char* password = "XXXX";
AsyncWebServer server(80);
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
if(!MDNS.begin("esp32")) {
Serial.println("Error starting mDNS");
return;
}
Serial.println(WiFi.localIP());
server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "Hello World");
});
server.begin();
}
void loop(){}

Bizzare behaviour of ESP32 websocket webserver when not connected to the internet

I am working on a project that uses the ESP32 C3 dev board and runs a webserver with dns, and comunicates with the client via websocket. here is the arduino code (the part of it relevent to the webserver):
#include <math.h>
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
#include <time.h>
#include "SPIFFS.h"
#include <FS.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
#include <ESPmDNS.h>
#include <WebSocketsServer.h>
#include <DNSServer.h>
#include <NTPClient.h>
#include <Adafruit_NeoPixel.h>
#include <driver/adc.h>
WebServer server(8000);
DNSServer dnsServer;
WebSocketsServer webSocket = WebSocketsServer(81);
WiFiServer server2(80);
IPAddress apIPDNS(8,8,4,4);
IPAddress testIP1(192,168,1,4);
IPAddress testIP2(192,168,1,5);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
IPAddress apIP(192, 168, 4, 3);
IPAddress netMsk(255, 255, 255, 0);
uint8_t* data;
void onWebSocketEvent(uint8_t num,
WStype_t type,
uint8_t * data1,
size_t length) {
// Figure out the type of WebSocket event
switch(type) {
// Client has disconnected
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
// New client has connected
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
// Serial.printf("[%u] Connection from ", num);
// Serial.println(ip.toString());
}
break;
// Echo text message back to client
case WStype_TEXT:
data = data1;
parsedata();
break;
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
default:
break;
}
}
void setup() {
Serial.begin(115200);
EEPROM.begin(512);
time_now = millis();
WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(testIP1, testIP1, netMsk);
WiFi.softAP(ssid, password);
WiFi.hostname("esp32server");
WiFi.begin(sta_ssid, sta_password);
dnsServer.start(53, "*", WiFi.softAPIP());
Serial.println("connecting to wifi1...");
delay(6000);
if(WiFi.status() == WL_CONNECTED) { current_connected_wifi = String(sta_ssid); current_connected_password = String(sta_password);Serial.println("connected to:" + current_connected_wifi);}
else {WiFi.begin(sta_ssid2, sta_password2);delay(6000);Serial.println("connecting to wifi2...");
if(WiFi.status() == WL_CONNECTED) { current_connected_wifi = String(sta_ssid2); current_connected_password = String(sta_password2);Serial.println("connected to:" + current_connected_wifi);}
else {WiFi.begin(sta_ssid3, sta_password3);delay(6000);
Serial.println("connecting to wifi3...");
if(WiFi.status() == WL_CONNECTED) { current_connected_wifi = String(sta_ssid3); current_connected_password = String(sta_password3);Serial.println("connected to:" + current_connected_wifi);}
else { Serial.println("NO INTERNET"); WiFi.disconnect();dnsServer.start(53, "*", WiFi.softAPIP());}
}
}
SPIFFS.begin();
MDNS.begin("espserver");
server.on("/", handle_root);
server.on("/Chart",HTTP_GET, handle_chart);
server.on("/Style",HTTP_GET, handle_chartcss);
server.on("/mark",HTTP_GET, handle_img);
server.on("/nonet",HTTP_GET, handle_img2);
server.on("/wifilogo",HTTP_GET, handle_img3);
server.on("/adressip", handle_adressip);
server.on("/maxcoff", handle_maxcoff);
server.on("/request", handle_request);
server.on("/mintime", handle_mintime);
server.on("/maxtime", handle_maxtime);
server.on("/dailychart", handle_dailychart);
server.on("/hourlychart", handle_hourlychart);
server.on("/monthlychart", handle_monthlychart);
server.on("/cumulative", handle_cumulative);
server.on("/monthly", handle_monthly);
server.on("/SSIDnames", handle_SSIDnames);
server.on("/getmaxenergy", handle_maxenergy);
server.begin();
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
server2.begin();
MDNS.addService("http", "tcp", 80);
}
void loop() {
WiFiClient client = server2.available();
dnsServer.processNextRequest();
server.handleClient();
webSocket.loop();
delay(20);
}
quick explenation:
when the ESP32 starts, it checks if it can connect to the internet with any of the 3 saved credentials from eeprom (sta_ssid, sta_password). If it manages to connect with any of them, the webserver can be accesed either when connecting to the wifi of the ESP or on the network that the ESP32 is connected to. When it is unable to find a a network it can only be accesed when connected to the wifi AP of the ESP32. The webserver is accesed from either 192.168.1.4:8000 or espserver.local:8000.
This works great regardless of internet access. When i connect to the webserver i send my html and javascript code to the client and then establish a websocket connection. When the esp doesnt have internet acces the websocket connection works fine, and packets get recieved within milieconds, worst case scenario a couple of them take 2 seconds to arrive.
HOWEVER, when the ESP is not connected to the internet, the packets get stuck on pending and take forever to arive (even 10 seconds), or dont arive at all. Its almost like the webserver is too busy, even tho it has far less to do compered to when it is connected to the internet.
My question is, do i have to use a special function to disconect the webserver from the internet (where WiFi.disconect() is not enough?) or how can I make the WebServer library work better to not lose any packets?
(i have tried using the asynch websocket library, however it does not work on my specific chipset (esp32 c3) so i have to make it work with this library)
I figured it out. the problem was as some people hinted, that the wifi status was 1, which meant it was constantly trying to reconnect (and using cpu time). I used WiFi.disconnect(true); to disconnect the wifi if it didnt connect to any network during setup. that solved the problem.

Using PuTTY to print from STM32

I want to print messages from my STM32 Nucleo-L073RZ microcontroller. How should I go about it?
Should I use UART? Where can I get the corresponding code?
#include "stm32l0xx.h"
#include "stm32l0xx_nucleo.h"
#include "stm32l0xx_hal.h"
#include "stdio.h"
static void GPIO_Init (void);
static void UART_Init (void);
int main(void)
{
HAL_Init();
GPIO_Init();
printf("Hello");
while(1)
{
}
}
static void GPIO_Init(void)
{
BSP_LED_Init(LED2);
BSP_LED_On(LED2);
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA13*/
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
/*Uart Init Function*/
static void UART_Init(void)
{
}
void EXTI4_15_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_PIN)
{
BSP_LED_Toggle(LED2);
counter();
}
int counter()
{
int i;
i = 0;
i++;
printf("/n %d", i);
}
How do I display the counter on my PC? I want the number of times the interrupt is given to be seen on PuTTY. Should I interface an UART or is it possible to print?
You can use UART on the Nucleo
All Nucleo boards have a built-in UART-to-USB module that automatically transmits data to a Serial Port on your computer. If on windows, open your Control Panel, go to Device Manager, and under COM Ports you should see your Nucleo.
Initialize the UART Peripheral
Reference your Nucleo user manual to see which UART pins connect to the USB port (STM32CubeMX might have these already mapped).
When initializing the peripheral, select a baud rate like 9600, and remember it
Configure PuTTy
Enter the COM port of the Nucleo and the Baud Rate that you selected earlier, and select Serial as the transmission method. You might have to disable some of the hardware flow control options if they are enabled
Code to transmit
HAL has functions for transmitting over UART. Something like HAL_UART_Transmit(...). You'll have to look up how to use the function specifically, plenty of great tutorials out there.
I personally use sprintf to print nicely formatted strings over UART like this:
char buf[64];
sprintf(buf, "Value of counter: %d\r\n", i);
// change huartX to your initialized HAL UART peripheral
HAL_UART_Transmit(&huartX, buf, strlen(buf), HAL_MAX_DELAY);
First Add use UART Handler and its init in this function i used UART2 change it to your periph if you use Stm32 Cube or IDE just select the periph it is automatically generated.
Use this function in order to use the print function it's act the same like Printf.
#include <stdint.h>
#include <stdarg.h>
void printmsg(char *format,...) {
char str[80];
/*Extract the the argument list using VA apis */
va_list args;
va_start(args, format);
vsprintf(str, format,args);
HAL_UART_Transmit(&huart2,(uint8_t *)str, strlen(str),HAL_MAX_DELAY);
va_end(args);
}
In your Counter function just Change printf to printmsg
int counter()
{
int i;
i = 0;
i++;
printmsg("/n %d", i);
}
Remember to change Printmsg uart Handler .

RPi + ESP8266 stability issues

I was recently working on a home automation project which has finally come to end and I am thinking to install it in my home. First of all, I would like to brief you with the basic architecture.
I am using a Raspberry Pi 3 as the central controller node which is running Node-Red and its Mosca palette. Currently, there are 5 ESP-01 in the project. Four of them are wired up with a relay and the remaining ESP is wired up with a DHT11 temperature sensor. Almost everything is running good but I am facing some stability issues, like, when I recycle the power the ESP-01 doesn't run the program. Serial monitor stays blank. However, when I disconnect the GPIO2 from the relay and then power up the ESP. The program begins. So, I have to pull out the GPIO2 then power up the ESP then connect the GPIO2 with the relay on every power recycle. Another problem which I am facing is sometimes the ESP crashes automatically. It sometimes prints out fatal exception(0) or soft wdt reset even though I have added a watchdog timer.
Here is the code for one of the client ESP:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "........";
const char* password = ".........";
const int led = 13;
const char* mqtt_server = "192.168.1.8";
WiFiClient espClient;
PubSubClient client(espClient);
const int ledGPIO2 = 2;
void setup_wifi() {
int i;
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("WIFI BEGUN");
while (WiFi.status() != WL_CONNECTED) {
ESP.wdtFeed();
delay(500);
i++;
if ((i&0x01)==0){
digitalWrite(led, 0);
} else {
digitalWrite(led, 1); // led should start blinking at .5 seconds
}
Serial.print(".");
if (i>1000) break; // get out after 50 seconds
if (i==1000){
}
Serial.print(".");
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
}
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if(topic=="Lamp1"){
Serial.print("Changing GPIO 2 to ");
if(messageTemp == "on"){
digitalWrite(ledGPIO2, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(ledGPIO2, LOW);
Serial.print("Off");
}
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.subscribe("Lamp1");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(ledGPIO2, OUTPUT);
digitalWrite(ledGPIO2, true);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
}
Also, I have been thinking for an efficient power supply for ESP. Batteries cannot be reliable for long-term and powering up via an adapter would be unfeasible as the module is going to be mounted on the wall. Moreover, ac to dc converter was something which seems to be a decent way for power supply.
Vcc - 3.3V
CH_PD - 3.3V
Tx - Tx (Arduino)
Rx - Rx (Arduino)
GPIO0 - GND (while uploading the sketch)/ 3.3V
GND - GND
I am not using capacitors or resistors. I am getting a 5V supply from Arduino which is regulated to 3.3V using LD33V voltage regulator.
Any suggestions would be appreciated. Thank You!!

Connecting Intel Galileo to Mac

I am trying to connect an Intel Galileo board to a Mac via the Galileo ethernet port which connects through to a thunderbolt ethernet port on the Mac. I have uploaded an Arduino sketch to start the LAN connection:
#include <SPI.h>
#include <Ethernet.h>
// the media access control (ethernet hardware) address for the Galileo:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the Galileo:
byte ip[] = { 192, 168, 0, 2 };
void setup()
{
Serial.begin(9600);
Serial.println("Attempting to start Ethernet");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Serial.println("Attempting to configure Ethernet using Static IP");
Ethernet.begin(mac, ip);
}
Serial.print("Your IP address: ");
Serial.println(Ethernet.localIP());
}
void loop () {}
In my network settings, do I put my thunderbolt ethernet as DHCP? or manually enter an ip address? What would be the ip address of my galileo so that I can ssh to it?

Resources