arduinoWebSockets icon indicating copy to clipboard operation
arduinoWebSockets copied to clipboard

Using TinyGSM with WebSockets

Open x0ryuu opened this issue 8 years ago • 11 comments

Hello @Links2004 :)!

I would like using Ai Thinker A6 (GSM/GPRS) with this library instead of ESP8266 wifi client.

I can create a class with methods called equal as "wifi client class" using A6 module, but I don't know where I can insert my own class and using it with your library.

Could you give me a clue about it, please?

Thanks! Your library is really awesome!

x0ryuu avatar May 27 '17 20:05 x0ryuu

at first you need to add your CPU here (if you not use an all ready existing one an AVR, STM or ESP for example): https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L55 set the options based on what make the most sens for your hardware.

next define a Network stack:

#define NETWORK_AI_A6_GSM        (4)

https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L69

then auto select it here: https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L79 based on a CPU / BOARD define form the compiler / IDE.

then include the needed header files and define the TCP Client and Server class:

#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_AI_A6_GSM)

#include <UIPEthernet.h>
#define WEBSOCKETS_NETWORK_CLASS UIPClient
#define WEBSOCKETS_NETWORK_SERVER_CLASS UIPServer

https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L139

as long your class has the same API then all the Network stuff in Arduino ( Client/Stream and Server class ) it will work fine.

Links2004 avatar May 28 '17 09:05 Links2004

Thanks for answering me @Links2004 :)!!

But I think that I explains me wrong, I go again.

No, I do not want to put this library inside the A6, it's a bit different, I currently have an ESP8266 which communicates via UART (Rx, Tx) with an Ai Thinker GSM / GPRS A6 module, the WebSockets library will be within ESP8266.

The A6 module uses AT commands to make HTTP requests using TCP, I created a client class that has methods that use these AT commands to make the requests, what I want is for the WebSockets library which is within the ESP8266 to use my client class which implements AT commands of the A6 module, in other words, the WebSockets library will never be inside the A6 module, it will be inside the ESP8266 but I want it use my class that communicates with the A6 module to make the requests instead of the client class that comes with the ESP8266 module inside its source code.

For example, here : https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.cpp#L119

You use a internal client class from ESP8266, is that right?

I want to change this and using my own client class with A6 comunication.

Thanks!

x0ryuu avatar May 28 '17 20:05 x0ryuu

the process is the same ;)

line 199 you linked is exklusive for the SSL on the ESP the generic client part is done with:

_client.tcp = new WEBSOCKETS_NETWORK_CLASS();

https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.cpp#L130

which class will be used is controlled via the defines.

for the network type ESP8266 they are here: https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L108-L122

skip the new CPU part from my first post, in your case you only need a new "Network type" its possible to combine different network type with different platforms, for example ESP8266 CPU with NETWORK_ENC28J60 will work fine. https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L77-L86

if the A6 can handle all requirements via the AT command interface is the bigger question.

  • specific HTTP headers
  • connection need to Keep open and data is flowing in both directions is possible at any time (not something a normal HTTP client implementation is keepable of)
  • binary data in the WS Protokoll ( AT / ASCII and binary normal not work well together, is possible to do but only if the A6 engineers have do multible thinks in the Protokoll the right way, hope dies at last. )

if you can get raw TCP access to the data stream on the A6 think looking good, but when the HTTP stack is build in the A6 I dont think this is possible.

Links2004 avatar May 28 '17 21:05 Links2004

I see..

A6 module is able to send specific HTTP headers but it can not keep open a connection and you can not access to HTTP stack because it is build inside...

So I guess it is impossible what I want!

So, do you know any solution to use GSM/GPRS with WebSockets instead of a wifi connection?

x0ryuu avatar May 28 '17 22:05 x0ryuu

currently I am not aware of a GSM module that will handle that, but I never did deep researche for it.

if your project not need to run a long time with battery or you have access to line power, may simple use a cheap and/or old Android Phone and open an wifi hotspot with it.

if this is not an option you need to find a module where you can open a TCP connection to a server and then send and receive data from it (basic TCP client stuff)

or a GSM module where the firmware is open.

Links2004 avatar May 29 '17 17:05 Links2004

Hello @Links2004 I'm using TinyGSM library. kindly suggest how I can integrate it with WebSocket library.

SwapB5632 avatar Sep 05 '24 07:09 SwapB5632

It may is possible since TinyGSM Implements a TCP client (TinyGsmClient) the steps are as stated in this ticket.

In short a new NETWORK stack definition for TinyGSM is needed. https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSockets.h#L137

but I can not really Implement this since I have no way to test it since I do not own any TinyGSM compatible hardware.

Links2004 avatar Sep 05 '24 10:09 Links2004

Hello @Links2004 I'm followed your steps for ESp32. having the confusion in line number 79. (What is auto Select ) image

Kindly guide us for implementing GSM Stack. waiting for your response.

SwapB5632 avatar Sep 08 '24 04:09 SwapB5632

Here’s an example changes I did for Uno R4 WiFi support. You just have to include the correct headers do something similar for GSM https://github.com/Links2004/arduinoWebSockets/pull/894/files

kakopappa avatar Sep 08 '24 05:09 kakopappa

there is no need for that since the the GSM module is external to the µC used, the defined need the match the CPU used. for example TinyGSM can be used with ESP, RP, SAM and many other µC's, so the arduino variant detection is independent from the network stack in this case.

In short for TinyGSM only a new network type is needed.

side note: no auto detection is possible since its a the GSM module is external to the µC. the only way to activate the network type is by setting the WEBSOCKETS_NETWORK_TYPE define global via the IDE / compiler

Links2004 avatar Sep 08 '24 08:09 Links2004

Hi @Links2004 , I successfully followed your instructions in this post to integrate my A7670 modem (supported by TinyGsm as SIM7600) with ESP32 and edited WebSockets.h to only work with unencrypted connection since A7670 doesn't support TinyGsmClientSecure.

  • I added #define NETWORK_A7670_GSM (4)
  • Edited this section:
 #elif defined(ESP32)
#define WEBSOCKETS_NETWORK_TYPE NETWORK_A7670_GSM
//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32
// #define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP32_ETH
  • finally created a new section :
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_A7670_GSM)
#define TINY_GSM_MODEM_SIM7600
#include <TinyGsmClient.h>
#define WEBSOCKETS_NETWORK_CLASS TinyGsmClient

Everything looks just great but when I try to do begin I can't get DNS to work

[ 45507][E][WiFiGeneric.cpp:1583] hostByName(): DNS Failed for --myhostname--

Here is the standard piece of code I run to connect to a working WebSocket server:

    webSocket.begin(<myhostname>, 80, websocket_path);
    webSocket.onEvent(webSocketEvent);
    webSocket.setReconnectInterval(5000); // Optional: Set internal reconnection interval

Why WifiGeneric.cpp is trying to resolve DNS instead of TinyGsmClient I used as WEBOSCKETS_NETWORK_CLASS? Should I fix another part of your library to let DNS work with TinyGsm?

Any help would be appreciated.

giuseppezicaffe avatar May 11 '25 09:05 giuseppezicaffe