-
-
Save Tolerant/2d1af4c99995c6c7e348b874b73ecbb8 to your computer and use it in GitHub Desktop.
/************************************************************** | |
* 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"); | |
} | |
} | |
} |
#include <Hash.h> undefined
Just trying to figure out how this is working. Anyone tried it?
isConnected is defined as false and never becomes true.
client is defined as TinyGsmClient, but not used in code.
If this is with arduinoWebSockets,
webSocket.beginSocketIO by default goes to WiFi. How is the library changed to point to TinyGsm?
@shardul10 you got the working solution for this, websocket with Tinygsm
You are trying to connect to a websocket on a local LAN (192.168.0.123) over a WAN connection like GPRS?
Did anyone find a working solution for this ? I am trying to use sim800 with esp32 and connect to a server over websocket .
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
Hi dear friend, could you please help me, I try your code but the esp when goes to the websocket.loop it restarts