Created
June 15, 2021 09:36
-
-
Save sblantipodi/b3b9eecd369fe1482ad9722ae34f7c8f to your computer and use it in GitHub Desktop.
WiFi performance: ESP8266 vs ESP32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#else | |
#include <WiFi.h> | |
#endif | |
#if defined(ESP8266) | |
#include <ESP8266WebServer.h> | |
#else | |
#include <WebServer.h> | |
#endif | |
#include <PubSubClient.h> | |
#include <FastLED.h> | |
const char *mqtt_server = "192.168.1.3"; //mqtt server | |
const char *ssid = "XXX"; | |
const char *password = "XXX"; | |
float framerate = 0; | |
float framerateCounter = 0; | |
WiFiClient espClient; | |
PubSubClient client(espClient); //lib required for mqtt | |
void callback(char *topic, byte *payload, | |
unsigned int length) { //callback includes topic and payload ( from which (topic) the payload is comming) | |
framerateCounter++; | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.println("Attempting MQTT connection..."); | |
if (client.connect("ESP32_clientID")) { | |
Serial.println("connected"); | |
client.subscribe("lights/glowwormluciferin/set/stream"); //topic=Demo | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void connectmqtt() { | |
client.connect("ESP32_clientID"); // ESP will connect to mqtt broker with clientID | |
{ | |
Serial.println("connected to MQTT"); | |
client.subscribe("lights/glowwormluciferin/set/stream"); //topic=Demo | |
if (!client.connected()) { | |
reconnect(); | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(1000000); | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, LOW); | |
WiFi.begin(ssid, password); | |
Serial.println("connected"); | |
client.setServer(mqtt_server, 1883);//connecting to mqtt server | |
client.setCallback(callback); | |
//delay(5000); | |
connectmqtt(); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
EVERY_N_SECONDS(10) { | |
framerate = framerateCounter > 0 ? framerateCounter / 10 : 0; | |
framerateCounter = 0; | |
Serial.println("framerate"); | |
Serial.println(framerate); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment