Connecting Arduino SocketIoClient to NextJs api route - next.js

I have a nextJs socket server in pages/api/socket hosted locally and my code is the same as what this article talks about.
My Arduino code is working in ESP8266 like this example from github
Basically, I emit a message event every 500 milliseconds from the Arduino loop and I want to console log that message when received by the nextJs api route but it does not worked for me.
I don't know the exact problem here, but when I tried this, a server using nodeJs and express and socket.io, it does work.
I am thinking that the problem is, in Arduino SocketIoClient, the websocket.begin("192.168.1.4", 3000) will connect to my http://localhost:3000 but my nextJs socket server lives in /api/socket so I added the url as the third parameter that looks like this, websocket.begin("192.168.1.4", 3000, "/api/socket"). But this does not work. NextJs cannot receive the connection from the Arduino.
Can anybody help or explain what is going on here?

Related

Problems making a HTTP request with SIMCOM module

For the past few weeks, i have been trying to make a HTTP request on a SIMCOM7080. I'm controlling this module via AT Commands. Every path that i try, ends with an ERROR. Does anyone have experience on getting this module to talk with for example Thingspeak? The link that i have tried is this one:
https://prod-162.westeurope.logic.azure.com/workflows/48fcd3ff69634babaa31ef51f89e2051/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=_4eWTUWPLqk5evTp1014kcntXff4akjlFpQJZo_5dFc
The purpose for this is to eventually connect this to an arduino via a UART connection, and displaying data on an IoT device.
Thanks in advance!
Every path with AT commands ends in an error. I have looked up all the documentation given by the manufacturer but it ends with ERROR.

Flask-MQTT disconnects after 'Socket error on client <unknown>' while running on uWSGI NGINX

I have a setup where I use Flask-MQTT to connect my python Flask API to a Mosquitto broker. Whenever I run the Flask API with the development server all is well. But whenever I spin it up for production (using wsgi+nginx), the connection with Mosquitto is made, but everytime i try to publish something i get the following error:
Socket error on client <unknown>, disconnecting.
My app.ini has the processes configured to 1 (processes = 1)
My mosquitto.config has the allow_anonymous flag set to trye (allow_anonymous true)
I can't really seem to figure out what I'm doing wrong here...
Update:
So what i think is happening is that the Flask-uwsgi application is trying to connect to mosquitto more than once. There is a master process that connects with Mosquitto on initialize. Then there is a second process that is being used whenever input is given on the Flask app. I'm not sure, but I think Mosquitto only wants one connection at the time, therefor erroring on the second. So now i either need to:
A) Configure Mosquitto in a way that it accepts multiple connection from the same device
B) Configure Flask in a way that wil only use one single process (configuring processes = 1 is not enough, it will still spawn two processes)
99% of the time, a "Socket error on client <unknown>" is an authentication error. I don't know Flask, so I don't know where to point you at, but something in your code is either trying to pass a username/password that is not defined to Mosquitto, or its trying a TLS connection with an cert that Mosquitto doesn't like.
Alright, it turns I could've read that the whole multiple processes wouldn't work from the start at the official Flask-MQTT documentation. It sais right there in think letters:
Flask-MQTT is currently not suitable for the use with multiple worker
instances.
So I looked at my uwsgi app.ini file again closely and actually the answer is quite simple. I turned out i had a like in there master = true.. after I removed that it works like a charm.

How to send HTTP requests with ESP8266 AT COMMAND?

I want to send HTTP requests to Firebase or Cloudant using AT commands. I'm able to connect to my router and ping to Google:
AT+PING="www.google.com"
+34
OK
For example, in Firebase:
AT+CIPMODE?
+CIPMODE:0
OK
AT+CIPMUX?
+CIPMUX:1
OK
AT+CIPSTART=1,"TCP","https://myapp.firebaseio.com/my_doc.json?mykey",80
ERROR
Or, for Cloudant:
AT+CIPSTART=3,"TCP","https://USERNAME-password#username-bluemix.cloudant.com/estado/doc_estados",8080
ERROR
Is there a way to see what error occurs?
I've seen several examples using Arduino, but I don't want use Arduino.

"iot_tls_connect L#143 TCP Connection Error" using basic AWS example with Raspberry Pi

I am very new to aws IOT. I am getting the error:
iot_tls_connect L#143 TCP Connection Error.
I am trying to use the sample aws c code. I have replaced the header config with the config for my account. but none of the sample example go beyond this error. I am using Raspberry Pi.
Sounds like you have the correct certs/keys in place. Make sure the host name is just .iot.us-west-2.amazonaws.com and not https://A<...>.iot.us-west-2.amazonaws.com if you copy-paste it from the console.

Remote server push notification to arduino (Ethernet)

I would want to send a message from the server actively, such as using UDP/TCPIP to a client using an arduino. It is known that this is possible if the user has port forward the specific port to the device on local network. However I wouldn't want to have the user to port forward manually, perhaps using another protocol, will this be possible?
1 Arduino Side
I think the closest you can get to this is opening a connection to the server from the arduino, then use available to wait for the server to stream some data to the arduino. Your code will be polling the open connection, but you are avoiding all the back and forth communications to open and close the connection, passing headers back and forth etc.
2 Server Side
This means the bulk of the work will be on the server side, where you will need to manage open connections so you can instantly write to them when a user triggers some event which requires a message to be pushed to the arduino. How to do this varies a bit depending on what type of server application you are running.
2.1 Node.js "walk-through" of main issues
In Node.js for example, you can res.write() on a connection, without closing it - this should give a similar effect as having an open serial connection to the arduino. That leaves you with the issue of managing the connection - should the server periodically check a database for messages for the arduino? That simply removes one link from the arduino -> server -> database polling link, so we should be able to do better.
We can attach a function triggered by the event of a message being added to the database. Node-orm2 is a database Object Relational Model driver for node.js, and it offers hooks such as afterSave and afterCreate which you can utilize for this type of thing. Depending on your application, you may be better off not using a database at all and simply using javascript objects.
The only remaining issue then, is: once the hook is activated, how do we get the correct connection into scope so we can write to it? Well you can save all the relevant data you have on the request to some global data structure, maybe a dictionary with an arduino ID as index, and in the triggered function you fetch all the data, i.e. the request context and you write to it!
See this blog post for a great example, including node.js code which manages open connections, closing them properly and clearing from memory on timeout etc.
3 Conclusion
I haven't tested this myself - but I plan to since I already have an existing application using arduino and node.js which is currently implemented using normal polling. Hopefully I will get around to it soon and return here with results.
Typically in long-polling (from what I've read) the connection is closed once data is sent back to the client (arduino), although I don't see why this would be necessary. I plan to try keeping the same connection open for multiple messages, only closing after a fixed time interval to re-establish the connection - and I hope to set this interval fairly high, 5-15 minutes maybe.
We use Pubnub to send notifications to a client web browser so a user can know immediately when they have received a "message" and stuff like that. It works great.
This seems to have the same constraints that you are looking at: No static IP, no port forwarding. User can theoretically just plug the thing in...
It looks like Pubnub has an Arduino library:
https://github.com/pubnub/arduino

Resources