Problem connecting ESP-MQTT with TCP (ESP-IDF)
I have a problema that I have a correctly connection to my mobile hotspot wifi, but when I try to connect MQTT to upload some sensor data to my PC ip (I did it with mosquitto broker), I always have the same problem.
esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://192.168.166.66:1883",
//.host = "192.168.166.66",
//.port = 1883,
.buffer_size = 1024,
.client_id = "ESP-TFG-FRAN",
.lwt_qos = 1,
//.transport = MQTT_TRANSPORT_OVER_TCP,
.protocol_ver = MQTT_PROTOCOL_V_3_1_1,
};
client = esp_mqtt_client_init(&mqtt_cfg);
vTaskDelay(100/portTICK_RATE_MS);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
ret = esp_mqtt_client_start(client);
if(ret != ESP_OK) {
printf("ERROR in first connecting of MQTT: %x\n", ret);
esp_mqtt_client_reconnect(client);
}
Could someone help me to solve it?
This is my error code that I always have:
I will think it is probably a problem with the sdk and the idf.py menuconfig
or the TCP/IP protocol... but I can`t fine the solution of the problem...
1 answer
-
answered 2022-05-04 12:30
hardillb
The error means that the ESP could not connect to the machine with the broker. This could be for a number of reasons
- The broker host is not on the IP address you've given
- The WiFi Access point you are using is set up with client separation enabled, which means that it won't allow 2 connected client to talk to each other.
- If the Broker is not connected to the Mobile Access Point then you probably need to be using the public (static or have DynDNS enabled) address of your network and have Port forwarding enabled
do you know?
how many words do you know
See also questions close to this topic
-
How do I manipulate the contents of a stat() struct?
I have a stat struct, and I'm looking for a way to get data out of it to be manipulated. The program will successfully run and print the desired st_mtime value, but including either of the "seg-fault" lines below causes a segmentation fault in runtime.
struct stat buf; time_t time_m; time_t sys_time = time(0); if(stat(sub_dirp->d_name,&buf)==0) { //time_m = buf.st_mtime; //seg-fault //double since_last = (difftime(sys_time, buf.st_mtime)/60); //seg-fault printf("%d ", (int)buf.st_mtime); //This works. }
Both lines are attempting to manipulate the
buf.st_mtime
value in some way.I've had a hard time finding any examples of the usage of stat() that do anything other than print its contents, which makes me wonder if it's even possible.
So my question is, if it is possible, what am I missing?
P.S. I do wish to keep
st_mtime
in the Unix timestamp format to make it easier to manipulate.Edit: After realizing that
st_mtime
is itself its own struct (timespec), how can I access thest_mtime.tv_sec
member?The compiler doesn't like
buf.st_mtime.tv_sec
one bit. -
What's purpose of __linecapp in getline, when it'll auto resize?
It's all about second parameter of getline in stdio.h,
I'll name it 'n' or '__linecapp' below.
According to the document:
If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary.
It'll automatically update line capacity, then why should we input __linecapp?
P.S Someone ask before, but discussion didn't explain when we need it, or how to make it useful.
-
Linked List Deletion function causing program to crash *wrong pointers?*
In my program when a username is following another one and also has posted a "tweet" which is a string in an attempt to delete the followed username it breaks the program. I believe this is the code snippet in question. If anyone could help me identify the issue I would be thankful as I am learning the usage of linked lists for scale able projects. Thanks
void deleteAccount(accountNodePtr *startPtr, accountNodePtr *curAcPtr, tweetsNodePtr *startTwtPtr){ accountNodePtr acLoopPtr; accountNodePtr tempCur = *curAcPtr; followNodePtr followingPtr = tempCur->followingPtr; followNodePtr tempPtr; followNodePtr tempPtr2; tweetsNodePtr iterTwtPtr; iterTwtPtr = *startTwtPtr; *below here in question* tempCur = *curAcPtr; while (tempCur->followersPtr!=NULL){ acLoopPtr = *startPtr; while (strcmp(acLoopPtr->username, tempCur->followersPtr->username)!=0){ acLoopPtr=acLoopPtr->nextPtr; } if (strcmp(acLoopPtr->followingPtr->username, tempCur->username)==0){ tempPtr=acLoopPtr->followingPtr->nextPtr; free(acLoopPtr->followingPtr); acLoopPtr->followingPtr=tempPtr; }else{ tempPtr=acLoopPtr->followingPtr; while(strcmp(tempPtr->nextPtr->username, tempCur->username)!=0){ tempPtr=tempPtr->nextPtr; } tempPtr2=tempPtr->nextPtr->nextPtr; free(tempPtr->nextPtr); tempPtr->nextPtr=tempPtr2; } tempPtr = tempCur->followersPtr->nextPtr; free(tempCur->followersPtr); tempCur->followersPtr=tempPtr; }
This is the structure
typedef struct followsNode { char username[MAX_USERNAME]; struct followsNode *nextPtr; } followNode; typedef struct accountsNode { char username[MAX_USERNAME]; struct followsNode *followersPtr; struct followsNode *followingPtr; struct accountsNode *nextPtr; } accountNode; typedef followNode *followNodePtr; typedef accountNode *accountNodePtr;
-
How to run FreeRTOS + Lwip application on EK-TM4C1294XL launchpad featuring a TM4C1294NCPDTI microcontroller device?
I have a EK-TM4C1294XL launchpad with a TM4C1294NCPDTI microcontroller device.I want to run a FreeRTOS + Lwip application on the board. The examples provided by TI has lwip example but it uses Tiva-OS and I want to use FreeRTOS. Can you provide any resources on how to run a FreeRTOS+Lwip on EK-TM4C1294XL microcontroller.
-
Platformio: teensy platform error, wiring.h
I'm trying to compile my project in platformio. I'm using teensy 4.1 board. Everything is fine besides built-in package wiring.h. I get error:
C:\Users\kubaj\.platformio\packages\framework-arduinoteensy\cores\teensy4/wiring.h:123:12: error: expected identifier before numeric constant #define PI 3.1415926535897932384626433832795 ^
as it's built-in I didn't change anything, but still getting the errors. Please give me a clue where to look for my mistakes.
-
PyFtdi Value Error 'No backend available' every now and then
I'm using pyftdi (https://eblot.github.io/pyftdi/) to handle a FTDI ft4232h. Everything usually works fine but every now and then I come across a ValueError saying 'No backend available'.
Is there any solution regarding that issue? I tried to copy the libusb-1.0.dll to C:\Windows32 as well as to C:\Windows\SysWOW64. I also tried reinstalling the ftdi driver with zadig as recommended.
Nothing worked so far.
NOTE: I'm bound to windows.
-
Method to predict outcome with percentage
in my setup i got 4 sensors labled a,b,c and d each sensor is receiving a signal from a beacon via MQTT. Depending on the signalstrength i want to predict the position of the beacon.
By now i used a logit model for each position to check if my beacon is at a position e.g.
- logit(position_1~a+b+c+d)
- logit(position_2~a+b+c+d)
- logit(position_3~a+b+c+d)
This only returns true or false for each logit, this seems to work BUT is there a method that takes the sensordata and returns a probability for each postion?
e.g. f(result=a,b,c,d) and returns a list of:
[
- position_1:90%,
- position_2:20%,
- position_3:0%
]
I'm pretty sure there is a method but i couldn't find it
my example data:
a b c d result 7 10 9 12 position_1 45 47 43 42 position_2 95 96 98 98 position_3 Also: I checked a CNN for image classification which retruns probabilities for each given image, can i reuse such a CNN for my case or is it stric to images classification
-
Service info.mqtt.android.service.MqttService has > leaked IntentReceiver
I keep having this error log when ever i toggle off a switch in my app.
E/ActivityThread: Service info.mqtt.android.service.MqttService has leaked IntentReceiver info.mqtt.android.service.ping.AlarmPingSender$AlarmReceiver@9cd447c that was originally registered here. Are you missing a call to unregisterReceiver()? android.app.IntentReceiverLeaked: Service info.mqtt.android.service.MqttService has leaked IntentReceiver info.mqtt.android.service.ping.AlarmPingSender$AlarmReceiver@9cd447c that was originally registered here. Are you missing a call to unregisterReceiver()? at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:1724) at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1462) at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1684) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1650) at android.app.ContextImpl.registerReceiver(ContextImpl.java:1638) at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:686) at info.mqtt.android.service.ping.AlarmPingSender.start(AlarmPingSender.kt:58) at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214) at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050) at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151) at java.lang.Thread.run(Thread.java:923)
The service is declared in the manifest file as follows:
<receiver android:name=".FirebaseNotification.MqttReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name="info.mqtt.android.service.MqttAndroidClient" android:exported="false" android:permission="android.permission.BIND_JOB_SERVICE" tools:ignore="Instantiatable" />
-
Python FastAPI backend get request path operation function access data saved outside of the function
I am quite new to FastAPI, so I have a question if it is possible to access data within the path operation function, which(data) is stored/generated outside of that function.
Generally, API endpoints look like this:
@app.get('/root'): def some_function(params): get_data() # data outside of this function .... (some processing) return data
The system, I'm trying to make, consists of two independently operating scripts. And they communicate using MQTT Broker. So, I intend to return data(received from other script) and return it through API endpoint.
Any help would be appreciated.
-
Stumped. Bool values from class objects are returning ascending integers?
Lot of code to post because I can't really tell where the issue is. I am trying to run this on an ESP32 chip and am writing the code in vscode with platformio.
The function
byte _previousPoint(){ for (byte point = 0; point < maxPoints; point ++){ //loop through active points. if (_points[point + 1].getActive() != 1){ return point; } else { long t1 = point_seconds(point); long t2 = point_seconds(point + 1); if (t1 <= currentTime){ //If current time is after this point. if (t2 > currentTime){ //If current time is less than the next point (it is sandwiched). return point; } } } } return 0; }
Is not behaving as I expect. And it seems the cause is that the function
bool getActive(){ return _active; }
under the class point is returning not a bool but ascending numbers for each channel. point.getActive() should return a bool value, and that bool value should default to 0 (as it is set in the class definition), which is why this makes no sense. The point that is returning the erroneous values is the last one in the _points[maxPoints] array for each channel, and that value seems to match the channel numbers (for red it is 0, for green it is 1, for blue it is 2, etc.)
If I print the value _active upon running the point.setActive() function it comes out correctly. So something is wrong with "getting" it later.
Here is full code below. Let me know if you need some clarification because I know it's a lot. And thanks for anybody patient enough to help.
#include <Arduino.h> long currentTime; long lastUp; byte totalChannels = 4; const byte maxPoints = 3; class point { public: point(){ } void clear(){ _active = 0; _day = 0; _hour = 0; _minute = 0; _second = 0; _intensity = 0; _mode = 0; } bool getActive(){ return _active; } uint getDay(){ return _day; } byte getHour(){ return _hour; } byte getMinute(){ return _minute; } byte getSecond(){ return _second; } int getIntensity(){ return _intensity; } byte getMode(){ return _mode; } void setActive(bool active){ _active = active; } void setDay(uint day){ _day = day; } void setHour(byte hour){ _hour = hour; } void setMinute(byte minute){ _minute = minute; } void setSecond(byte second){ _second = second; } void setIntensity(byte intensity){ _intensity = intensity; } void setMode(byte mode){ _mode = mode; } private: bool _active = 0; uint _day = 0; byte _hour = 0; byte _minute = 0; byte _second = 0; int _intensity = 0; byte _mode = 0; }; class channel { public: channel(String color, byte pin){ this->_color = color; this->_pin = pin; init(); }; void setpoint(byte row, point &newPoint, uint day, byte hour, byte minute, byte second, byte intensity, byte mode){ //edits points (for debug) newPoint.setDay(day); newPoint.setHour(hour); newPoint.setMinute(minute); newPoint.setSecond(second); newPoint.setIntensity(intensity); newPoint.setMode(mode); newPoint.setActive(1); _points[row] = newPoint; } bool getPoint(byte point){ return _points[point + 1].getActive(); } void clearAllPoints(){ for (byte point = 0; point < maxPoints; point ++ ){ _points[point].clear(); } } void setPin(byte pin){ _pin = pin; } byte getPin(){ return _pin; } uint getIntensity(){ byte point1 = _previousPoint(); byte point2 = _nextPoint(point1); byte fade_mode = _points[point1].getMode(); uint intensity; if (point2 != 0){ if (fade_mode == 0){ intensity = _interpolate_lin(point1, point2); } else if (fade_mode == 1){ intensity = _interpolate_sin(point1, point2); } } else if (point2 != 1){ if (fade_mode == 0){ intensity = _interpolate_lin(point1, point2); } else if (fade_mode == 1){ intensity = _interpolate_sin(point1, point2); } } return intensity; } private: //class attributes point _points[maxPoints]; //points maximum of 64 points per channel. byte _pin; //PWM pin output for channel String _color; //LED color float _interpolate_lin(byte point1, byte point2){ float idiff = _points[point2].getIntensity() - _points[point1].getIntensity(); float tdiff = point_seconds(point2) - point_seconds(point1); float m; if (tdiff != 0){ m = idiff / tdiff; } else{ m = 0; } float t = currentTime - point_seconds(point1); float b = _points[point1].getIntensity(); //linear equation result float i = (m * t) + b; return i; } float _interpolate_sin(byte point1, byte point2){ float amplitude = _points[point2].getIntensity() - _points[point1].getIntensity(); float tdiff = point_seconds(point2) - point_seconds(point1); float a = (-0.5 * amplitude); float b = (2 * PI) / (2 * tdiff); float t = (currentTime - point_seconds(point1)); float d = 0.5 * abs(amplitude); //cosine equation result float i = (a * cos(b * t)) + d; return i; } byte _previousPoint(){ for (byte point = 0; point < maxPoints; point ++){ //loop through active points. if (_points[point + 1].getActive() != 1){ return point; } else { long t1 = point_seconds(point); long t2 = point_seconds(point + 1); if (t1 <= currentTime){ //If current time is after this point. if (t2 > currentTime){ //If current time is less than the next point (it is sandwiched). return point; } } } } return 0; } byte _nextPoint(byte point){ if (_points[point + 1].getActive() != 1){ //if next point is inactive, previous is last in cycle. Next point is 0. return 0; } else if (_points[point + 1].getActive() == 1){ //if next point is active, return it as _nextPoint. return point + 1; } return 0; } long point_seconds(byte point){ return ((_points[point].getHour() * 3600) + (_points[point].getMinute() * 60) + _points[point].getSecond()); } }; //declaring channels and initializing channel array. channel red("red", 0); channel green("green", 1); channel blue("blue", 2); channel royal("royal blue", 3); channel *channels[] = { &red, &green, &blue, &royal }; void setIntensities(){ for (byte ch = 0; ch < totalChannels; ch ++){ channel ledChannel = *channels[ch]; byte intensity = ledChannel.getIntensity(); ledcWrite(ledChannel.getPin(), intensity); } } void setup() { Serial.begin(9600); //set up LED outputs ledcAttachPin(12, 0); ledcAttachPin(13, 1); ledcAttachPin(16, 2); ledcSetup(0, 1000, 8); ledcSetup(1, 1000, 8); ledcSetup(2, 1000, 8); //clear all points for (byte ch = 0; ch < totalChannels; ch++){ channel ledChannel = *channels[ch]; ledChannel.clearAllPoints(); } //Set points for testing purposes point red1; point red2; point red3; point green1; point green2; point green3; point blue1; point blue2; point blue3; point royal1; point royal2; point royal3; red.setpoint(0, red1, 0, 0, 0, 0, 0, 0); red.setpoint(1, red2, 0, 0, 0, 10, 255, 0); red.setpoint(2, red3, 0, 0, 0, 20, 0, 0); green.setpoint(0, green1, 0, 0, 0, 10, 0, 0); green.setpoint(1, green2, 0, 0, 0, 20, 255, 0); green.setpoint(2, green3, 0, 0, 0, 30, 0, 0); blue.setpoint(0, blue1, 0, 0, 0, 20, 0, 0); blue.setpoint(1, blue2, 0, 0, 0, 30, 255, 0); blue.setpoint(2, blue3, 0, 0, 0, 40, 0, 0); royal.setpoint(0, royal1, 0, 0, 0, 30, 0, 0); royal.setpoint(1, royal2, 0, 0, 0, 40, 255, 0); royal.setpoint(2, royal3, 0, 0, 0, 50, 0, 0); Serial.print("red: "); // <----------These are to debug. The channels are returning ascending bool values for point 3 (which should all be 0) for (byte point = 0; point < 3; point++){ Serial.print(red.getPoint(point)); } Serial.print(" green: "); for (byte point = 0; point < 3; point++){ Serial.print(green.getPoint(point)); } Serial.print(" blue: "); for (byte point = 0; point < 3; point++){ Serial.print(blue.getPoint(point)); } Serial.print(" royal: "); for (byte point = 0; point < 3; point++){ Serial.print(royal.getPoint(point)); } } void loop() { currentTime = millis() / 1000; if (currentTime > lastUp){ setIntensities(); lastUp = currentTime; } }
-
error: no matching function for call to 'MQTTClient::MQTTClient(int)' MQTTClient client = MQTTClient(256);
I'm working on an IoT project. ESP32 with AWS IoT Core.
I followed https://catalog.us-east-1.prod.workshops.aws/workshops/5b127b2f-f879-48b9-9dd0-35aff98c7bbc/en-US/module3/configure-esp32 for doing my project, But I received an Error message.
"LampProject:33:35: error: no matching function for call to 'MQTTClient::MQTTClient(int)' MQTTClient client = MQTTClient(256); "
-
Adding wifi SSID and PASSWORD to sdkconfig
I'm creating a wifi application using ESP-IDF based on the examples but the example I'm using has options for setting the SSID and PASSWORD under:
Example Connection Configuration
when using
idf.py menuconfig
and which are used byCONFIG_EXAMPLE_WIFI_SSID
andCONFIG_EXAMPLE_WIFI_PASSWORD
in the code.I want to set SSID and PASSWORD in my own project without using this in
CMakeLists.txt
:set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
without the above there isn't a way to define SSID and PASSWORD. They appear in:
build/config/kconfig_menus.json
but I can't seem to keep this from being overwritten in my own project.
sdkconfig
states it should not be edited.Is there a best practice for using
idf.py menuconfig
? Or do most people just usesdkconfig.defaults
and not useidf.py menuconfig
? -
Getting DHCP options using ESP32
I've dealing with an ESP32 module (ESP-IDF) trying to connect via Wifi and ethernet to a dhcp server. The point is, I'm trying to get the DHCP options, but I failed...
I tried using LWIP and also ESP-NETIF. (I have also to guess whats XXXX and YYYY)
// Create an esp_netif pointer to store current interface esp_netif_t* ifscan = esp_netif_next(NULL); // Stores the unique interface descriptor, such as "PP1", etc char ifdesc[7]; ifdesc[6] = 0; // Ensure null terminated string uint32_t value = 0; while (ifscan != NULL) { esp_netif_get_netif_impl_name(ifscan, ifdesc); Serial.printf("IF NAME: %s\n", ifdesc); esp_err_t code = esp_netif_dhcpc_option(ifscan, ESP_NETIF_OP_GET, ESP_NETIF_DOMAIN_NAME_SERVER, XXXX, YYYY); Serial.printf("RES: %s - OPTION: %s\n", esp_err_to_name(code), XXXXX); // Get the next interface ifscan = esp_netif_next(ifscan); } Serial.printf("Done listing network interfaces");
Does anyone have an example of sourcecode showing how to get the options after connecting?
Thank you in advance.
-
Micropython How to convert mp_obj_t to bytes
In micropython you convert from
mp_obj_t
toint
usingmp_obj_get_int
How can I convert from
mp_obj
touint8_t
?