Guest User

WifiSSL_Code

a guest
Aug 11th, 2023
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include "WiFiS3.h"
  2. #include "WiFiSSLClient.h"
  3. #include "IPAddress.h"
  4.  
  5. #include "arduino_secrets.h"
  6.  
  7. ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  8. char ssid[] = SECRET_SSID;  // your network SSID (name)
  9. char pass[] = SECRET_PASS;  // your network password (use for WPA, or use as key for WEP)
  10.  
  11. int status = WL_IDLE_STATUS;
  12. // if you don't want to use DNS (and reduce your sketch size)
  13. // use the numeric IP instead of the name for the server:
  14. //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
  15. char server[] = "api.telegram.org";  // name address for Google (using DNS)
  16.  
  17. // Initialize the Ethernet client library
  18. // with the IP address and port of the server
  19. // that you want to connect to (port 80 is default for HTTP):
  20. WiFiSSLClient client;
  21.  
  22. /* -------------------------------------------------------------------------- */
  23. void setup() {
  24.   /* -------------------------------------------------------------------------- */
  25.   //Initialize serial and wait for port to open:
  26.   Serial.begin(115200);
  27.   while (!Serial) {
  28.     ;  // wait for serial port to connect. Needed for native USB port only
  29.   }
  30.  
  31.   // check for the WiFi module:
  32.   if (WiFi.status() == WL_NO_MODULE) {
  33.     Serial.println("Communication with WiFi module failed!");
  34.     // don't continue
  35.     while (true)
  36.       ;
  37.   }
  38.  
  39.   String fv = WiFi.firmwareVersion();
  40.   if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
  41.     Serial.println("Please upgrade the firmware");
  42.   }
  43.  
  44.   // attempt to connect to WiFi network:
  45.   while (status != WL_CONNECTED) {
  46.     Serial.print("Attempting to connect to SSID: ");
  47.     Serial.println(ssid);
  48.     // Connect to WPA/WPA2 network.
  49.     status = WiFi.begin(ssid, pass);
  50.  
  51.     // wait 10 seconds for connection:
  52.     delay(10000);
  53.   }
  54.  
  55.   printWifiStatus();
  56.  
  57.   Serial.println("\nStarting connection to server...");
  58.   // if you get a connection, report back via serial:
  59.  
  60.   String getRequest = "/bot";
  61.   getRequest += BOT_TOKEN;
  62.   getRequest += "/sendMessage?chat_id=";
  63.   getRequest += CHAT_ID;
  64.   getRequest += "&text=ABC";
  65.  
  66.   if (client.connect(server, 443)) {
  67.     Serial.println("connected to server");
  68.     // Make a HTTP request:
  69.     client.println("GET /" + getRequest + " HTTP/1.1");
  70.     client.println("Host: api.telegram.org");
  71.     client.println("Connection: close");
  72.     client.println();
  73.   }
  74. }
  75.  
  76. /* just wrap the received data up to 80 columns in the serial print*/
  77. /* -------------------------------------------------------------------------- */
  78. void read_response() {
  79.   /* -------------------------------------------------------------------------- */
  80.   uint32_t received_data_num = 0;
  81.   while (client.available()) {
  82.     /* actual data reception */
  83.     char c = client.read();
  84.     /* print data to serial port */
  85.     Serial.print(c);
  86.     /* wrap data to 80 columns*/
  87.     received_data_num++;
  88.     if (received_data_num % 80 == 0) {
  89.       Serial.println();
  90.     }
  91.   }
  92. }
  93.  
  94. /* -------------------------------------------------------------------------- */
  95. void loop() {
  96.   /* -------------------------------------------------------------------------- */
  97.   read_response();
  98.  
  99.   // if the server's disconnected, stop the client:
  100.   if (!client.connected()) {
  101.     Serial.println();
  102.     Serial.println("disconnecting from server.");
  103.     client.stop();
  104.  
  105.     // do nothing forevermore:
  106.     while (true)
  107.       ;
  108.   }
  109. }
  110.  
  111. /* -------------------------------------------------------------------------- */
  112. void printWifiStatus() {
  113.   /* -------------------------------------------------------------------------- */
  114.   // print the SSID of the network you're attached to:
  115.   Serial.print("SSID: ");
  116.   Serial.println(WiFi.SSID());
  117.  
  118.   // print your board's IP address:
  119.   IPAddress ip = WiFi.localIP();
  120.   Serial.print("IP Address: ");
  121.   Serial.println(ip);
  122.  
  123.   // print the received signal strength:
  124.   long rssi = WiFi.RSSI();
  125.   Serial.print("signal strength (RSSI):");
  126.   Serial.print(rssi);
  127.   Serial.println(" dBm");
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment