Skip to content

Instantly share code, notes, and snippets.

@Tolerant
Last active December 9, 2024 16:47
Show Gist options
  • Save Tolerant/2d1af4c99995c6c7e348b874b73ecbb8 to your computer and use it in GitHub Desktop.
Save Tolerant/2d1af4c99995c6c7e348b874b73ecbb8 to your computer and use it in GitHub Desktop.
TinyGSM: Web Socket Client for Socket.io
/**************************************************************
* WebSocketClient SocketIO
* TinyGSM Getting Started guide:
* http://tiny.cc/tiny-gsm-readme
*
**************************************************************/
// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_ESP8266
// Increase RX buffer if needed
//#define TINY_GSM_RX_BUFFER 512
#include <TinyGsmClient.h>
#include <WebSocketsClient.h>
#include <Hash.h>
WebSocketsClient webSocket;
#define MESSAGE_INTERVAL 30000
#define HEARTBEAT_INTERVAL 25000
uint64_t messageTimestamp = 0;
uint64_t heartbeatTimestamp = 0;
bool isConnected = false;
// Uncomment this if you want to see all AT commands
//#define DUMP_AT_COMMANDS
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX
// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[] = "YourAPN";
const char user[] = "";
const char pass[] = "";
// Server details
const char resource[] = "/TinyGSM/logo.txt";
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
int waitInternet() {
SerialMon.print(F("Waiting for network..."));
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return 0;
}
SerialMon.println(" OK");
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
SerialMon.println(" fail");
delay(10000);
return 0;
}
SerialMon.println(" OK");
webSocket.beginSocketIO("192.168.0.123", 81);
//webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
webSocket.onEvent(webSocketEvent);
return 1;
}
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(115200);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println(F("Initializing modem..."));
modem.restart();
String modemInfo = modem.getModemInfo();
SerialMon.print(F("Modem: "));
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
}
void loop() {
while(!waitInternet()) {
delay(100);
}
webSocket.loop();
if(isConnected) {
uint64_t now = millis();
if(now - messageTimestamp > MESSAGE_INTERVAL) {
messageTimestamp = now;
// example socket.io message with type "messageType" and JSON payload
webSocket.sendTXT("42[\"messageType\",{\"greeting\":\"hello\"}]");
}
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
heartbeatTimestamp = now;
// socket.io heartbeat message
webSocket.sendTXT("2");
}
}
}
@bejosa
Copy link

bejosa commented Dec 9, 2024

00:00:00.001 HDW: ESP8266EX
00:00:00.033 CFG: Loaded from flash at FA, Count 308
00:00:00.036 FRC: Some settings have been reset (2)
00:00:00.044 Project Tasmota - Tasmota Version 14.2.0.4(tasmota)-2_7_7(2024-12-09T18:36:38)
00:00:05.663 PRO: Initializing modem...(Success)
00:00:44.392 PRO: Modem Info: ATI SIM800 R14.18
00:00:44.393 PRO: Overriding TCP KeepAlive to 120 secs
00:00:44.451 PRO: Waiting GSM network...
00:01:15.973 PRO: Success
00:01:15.973 PRO: SinricPro init
00:01:15.974 [SinricPro:add()]: Adding device with id "671d27268fce4464af0614a6".
00:01:15.975 PRO: SinricPro init is done...
00:01:15.980 [SinricPro:stop()
00:01:15.981 [WS-Client] no client
00:01:15.985 PRO: GSMClient set
00:01:16.000 CMD: wifi 0
00:01:16.004 RSL: RESULT = {"Wifi":"OFF","Mode":"11n"}
00:01:16.018 PRO: Loop Starting...
00:01:16.068 [SinricPro:Websocket]: Conneth{�to WebSocket Server (ws.sinric.pro)
00:01:16.069�R�K��Aɽ:Websocket]: headers: appkey:9f685fce-b81d �
-ac54-6f61db51df1c
deviceids:671d27268fce4464aS+6
restoredevicestates:true
ip:(IP unset)
mac:34:94:54:8F:9A:24
platform:ESP8266
SDKVersion:3.2.0
00:01:16.084 [WS-Client] Websocket Version: 2.6.1
00:0:1r���[WS-Client] Connecting!
00:01:18.366 [WS-Client] connection to ws.sinric.pro:80 Failed
00:01:34.132 [WS-Client] Connecting!
00:01:34.314 [WS-Client] connection to ws.sinric.pro:80 Failed
00:01:34.316 MQT: Attempting connection...
00:01:34.403 MQT: Connecting to APN: internet
00:01:39.631 MQT: GPRS connected
00:01:42.208 MQT: Connected
00:01:42.365 MQT: tele/Tasmota_8F9A24/LWT = Online (retained)

00:06:03.615 PRO: Check GPRS...
00:06:04.344 PRO: GPRS connected
00:06:06.655 [WS-Client] Connecting!
00:06:07.403 WSJi�����u connected to ws.sinric.pro:80.
00:06:07.04 [WS-Client][sendHeader] sending header...
00:06:07.405 [write] n: 408 t: 367517
00:06:08.033 [WS-Client][snHead]sending header... Done (628021us).
00:06:09.425 [WS-Client][hadleeder] RX: HTTP/1.1 101 Switching Protocols
00:06:09.860 [WS-Client][hndlHader] RX: Server: nginx
00:06:10.087 [WS-Client][hadS��(ɲWRX: Sec-WebSocket-Accept: KQISzLXTM4I9dzbe"�a-go=
00:0:0��[YM-Client][handleHeader] RX: Sec-WebSocket-Protocol: arduino
00:06:10.189 [WS][0][headrDne] Header Handling Done.
00:06:10.193 [SinricPro�W%���U�W��������ѕ�5
00:06:10.197 PRO: SinricPro Connected
0�Ҋ�r����[WS][0][handleWebsocketWaitFor] siz: csXsize: 0
00:06:10.433 [readCb] n: 2 t: 37054
000151�WS][0][handleWebsocketWaitFor][readCb] size'��z:1
00:06:10.519 [WS][0][handleWebsocket] ----- rad massage frame -------
00:06:10.521 [WS][0][handleWebocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCoe1
006:10.527 [WS][0][handleWebsocket] mask: 0 ayoaLn 24
00:06:10.533 [readCb] n: 24 t: 370645
0:06:10.536 [WS][0][handleWebsocket] text: {"timestp:1362423}
00:06:10.542 [SinricPro:Websocket]: ȕ��ٝdata
00:06:10.547 [SinricPro.handleReceiveQuue()]:1 message(s) in receiveQueue
00:06:10.554[nri,%.handleReceiveQueue()]: Signature is valid.�����ͥ�� message...
00:06:10.561 [SinricPro:extratTmesamp(): Got Timestamp 1733762423
00:06:20.328 MQT: Attempting connection...
00:06:21.059 MQT: GPRS connected

Tasmota Mqtt TinyGSM SIM800 Sinricpro websocket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment