Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2.  
  3. #include "arduino_secrets.h"
  4. #include <SPI.h>
  5. #include <WiFi101.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. int keyIndex = 0; // your network key Index number (needed only for WEP)
  11.  
  12. int status = WL_IDLE_STATUS;
  13. char server[] = "api.openweathermap.org"; // name address for Google (using DNS)
  14.  
  15. WiFiClient client;
  16.  
  17. void setup() {
  18. //Initialize serial and wait for port to open:
  19. Serial.begin(9600);
  20. while (!Serial) {
  21. ; // wait for serial port to connect. Needed for native USB port only
  22. }
  23.  
  24. // check for the presence of the shield:
  25. if (WiFi.status() == WL_NO_SHIELD) {
  26. Serial.println("WiFi shield not present");
  27. // don't continue:
  28. while (true);
  29. }
  30.  
  31. String fv = WiFi.firmwareVersion();
  32. if (fv != "1.1.0") {
  33. Serial.println("Please upgrade the firmware");
  34. }
  35.  
  36. // attempt to connect to Wifi network:
  37. while (status != WL_CONNECTED) {
  38. Serial.print("Attempting to connect to SSID: ");
  39. Serial.println(ssid);
  40. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  41. status = WiFi.begin(ssid, pass);
  42.  
  43. // wait 10 seconds for connection:
  44. delay(10000);
  45. }
  46. Serial.println("Connected to wifi");
  47. printWifiStatus();
  48.  
  49. Serial.println("\nStarting connection to server...");
  50. // if you get a connection, report back via serial:
  51. if (client.connect(server, 80)) {
  52. Serial.println("connected to server");
  53. // Make a HTTP request:
  54. client.println("GET /data/2.5/weather?q=Lahti&APPID={APPid}");
  55. client.println("Host: api.openweathermap.org");
  56. client.println("Connection: close");
  57. client.println();
  58. }
  59.  
  60. }
  61.  
  62. void loop() {
  63. // if there are incoming bytes available
  64. // from the server, read them and print them:
  65. while (client.available()) {
  66. char c = client.read();
  67. Serial.write(c);
  68. }
  69.  
  70. // if the server's disconnected, stop the client:
  71. if (!client.connected()) {
  72. Serial.println();
  73. Serial.println("disconnecting from server.");
  74. client.stop();
  75.  
  76. // do nothing forevermore:
  77. while (true){
  78. Serial.println("luuppaus...");
  79. delay(5000);
  80. };
  81. }
  82. }
  83.  
  84.  
  85. void printWifiStatus() {
  86. // print the SSID of the network you're attached to:
  87. Serial.print("SSID: ");
  88. Serial.println(WiFi.SSID());
  89.  
  90. // print your WiFi shield's IP address:
  91. IPAddress ip = WiFi.localIP();
  92. Serial.print("IP Address: ");
  93. Serial.println(ip);
  94.  
  95. // print the received signal strength:
  96. long rssi = WiFi.RSSI();
  97. Serial.print("signal strength (RSSI):");
  98. Serial.print(rssi);
  99. Serial.println(" dBm");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement