I have been trying to get my ESP32 connected to Wifi with C++. I have a small issue which I don't understand at all. And will eventually getting it connected to AWS IOT. I have it all working perfectly with the Arduino native language with no issues, but when trying to get it running with C++ I can't even get the wifi connecting.
When I read the localIP back in the loop when running the connectWifi() function (arduino code) it reads back the IP easily and I already have this with other code working with AWS IOT perfectly. When trying to run the initialiseWifi() function (C++) it doesn't seem to work and it returns an IP of 0.0.0.0.
I got this code from a ESP32 AWS IOT tutorial using C++ found on Visual Studio PlatformIO, and I also couldn't get the tutorial working either.
I feel like this is a simple error and I am new to C++, so am hoping someone can point it out so I can fix it up please!
#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "lwip/inet.h"
#include "lwip/dns.h"
const int CONNECTED_BIT = BIT0;
WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);
void messageHandler(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}
void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
//Allocate storage for the struct
wifi_config_t wifi_config = {};
//Assign ssid & password strings
strcpy((char*)wifi_config.sta.ssid, "XXXXXXXXXX");
strcpy((char*)wifi_config.sta.password, "XXXXXXXXXXX");
wifi_config.sta.bssid_set = false;
// ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
void connectWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
printf("\n");
printf("Hello world!\n");
printf("Local IP: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(9600);
//connectWifi();
initialise_wifi();
}
void loop() {
Serial.println(WiFi.localIP());
delay(1000);
}
Related
I have an I2C multiplexer connected to my ESP8266 and attached five SH1106 OLED displays to it (bus 2 to 6). I tested everything with simple example code and it works. I have then written the following code in which I try to externalize the display switch logic to an own class and make it more handy to use. It compiles and loads onto the board, but I keep getting errors and only one of the display infrequently flickers the word "TEST". Do you have any idea why?
main.cpp :
#include <Arduino.h>
#include <services/displayservice/DisplayService_h>
#include "SH1106Wire.h"
DisplayService displayService;
void setup() {
}
void loop() {
for (int i = 2; i <= 6; i++) {
SH1106Wire display = displayService.display(i);
delay(2000);
display.clear();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_24);
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.drawString(64, 20, String("TEST"));
display.display();
delay(500);
}
}
With my own class DisplayService:
DisplayService_h :
#ifndef DisplayService_h
#define DisplayService_h
#include "SH1106Wire.h"
class DisplayService {
public:
DisplayService();
SH1106Wire display(u_int8_t displayNumber);
private:
void changeDisplay(u_int8_t displayNumber);
};
#endif
DisplayService.cpp :
#include "Arduino.h"
#include "SH1106Wire.h"
#include <Wire.h>
#include "DisplayService_h"
SH1106Wire displayObject(0x3C, SDA, SCL); // ADDRESS, SDA, SCL
DisplayService::DisplayService() {}
SH1106Wire DisplayService::display(u_int8_t displayNumber){
Serial.println(displayNumber);
this->changeDisplay(displayNumber);
displayObject.init();
return displayObject;
}
void DisplayService::changeDisplay(u_int8_t displayNumber){
Wire.beginTransmission(0x70); // TCA9548A address
Wire.write(1 << displayNumber); // send byte to select bus
Wire.endTransmission();
}
"Error message" if you can call it that :
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v00046240
~ld
I don't know the reason, but I found a solution: Instead of passing the display object around, I now pass only a pointer to the display.
#include "Arduino.h"
#include "SH1106Wire.h"
#include <Wire.h>
#include "DisplayService_h"
SH1106Wire aDisplay(0x3C, SDA, SCL); // ADDRESS, SDA, SCL
DisplayService::DisplayService() {
}
SH1106Wire* DisplayService::display(u_int8_t displayNumber){
this->changeDisplay(displayNumber);
aDisplay.init();
return &aDisplay;
}
void DisplayService::changeDisplay(u_int8_t displayNumber){
Wire.beginTransmission(0x70); // TCA9548A address
Wire.write(1 << displayNumber); // send byte to select bus
Wire.endTransmission();
}
However, it wouldn't work if I wouldn't init the display each time I change the active display.
ESP32S NodeMCU
VSCode with PlatformIO
Hey all,
Apologies if I get terms wrong, this is my first time with an ESP32 and webservers. I'm developing code for an amateur ESP32 project that involves connection to an http page. I have set up an if-else loop that reads the ending of the webserver url to set the waterState variable. The ESP32 will connect to my WiFi network with no issue. However, my computer will not connect to the url. The result should print out the state chosen (ex: morning) and the terminal will indicate that the function has completed execution.
I tried moving around the WiFiClient and WiFiServer instances but that hasn't worked. I was able to get this program working once before when I kept the ScheduleProt code inside the WiFi connect. I tried that replicating that again but it now isn't working.
WiFiConnect.h
#ifndef WIFICONNECT_H
#define WIFICONNECT_H
#include <WiFiClient.h>
#include "Wifi.h"
class WiFiConnect
{
private:
#define WIFI_NETWORK "NetworkName"
#define WIFI_PASSWORD "Password"
#define WIFI_TIMEOUT 20000
public:
void wifiConnect();
void wifiDisconnect();
};
#endif
WiFiConnect.cpp - Handles connection to WiFi
#include <WiFi.h>
#include <WiFiConnect.h>
void WiFiConnect::wifiConnect() {
WiFiServer server(80);
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_AP);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
//Connection Protocol
while(WiFi.status() != WL_CONNECTED && (millis() - startAttemptTime) < WIFI_TIMEOUT) {
Serial.println("...");
delay(100);
}
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed, attempting again...");
delay(5000);
}
else{
Serial.print("Connected: ");
Serial.println(WiFi.localIP());
server.begin();
}
}
void WiFiConnect::wifiDisconnect() {
Serial.println("Disconnecting from WiFi...");
WiFi.disconnect();
}
ScheduleProt.h
#ifndef SCHEDULEPROT_H
#define SCHEDULEPROT_H
class ScheduleProt {
public:
int waterState = -1;
void scheduleWatering();
};
#endif
ScheduleProt.cpp - Reads the URL from the http server
#include <WiFi.h>
#include <Arduino.h>
#include "WiFiConnect.h"
#include "ScheduleProt.h"
void ScheduleProt::scheduleWatering() {
WiFiServer server(80);
WiFiClient client = server.available();
WiFiConnect newConnection;
newConnection.wifiConnect();
while(waterState = -1){
if (client) {
String req = client.readStringUntil('\r');
Serial.println("Waiting for user input...");
//Watering Times
if(req.indexOf("/morning/") != -1){
client.print("Morning");
waterState = 0;
}
else if(req.indexOf("/noon/") != -1){
waterState = 1;
client.print("Noon");
}
else if(req.indexOf("/evening/") != -1){
waterState = 2;
client.print("Evening");
}
else if(req.indexOf("/midnight/") != -1){
waterState = 3;
client.print("Midnight");
}
}
}
Serial.println("User input recieved, Huzzah!" + waterState);
newConnection.wifiDisconnect();
}
Here is the terminal
�Connecting to WiFi...
...
...
...
...
...
...
...
...
Connected: 192.168.1.100
If it helps, here is the main.cpp code
#include <Arduino.h>
#include <time.h>
#include "Wifi.h"
#include "ScheduleProt.h"
#include "WaterProt.h"
#include "CurrentTime.h"
#define DEEPSLEEPTIME 86400000
#define WATER_DURATION 10000
#define MOTORPIN 0
WaterProt waterProtocol;
CurrentTime currentTime;
ScheduleProt newSchedule;
void setup() {
Serial.begin(9600);
newSchedule.scheduleWatering();
}
void loop() {
if (waterProtocol.getWateringHour(newSchedule.waterState) == currentTime.getCurrentHour()){
waterProtocol.waterPlant(MOTORPIN, WATER_DURATION);
}
else {
esp_sleep_enable_timer_wakeup(1800000);
esp_deep_sleep_start();
}
esp_sleep_enable_timer_wakeup(DEEPSLEEPTIME);
esp_deep_sleep_start();
}
The webpage error
[1]: https://i.stack.imgur.com/HLIaH.png
Any help would be appreciated! Thank you!
It looks like you're actually creating two separate WiFiServers, and only calling begin() on one of them.
One WiFiServer is created inside WiFiConnect::wifiConnect(). This is a local variable and so it goes away when that function completes.
The other WiFiServer is created inside ScheduleProt::scheduleWatering(). This one is probably the one you want to keep, but you'll need to add a call to server.begin() before the call to server.available().
My suggestion is:
Remove all references to WiFiServer from WiFiConnect::wifiConnect().
Move the two lines inside ScheduleProt::scheduleWatering() that relate to WiFiConnect to the top of the function, so that they happen before the WiFiServer is initialized.
Add a call to server.begin() just before the call to server.available().
I also notice that ScheduleProt::scheduleWatering() is called from your setup() function, which means that it will only run once (i.e. accept one HTTP connection), and the loop() function will only run after that connection has completed. It looks like this might be intended, but I wasn't sure so thought I should point it out just in case you were unaware.
Update: I just had another look at the docs and it looks like WiFiServer.available() doesn't actually wait for a client to connect, it just returns the connection if there is one already. If that's the case, you really need to call it repeatedly until it returns a connection (or use a blocking function instead - I haven't looked into what's available in terms of that). Probably the easiest way to do this with your current code is to move the WiFiClient client = server.available(); inside the while loop.
It might also be a good idea to add a delay(1); inside that loop, so that it isn't running the processor at 100% load. I can't say this for sure without refreshing my memory on how the ESP32 does power management, but it's likely that adding the delay will save a significant amount of power. Either way, it won't hurt.
This question already has an answer here:
expected unqualified-id before '.' token arduino library
(1 answer)
Closed 1 year ago.
When i try to call any function from my library, it gives the following error:
In function 'void setup()':
error: expected unqualified-id before '.' token
netti.SetupWifi();
^
Yesterday it worked fine and i'm pretty sure that i haven't changed anything so i have no idea what's wrong with it. The library is for making wifi connection and telnet monitoring easier.
Adruino code:
#include <netti.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
netti.SetupWifi(); //error here
}
void loop() {
// put your main code here, to run repeatedly:
netti.ReconnectWifi(); //same error here
Serial.println("runs"); // test so it's running atleast some code
delay(1000);
}
netti.h:
#ifndef netti_h
#define netti_h
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <ESP8266WiFi.h>
class netti{
public:
//constructor
netti(bool displayMsg=false);
//methods
void SetupWifi();
void ReconnectWifi();
void SetupMobile(); // for different wifi
void ReconnectMobile(); //for different wifi
WiFiClient Telnet;
private:
};
#endif
and netti.cpp:
#include <ESP8266WiFi.h>
#include "netti.h"
#include "Arduino.h"
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
IPAddress ip(192, 168, 1, 100);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer TelnetServer(23);
WiFiClient Telnet;
netti::netti(bool displayMsg){
Serial.println("netti kirjasto käytössä");
}
void(* resetFunc) (void) = 0;
void handleTelnet(){
Telnet.println("test");
Serial.println("telnet handler called");
if (TelnetServer.hasClient()){
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop();
Serial.println("telnet available");
Telnet =TelnetServer.available();
}else {
TelnetServer.available().stop();
Serial.println("telnet in use");
}
}
}
void netti::SetupWifi() {
delay(100);
Serial.println();
Serial.print("Connecting to ");
Serial.println("wifi");
WiFi.hostname("ESP_host");
WiFi.config(ip, gateway, subnet);
WiFi.begin("wifi name","pass place");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
};
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
TelnetServer.begin();
TelnetServer.setNoDelay(true);
};
void netti::ReconnectWifi() {
int i=0;
handleTelnet();
Telnet.println("test2"); // does nothing, why??
if(WiFi.status() != WL_CONNECTED){
delay(5000);
SetupWifi();
};
};
void netti::SetupMobile() {
delay(100);
Serial.println();
Serial.print("Connecting to ");
Serial.println("mobile");
WiFi.hostname("ESP_host");
WiFi.begin("mobile_place","pass place");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
};
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
TelnetServer.begin();
TelnetServer.setNoDelay(true);
};
void netti::ReconnectMobile() {
int i=0;
handleTelnet();
if(WiFi.status() != WL_CONNECTED){
delay(5000);
SetupMobile();
}
};
This is my first attempt at making my own library and i have no idea why it's not working.
Thanks in advance for all the help
PS. I'm having trouble with Telnet, trying to work the solution, which is hard, when the library doesn't run
The error is the following. You have defined a class called netti but you have not created an instance of the class. Therefore you cannot call instance methods on this class. For example if you have
class netti{
public:
//constructor
netti(bool displayMsg=false);
//methods
void SetupWifi();
void ReconnectWifi();
void SetupMobile(); // for different wifi
void ReconnectMobile(); //for different wifi
WiFiClient Telnet;
private:
};
you need to write
netti netti_instance(true);
netti_instance.SetupWifi();
If you want to be able to call methods on a class without an instance you need to declare them as static methods.
I suggest you look into some basic tutorials on how to use C++ classes.
i am trying to connect ESP32 with google cloude
but i am getting this error
Settings incorrect or missing a cyper for SSL
Connect with mqtt.2030.ltsapis.goog:8883
ClientId: projects/nomadic-armor-279716/locations/asia-east1/registries/iot-sample-registry/devices/esp32newD
Waiting 60 seconds, retry will likely fail
my code is divided over 4 files :
esp32main :
#include "esp32-mqtt.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
setupCloudIoT();
}
void loop() {
mqttClient->loop();
delay(10); // <- fixes some issues with WiFi stability
if (!mqttClient->connected()) {
connect();
}
}
ciotic_config.h
// This file contains your configuration used to connect to Cloud IoT Core
#include "credentials.h"
// Wifi newtork details.
const char *ssid = WIFI_SSID;
const char *password = WIFI_PASSWD;
// Cloud iot details.
const char *project_id = PROJECT_ID;
const char *location = REGION;
const char *registry_id = REGISTRY;
const char *device_id = DEVICE;
// Configuration for NTP
const char* ntp_primary = "pool.ntp.org";
const char* ntp_secondary = "time.nist.gov";
#ifndef LED_BUILTIN
#define LED_BUILTIN 5
#endif
// To get the private key run (where private-key.pem is the ec private key
// used to create the certificate uploaded to google cloud iot):
// openssl ec -in <private-key.pem> -noout -text
// and copy priv: part.
// The key length should be exactly the same as the key length bellow (32 pairs
// of hex digits). If it's bigger and it starts with "00:" delete the "00:". If
// it's smaller add "00:" to the start. If it's too big or too small something
// is probably wrong with your key.
const char *private_key_str =
// Replace this below string with your own, in the same format
// The below key is dummy, so using it will not help you in any way
"f3:36:fe:81:72:36:77:60:29:1d:4a:fc:21:c7:6c:"
"96:75:2c:7f:6e:52:2f:c8:cb:7d:03:c8:25:ef:28:"
"61:0b";
// Time (seconds) to expire token += 20 minutes for drift
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)
// To get the certificate for your region run:
// openssl s_client -showcerts -connect mqtt.googleapis.com:8883
// for standard mqtt or for LTS:
// openssl s_client -showcerts -connect mqtt.2030.ltsapis.goog:8883
// Copy the certificate (all lines between and including ---BEGIN CERTIFICATE---
// and --END CERTIFICATE--) to root.cert and put here on the root_cert variable.
const char *root_cert =
"-----BEGIN CERTIFICATE-----\n"
"MIIDfDCCAmSgAwIBAgIJAJB2iRjpM5OgMA0GCSqGSIb3DQEBCwUAME4xMTAvBgNV\n"
"BAsMKE5vIFNOSSBwcm92aWRlZDsgcGxlYXNlIGZpeCB5b3VyIGNsaWVudC4xGTAX\n"
"BgNVBAMTEGludmFsaWQyLmludmFsaWQwHhcNMTUwMTAxMDAwMDAwWhcNMzAwMTAx\n"
"MDAwMDAwWjBOMTEwLwYDVQQLDChObyBTTkkgcHJvdmlkZWQ7IHBsZWFzZSBmaXgg\n"
"eW91ciBjbGllbnQuMRkwFwYDVQQDExBpbnZhbGlkMi5pbnZhbGlkMIIBIjANBgkq\n"
"hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzWJP5cMThJgMBeTvRKKl7N6ZcZAbKDVA\n"
"tNBNnRhIgSitXxCzKtt9rp2RHkLn76oZjdNO25EPp+QgMiWU/rkkB00Y18Oahw5f\n"
"i8s+K9dRv6i+gSOiv2jlIeW/S0hOswUUDH0JXFkEPKILzpl5ML7wdp5kt93vHxa7\n"
"HswOtAxEz2WtxMdezm/3CgO3sls20wl3W03iI+kCt7HyvhGy2aRPLhJfeABpQr0U\n"
"ku3q6mtomy2cgFawekN/X/aH8KknX799MPcuWutM2q88mtUEBsuZmy2nsjK9J7/y\n"
"hhCRDzOV/yY8c5+l/u/rWuwwkZ2lgzGp4xBBfhXdr6+m9kmwWCUm9QIDAQABo10w\n"
"WzAOBgNVHQ8BAf8EBAMCAqQwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMC\n"
"MA8GA1UdEwEB/wQFMAMBAf8wGQYDVR0OBBIEELsPOJZvPr5PK0bQQWrUrLUwDQYJ\n"
"KoZIhvcNAQELBQADggEBALnZ4lRc9WHtafO4Y+0DWp4qgSdaGygzS/wtcRP+S2V+\n"
"HFOCeYDmeZ9qs0WpNlrtyeBKzBH8hOt9y8aUbZBw2M1F2Mi23Q+dhAEUfQCOKbIT\n"
"tunBuVfDTTbAHUuNl/eyr78v8Egi133z7zVgydVG1KA0AOSCB+B65glbpx+xMCpg\n"
"ZLux9THydwg3tPo/LfYbRCof+Mb8I3ZCY9O6FfZGjuxJn+0ux3SDora3NX/FmJ+i\n"
"kTCTsMtIFWhH3hoyYAamOOuITpPZHD7yP0lfbuncGDEqAQu2YWbYxRixfq2VSxgv\n"
"gWbFcmkgBLYpE8iDWT3Kdluo1+6PHaDaLg2SacOY6Go=\n"
"-----END CERTIFICATE-----\n";
// In case we ever need extra topics
const int ex_num_topics = 0;
const char* ex_topics[ex_num_topics];
//const int ex_num_topics = 1;
//const char* ex_topics[ex_num_topics] = {
// "/devices/my-device/tbd/#"
//};
credentials.h
#define WIFI_SSID "Taha"
#define WIFI_PASSWD "mkt193540"
#define PROJECT_ID "nomadic-armor-279716"
#define REGION "asia-east1"
#define REGISTRY "iot-sample-registry"
#define DEVICE "esp32newD"
esp32-mqtt.h
// This file contains static methods for API requests using Wifi / MQTT
#ifndef __ESP32_MQTT_H__
#define __ESP32_MQTT_H__
#include <Client.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <MQTT.h>
#include <CloudIoTCore.h>
#include <CloudIoTCoreMqtt.h>
#include "ciotc_config.h" // Update this file with your configuration
// The MQTT callback function for commands and configuration updates
// Place your message handler code here.
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
int ledonpos=payload.indexOf("ledon");
if (ledonpos != -1) {
// If yes, switch ON the ESP32 internal led
Serial.println("Switch led on");
digitalWrite(LED_BUILTIN, HIGH);
} else {
// If no, switch off the ESP32 internal led
Serial.println("Switch led off");
digitalWrite(LED_BUILTIN, LOW);
}
}
/////////////////////////////////////////////
// Initialize WiFi and MQTT for this board //
Client *netClient;
CloudIoTCoreDevice *device;
CloudIoTCoreMqtt *mqtt;
MQTTClient *mqttClient;
unsigned long iss = 0;
String jwt;
/*
///////////////////////////////
// Helpers specific to this board
///////////////////////////////
String getDefaultSensor() {
return "Wifi: " + String(WiFi.RSSI()) + "db";
}
*/
String getJwt() {
iss = time(nullptr);
Serial.println("Refreshing JWT");
jwt = device->createJWT(iss, jwt_exp_secs);
return jwt;
}
void setupWifi() {
Serial.println("Starting wifi");
WiFi.mode(WIFI_STA);
// WiFi.setSleep(false); // May help with disconnect? Seems to have been removed from WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
configTime(0, 0, ntp_primary, ntp_secondary);
Serial.println("Waiting on time sync...");
while (time(nullptr) < 1592336214) {
delay(10);
}
}
void connectWifi() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
}
void connect() {
connectWifi();
mqtt->mqttConnect();
}
void setupCloudIoT() {
device = new CloudIoTCoreDevice(
project_id, location, registry_id, device_id,
private_key_str);
setupWifi();
netClient = new WiFiClientSecure();
mqttClient = new MQTTClient(512);//1024
mqttClient->setOptions(180, true, 1000); // keepAlive, cleanSession, timeout
mqtt = new CloudIoTCoreMqtt(mqttClient, netClient, device);
mqtt->setUseLts(true);
mqtt->startMQTT();
}
#endif //__ESP32_MQTT_H__
this code suppose to communicate with cloud and receive "ledon" or "ledoff" and implement the command.
First post in here. I was trying the same thing here and just got comm between my ESP8266 and GCP. I've learned three major things:
ESP32 and ESP8266 share a few libraries that are not compatible. You need to select the right one.
On root_cert you need to put the RSA_key
On private key (on firmware) you put the EC_private, while in the IoT Core you put the ec_public.
Good luck.
I'm having some trouble while putting my ESP8266WebServer standard connection procedure to its own class. I'm not able to pass the object server to my handleRoot function via bind::std.... I have tried multiple approaches, but right now, nothing succeeds. So maybe you could help me. Right now, the code compiles, I have commented out the corresponding lines. But in order to peform some actions on a client request, I need to have access to the server class methods in the functions handleRoot and handleForm. Here are the corresponding sketches. Thank you for your help.
Arduino sketch:
#include "WiFiHandler.h"
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
WiFiHandler myWiFiHandler;
void setup(){
Serial.begin(115200);
myWiFiHandler.setupWiFi(server); // Setup WiFi
}
void loop(){
myWiFiHandler.clientHandler(server); //Handle client requests
}
Header file:
#ifndef WiFiHandler_h
#define WiFiHandler_h
#include <WiFiSetup.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "handleHTML.h"
class WiFiHandler
{
private:
WiFiSetup myWiFiSetup; // Create object myWiFiSetup
handleHTML myHTMLhandler; // Create object myHTMLHandler
char* _ssid;
char* _password;
void handleRoot();
void handleForm();
public:
WiFiHandler();
~WiFiHandler();
void setupWiFi(ESP8266WebServer&);
void clientHandler(ESP8266WebServer&);
};
#endif
Source file:
#include <WiFiSetup.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "handleHTML.h"
#include "Arduino.h"
#include "WiFiHandler.h"
WiFiHandler::WiFiHandler()
: _ssid(myWiFiSetup.ssid()), _password(myWiFiSetup.passcode())
{
}
WiFiHandler::~WiFiHandler(){/*Nothing to destruct*/}
void WiFiHandler::setupWiFi(ESP8266WebServer& server_)
{
WiFi.begin(_ssid, _password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Status: \n");
Serial.println(WiFi.status());
}
//If connection successful show IP address in serial monitor
Serial.print("Connected to ");
Serial.println(_ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server_.on("/", std::bind(&WiFiHandler::handleRoot, this)); // Which routine to handle at root location
server_.on("/", std::bind(&WiFiHandler::handleForm, this)); // Form action is handled here
server_.begin(); //Start server
Serial.println("HTTP server started");
}
void WiFiHandler::handleRoot()
{
Serial.print("WiFi-Request received");
// server_.send(200, "text/html", myHTMLhandler.getHTML()); //Send web page
}
void WiFiHandler::handleForm()
{
// String buttonState = server.arg("State");
Serial.print("State: ");
// Serial.println(buttonState);
String s = "<a href='/'> Go Back </a>";
// server_.send(200, "text/html", s); //Send web page
}
void WiFiHandler::clientHandler(ESP8266WebServer& server_)
{
server_.handleClient();
}
Changing the callback finally did the job.
server_.on("/", [&]() { handleRoot(server_); });
server_.on("/action_page", [&]() { handleForm(server_); });
and the corresponding function:
void WiFiHandler::handleRoot(ESP8266WebServer& server_)
{
server_.send(200, "text/html", myHTMLhandler.getHTML()); //Send web page
}
I do it the following way, the principle works by the way also with async webserver:
class MY_WebServer : public WebServer { // inherit from the lib class
public:
MY_WebServer();
void begin() {
MDNS.addService("http", "tcp", 80);
WebServer::begin();
}
void startWebServer(); // this function initializes wifi, all handlers and params
private:
void handleFileList();
//.... some 30 handlers ...
}
Works in a quite complex application stable for months.