Hi everyone,
im starting a new topic here specific for semaphores to connect to MQTT and to wifi as the previous topic i was posting to was for an other thing.
I am trying to implant the code of an other very helpful member here (idohawalker) but im getting errors:
#include <ESP32Ping.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define WIFI_NETWORK "mywifi"
#define WIFI_PASSWORD "mypass"
#define WIFI_TIMEOUT_MS 30000 // 30 second WiFi connection timeout
#define WIFI_RECOVER_TIME_MS 10000 // Wait 10 seconds after a failed connection attempt
const char* mqtt_server = "myserver";
#define mqtt_port 111111
#define MQTT_USER "user1"
#define MQTT_PASSWORD "pass1"
#define MQTT_SERIAL_PUBLISH_CH "app/myuser1"
#define MQTT_SERIAL_RECEIVER_CH "app/myuser1"
WiFiClient wifiClient;
PubSubClient MQTT_client(wifiClient);
SemaphoreHandle_t MQTT_ConnectListen;
SemaphoreHandle_t InternetConnection;
SemaphoreHandle_t InternetStatus;
bool isonline=false;
void MQTT_callback(char* topic, byte *payload, unsigned int length) {
// Serial.println("-------new message from broker-----");
// Serial.print("channel:");
// Serial.println(topic);
Serial.print("data:");
//Serial.write(payload, length);
//Serial.println(payload);
Serial.println();
int i = 0;
char strPayload[300];
for ( i; i < length; i++)
{
strPayload[i] = ((char)payload[i]);
}
strPayload[i] = NULL;
MQTTparser(topic,strPayload);
}
void setup() {
Serial.begin(115200);
// Set time out for
Serial.setTimeout(500);
delay(5000);
MQTT_ConnectListen = xSemaphoreCreateBinary();
InternetConnection = xSemaphoreCreateBinary();
InternetStatus = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(InternetConnectionCode,"InternetConnection",5000,NULL,2,&InternetConnection,1);
xTaskCreatePinnedToCore(MQTT_ConnectListenCode,"MQTT_ConnectListen",30000,NULL,2,&MQTT_ConnectListen,1);
xTaskCreatePinnedToCore(InternetStatusCode,"InternetStatus",5000,NULL,2,&InternetStatus,1);
delay(2000);
}
void loop() {
}
void MQTT_ConnectListenCode( void * pvParameters ){
MQTT_client.setServer(mqtt_server, mqtt_port);
MQTT_client.setCallback(MQTT_callback);
for(;;){
if(isonline = false){
vTaskDelay(10000 / portTICK_PERIOD_MS);
continue;
}
if (!MQTT_client.connected()) {
// Loop until we're reconnected
while (!MQTT_client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "user1";
//clientId += String(random(0xffff), HEX);
// Attempt to connect
if (MQTT_client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) {
Serial.println("connected");
//vTaskDelay(1000/portTICK_PERIOD_MS);
//Once connected, publish an announcement...
// ... and resubscribe
MQTT_client.subscribe(MQTT_SERIAL_RECEIVER_CH,1);
} else {
Serial.print("failed, rc=");
Serial.print(MQTT_client.state());
Serial.println(" try again in 5 secondssss");
vTaskDelay(5000/portTICK_PERIOD_MS);
continue;
}
}
}
else{
xSemaphoreTake(MQTT_ConnectListen,portMAX_DELAY);
MQTT_client.loop();
xSemaphoreGive(MQTT_ConnectListen);
}
}
}
void InternetConnectionCode( void * pvParameters ){
int first=0;
for(;;){
if(first==0){
Serial.println("initiallising");
first=1;
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
if(WiFi.status() == WL_CONNECTED){
vTaskDelay(10000 / portTICK_PERIOD_MS);
continue;
}
Serial.println("[WIFI] Connecting");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);
unsigned long startAttemptTime = millis();
// Keep looping while we're not connected and haven't reached the timeout
while (WiFi.status() != WL_CONNECTED &&
millis() - startAttemptTime < WIFI_TIMEOUT_MS){}
// When we couldn't make a WiFi connection (or the timeout expired)
// sleep for a while and then retry.
if(WiFi.status() != WL_CONNECTED){
Serial.println("[WIFI] FAILED");
vTaskDelay(WIFI_RECOVER_TIME_MS / portTICK_PERIOD_MS);
continue;
}
}
}
void InternetStatusCode( void * pvParameters ){
for(;;){
if(Ping.ping("google.com")) {
isonline=true;
Serial.println("hi");
vTaskDelay(250/portTICK_PERIOD_MS);
continue;
}
else {
isonline=false;
Serial.println("Error connecting to wifi);
vTaskDelay(10000/portTICK_PERIOD_MS);
continue;
}
}
}
void MQTT_parser(char * ttopic, char* ppayload)
{
//print message just for test
Serial.print(ppayload);
}
Code was working untill i tried the semaphore things:
xSemaphoreTake(MQTT_ConnectListen,portMAX_DELAY);
MQTT_client.loop();
xSemaphoreGive(MQTT_ConnectListen);
instead of:
MQTT_client.loop();
error:
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
initiallising
Attempting MQTT connection...failed, rc=-2 try again in 5 secondssss
Error connecting to wifi
[WIFI] Connecting
Attempting MQTT connection...connected
/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c:1443 (xQueueGenericReceive)- assert failed!
abort() was called at PC 0x40088885 on core 1
Backtrace: 0x4008c434:0x3ffd0ae0 0x4008c665:0x3ffd0b00 0x40088885:0x3ffd0b20 0x400d137b:0x3ffd0b60 0x40088b7d:0x3ffd0ba0
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
What am i missing here? I only tried to use semaphores for first time and only in one place of the code