Last active
August 25, 2024 20:59
-
-
Save Tolerant/2d1af4c99995c6c7e348b874b73ecbb8 to your computer and use it in GitHub Desktop.
TinyGSM: Web Socket Client for Socket.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************** | |
* 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"); | |
} | |
} | |
} |
Dear friend,I don’t find the solution
Best regards
El El sáb, 15 de jul. de 2023 a la(s) 00:31, Saim Shuja <
***@***.***> escribió:
… ***@***.**** commented on this gist.
------------------------------
Did anyone find a working solution for this ? I am trying to use sim800
with esp32 and connect to a server over websocket .
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/Tolerant/2d1af4c99995c6c7e348b874b73ecbb8#gistcomment-4629782>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA3ZORXLHC34EFTVOY6KSMTXQITLZBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DSMZXGU3TAMFHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Hi dear friend, could you please help me, I try your code but the esp when goes to the websocket.loop it restarts
Hello bhai have you find any solution for thia
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did anyone find a working solution for this ? I am trying to use sim800 with esp32 and connect to a server over websocket .