Advertisement
Guest User

with pubnub

a guest
Aug 13th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. /*
  2.   Repeating WiFi Web Client
  3.  
  4.  This sketch connects to a a web server and makes a request
  5.  using an Arduino WiFi shield.
  6.  
  7.  Circuit:
  8.  * WiFi shield attached to pins SPI pins and pin 7
  9.  
  10.  created 23 April 2012
  11.  modified 31 May 2012
  12.  by Tom Igoe
  13.  modified 13 Jan 2014
  14.  by Federico Vanzati
  15.  
  16.  http://arduino.cc/en/Tutorial/WiFiWebClientRepeating
  17.  This code is in the public domain.
  18.  */
  19.  
  20. #include <SPI.h>
  21. #include <WiFi101.h>
  22.  
  23. char ssid[] = "TP-LINK_8AC0"; //  your network SSID (name)
  24. char pass[] = "37687281";    // your network password (use for WPA, or use as key for WEP)
  25. int keyIndex = 0;            // your network key Index number (needed only for WEP)
  26.  
  27. int status = WL_IDLE_STATUS;
  28.  
  29. // Initialize the WiFi client library
  30. WiFiClient client;
  31.  
  32. // server address:
  33. char server[] = "pubsub.pubnub.com";
  34. //IPAddress server(64,131,82,241);
  35.  
  36. unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
  37. const unsigned long postingInterval = 4L * 1000L; // delay between updates, in milliseconds
  38.  
  39. void setup() {
  40.   //Initialize serial and wait for port to open:
  41.   Serial.begin(9600);
  42.   while (!Serial) {
  43.     ; // wait for serial port to connect. Needed for native USB port only
  44.   }
  45.  
  46.   // check for the presence of the shield:
  47.   if (WiFi.status() == WL_NO_SHIELD) {
  48.     Serial.println("WiFi shield not present");
  49.     // don't continue:
  50.     while (true);
  51.   }
  52.  
  53.   // attempt to connect to WiFi network:
  54.   while ( status != WL_CONNECTED) {
  55.     Serial.print("Attempting to connect to SSID: ");
  56.     Serial.println(ssid);
  57.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  58.     status = WiFi.begin(ssid, pass);
  59.  
  60.     // wait 10 seconds for connection:
  61.     delay(10000);
  62.   }
  63.   // you're connected now, so print out the status:
  64.   printWiFiStatus();
  65. }
  66.  
  67. void loop() {
  68.   // if there's incoming data from the net connection.
  69.   // send it out the serial port.  This is for debugging
  70.   // purposes only:
  71.   while (client.available()) {
  72.     char c = client.read();
  73.     Serial.write(c);
  74.   }
  75.  
  76.   // if ten seconds have passed since your last connection,
  77.   // then connect again and send data:
  78.   if (millis() - lastConnectionTime > postingInterval) {
  79.     httpRequest();
  80.   }
  81.  
  82. }
  83.  
  84. // this method makes a HTTP connection to the server:
  85. void httpRequest() {
  86.   //-----------------------------
  87.  
  88.   // close any connection before send a new request.
  89.   // This will free the socket on the WiFi shield
  90.   client.stop();
  91.  
  92.   // if there's a successful connection:
  93.   // if (client.connect(server, 80)) {
  94.   if (client.connect("www.google.com", 80)) {
  95.     Serial.println("connected to www.google.com...");
  96.   }
  97.   else {
  98.     // if you couldn't make a connection:
  99.     Serial.println("connection failed to www.google.com");
  100.   }
  101.  
  102.   //-----------------------------
  103.  
  104.   // close any connection before send a new request.
  105.   // This will free the socket on the WiFi shield
  106.   client.stop();
  107.  
  108.   // if there's a successful connection:
  109.   // if (client.connect(server, 80)) {
  110.   if (client.connect("www.arduino.cc", 80)) {
  111.     Serial.println("connected to www.arduino.cc...");
  112.   }
  113.   else {
  114.     // if you couldn't make a connection:
  115.     Serial.println("connection failed to www.arduino.cc");
  116.   }
  117.  
  118.   //-----------------------------
  119.  
  120.   // close any connection before send a new request.
  121.   // This will free the socket on the WiFi shield
  122.   client.stop();
  123.  
  124.   // if there's a successful connection:
  125.   // if (client.connect(server, 80)) {
  126.   if (client.connect("pubsub.pubnub.com", 80)) {
  127.     Serial.println("connected to pubsub.pubnub.com...");
  128.   }
  129.   else {
  130.     // if you couldn't make a connection:
  131.     Serial.println("connection failed to pubsub.pubnub.com");
  132.   }
  133.  
  134.   //-----------------------------
  135.  
  136.   // note the time that the connection was made:
  137.   lastConnectionTime = millis();
  138. }
  139.  
  140.  
  141. void printWiFiStatus() {
  142.   // print the SSID of the network you're attached to:
  143.   Serial.print("SSID: ");
  144.   Serial.println(WiFi.SSID());
  145.  
  146.   // print your WiFi shield's IP address:
  147.   IPAddress ip = WiFi.localIP();
  148.   Serial.print("IP Address: ");
  149.   Serial.println(ip);
  150.  
  151.   // print the received signal strength:
  152.   long rssi = WiFi.RSSI();
  153.   Serial.print("signal strength (RSSI):");
  154.   Serial.print(rssi);
  155.   Serial.println(" dBm");
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement