Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromfechidal89:fechidal89/add-gsm-examples
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletionsexamples/MKRGSM1400WebSocket/MKRGSM1400WebSocket.ino
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp