forked fromamcewen/HttpClient
- Notifications
You must be signed in to change notification settings - Fork171
Add MKRGSM1400 Websocket example, issue #46#96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
fechidal89 wants to merge1 commit intoarduino-libraries:masterChoose a base branch fromfechidal89:fechidal89/add-gsm-examples
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletionsexamples/MKRGSM1400WebSocket/MKRGSM1400WebSocket.ino
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
MKRGSM1400 WebSocket client for ArduinoHttpClient library | ||
Connects to the Echo WebSocket server, and sends a hello | ||
message every 5 seconds. | ||
created 19 Dic 2020 | ||
by Fernando Hidalgo | ||
This example is in the public domain | ||
*/ | ||
#include <ArduinoHttpClient.h> | ||
#include <MKRGSM.h> | ||
#include "arduino_secrets.h" | ||
char PINNUMBER[] = SECRET_PINNUMBER; | ||
const char GPRS_APN[] = SECRET_GPRS_APN; | ||
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN; | ||
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD; | ||
char serverAddress[] = "echo.websocket.org"; // server address | ||
int port = 80; // To make a secure connection use 443 | ||
GSM gsmAccess; // pass true as param to enable debug mode: GSM gsmAccess(true); | ||
GPRS gprs; | ||
GSMClient gsmClient; // To make a secure connection use GSMSSLClient | ||
WebSocketClient client = WebSocketClient(gsmClient, serverAddress, port); | ||
int count = 0; | ||
void setup() { | ||
bool connected = false; | ||
Serial.begin(9600); | ||
while (!connected) { | ||
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) && | ||
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) { | ||
connected = true; | ||
} else { | ||
Serial.println("Not connected"); | ||
delay(1000); | ||
} | ||
} | ||
} | ||
void loop() { | ||
if (!client.connected()) { | ||
Serial.println("Starting WebSocket client"); | ||
client.begin(); | ||
} else { | ||
startEchoMessages(); | ||
} | ||
} | ||
void startEchoMessages() { | ||
sendMessage(); // send a message to receive an echo replay from the server | ||
while (client.connected()) { | ||
// check if a message is available to be received | ||
int messageSize = client.parseMessage(); | ||
if (messageSize > 0) { | ||
Serial.print("Received message: "); | ||
Serial.println(client.readString()); | ||
// increment count for next message | ||
count++; | ||
sendMessage(); | ||
} | ||
delay(5000); | ||
} | ||
Serial.println("Disconnected..."); | ||
} | ||
void sendMessage() { | ||
Serial.print("Sending: hello "); | ||
Serial.println(count); | ||
client.beginMessage(TYPE_TEXT); | ||
client.print("hello "); | ||
client.print(count); | ||
client.endMessage(); | ||
} |
4 changes: 4 additions & 0 deletionsexamples/MKRGSM1400WebSocket/arduino_secrets.h
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#define SECRET_PINNUMBER "" | ||
#define SECRET_GPRS_APN "" // replace your GPRS APN | ||
#define SECRET_GPRS_LOGIN "" // replace with your GPRS login | ||
#define SECRET_GPRS_PASSWORD "" // replace with your GPRS password |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.