Created
November 25, 2019 22:07
-
-
Save oriolrius/cfb8dbebe1732a218b96a3af874ddfe8 to your computer and use it in GitHub Desktop.
Exemple inspirat en un company, PLA5 - echo_client.ino
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
#include <SPI.h> | |
#include <WiFi101.h> | |
#include "arduino_secrets.h" | |
WiFiClient client; | |
char ssid[] = SECRET_SSID; | |
char pass[] = SECRET_PASS; | |
int status = WL_IDLE_STATUS; | |
#define PORT 7 | |
IPAddress SERVER(10,2,0,42); | |
const int TIME_OUT = 10000; | |
const char *msg = "hello world"; | |
void setup() { | |
Serial.begin(9600); | |
if (WiFi.status() == WL_NO_SHIELD) { | |
printWifiStatus(WiFi.status()); | |
while (true); | |
} | |
} | |
void loop() { | |
wifiMaintainConnection(); | |
if (client.connect(SERVER, PORT)) { | |
Serial.println("TCP connected"); | |
Serial.print("Send message: "); | |
Serial.println(msg); | |
client.write(msg); | |
Serial.print("Received message: "); | |
int timeOut = millis() + TIME_OUT; | |
while (client.available()||millis()<timeOut){ | |
if (client.available()){ | |
char c = client.read(); | |
Serial.print(c); | |
} | |
} | |
Serial.println(); | |
Serial.println("Disconnecting."); | |
client.stop(); | |
} else{ | |
Serial.println("Error connecting to the server"); | |
} | |
delay(10000); | |
} | |
void wifiMaintainConnection(){ | |
// attempt to connect to WiFi network if it's not connected: | |
if(WiFi.status() != WL_CONNECTED) { | |
printWifiStatus(WiFi.status()); | |
Serial.print("Attempting to connect to WPA SSID: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() == WL_IDLE_STATUS); | |
printWifiStatus(WiFi.status()); | |
} | |
} | |
void printWifiIP() { | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
} | |
void printCurrentNet() { | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.println(rssi); | |
printWifiIP(); | |
} | |
void printWifiStatus(int Status) { | |
switch (Status) { | |
case WL_CONNECTED: | |
Serial.println("Connected to a WiFi network"); | |
printCurrentNet(); | |
break; | |
case WL_NO_SHIELD: | |
Serial.println("No wifi shield present"); | |
break; | |
case WL_IDLE_STATUS: | |
Serial.println("Connecting to a WiFi network"); | |
break; | |
case WL_NO_SSID_AVAIL: | |
Serial.println("No SSID are avaible"); | |
break; | |
case WL_SCAN_COMPLETED: | |
Serial.println("Scan completed"); | |
break; | |
case WL_CONNECT_FAILED: | |
Serial.println("Connection failed"); | |
break; | |
case WL_CONNECTION_LOST: | |
Serial.println("Connection lost"); | |
break; | |
case WL_DISCONNECTED: | |
Serial.println("Disconnected network"); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment