element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Design for a Cause - Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Design for a Cause - Design Challenge
  • More
  • Cancel
Design for a Cause - Design Challenge
Blog Blowing-Whistle as Controller(BWaC) -----Pubnub over WiFi connection #6
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: fyaocn
  • Date Created: 31 Aug 2018 8:51 AM Date Created
  • Views 1356 views
  • Likes 5 likes
  • Comments 3 comments
Related
Recommended

Blowing-Whistle as Controller(BWaC) -----Pubnub over WiFi connection #6

fyaocn
fyaocn
31 Aug 2018

1. WiFi connection

Arduino MKR1000 has been Wi-Fi connectivity requiring minimal previous experience in networking.

It is based on the Atmel ATSAMW25 SoC (System on Chip). The ATSAMW25 is composed of three main blocks and one PCB Antenna

  • SAMD21 Cortex-M0+ 32bit low power ARM MCU
  • WINC1500 low power 2.4GHz IEEERegistered 802.11 b/g/n Wi-Fi
  • ECC508 CryptoAuthentication
  • 1x1 stream PCB Antenna.

Maybe it will make some effort if one try to use standard wifi library of arduino. While more appropriate choice is WiFi101 library, since it is totally compatible.

Here is one simple ping test, what you shall do is to fill in the ssid and password of your wifi. It shall be noted the ECC508 CryptoAuthentication is not standard configuration in wifi modules, it offer better security against unauthorized access.

#include <SPI.h>

#include <SPI.h>
#include <WiFi101.h>


#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status


// Specify IP address or hostname
String hostName = "www.google.com";
int pingResult;


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }


  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);


    // wait 5 seconds for connection:
    delay(5000);
  }


  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  printCurrentNet();
  printWiFiData();
}


void loop() {
  Serial.print("Pinging ");
  Serial.print(hostName);
  Serial.print(": ");


  pingResult = WiFi.ping(hostName);


  if (pingResult >= 0) {
    Serial.print("SUCCESS! RTT = ");
    Serial.print(pingResult);
    Serial.println(" ms");
  } else {
    Serial.print("FAILED! Error code: ");
    Serial.println(pingResult);
  }


  delay(5000);
}

The serial console output the following ,

image

The ping fails because the computer firewall block the process, so, it is Ok.

 

2. Pubnub service

Pubnub is evolving continuously. New service like chatengine is produced. While in this project IoT message is used for Securely monitor, control, provision and stream data between smart devices, sensor networks, hubs, and more. Trigger device action, monitor meta data, or stream and process incoming and outgoing data. PubNub provides the infrastructure and APIs for communication for any size IoT deployment.

It is a little more complex than wifi connection. But it is still as easy as you can image. Just fill the pubkey, subkey, pubnub channel. Then everything is Ok. The Arduino library packing everything.

For me, the challenge part is in far end of mobile application subscribe to the pubnub channel. I will take more time on it.

But the sketch returns well below.

image

The error code is fairly normal, since using public subkey as "demo" . The quota is exceed very soon. If using private subkey, the result will be good.

Here is the main part of the code.

Below is code, but for better understanding , this getting start blog shall be referred.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Serial set up");


  // attempt to connect using WPA2 encryption:
  Serial.println("Attempting to connect to WPA network...");
  status = WiFi.begin(ssid, pass);


  // if you're not connected, stop here:
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while (true);
  } else {
    Serial.print("WiFi connecting to SSID: ");
    Serial.println(ssid);


    PubNub.begin(pubkey, subkey);
    Serial.println("PubNub set up");
  }
}


void loop() {


  /* Publish */
  
  WiFiClient *client;
  
  char msg[] = "\"Yo!\"";


  client = PubNub.publish(channel, msg);


  if (!client) {
    Serial.println("publishing error");
    delay(1000);
    return;
  }
  if (PubNub.get_last_http_status_code_class() != PubNub::http_scc_success) {
    Serial.print("Got HTTP status code error from PubNub, class: ");
    Serial.print(PubNub.get_last_http_status_code_class(), DEC);
  }
  while (client->available()) {
    Serial.write(client->read());
  }
  client->stop();
  Serial.println("---");
}

  • Sign in to reply

Top Comments

  • fyaocn
    fyaocn over 6 years ago in reply to dixonselvan +1
    Thanks.
  • aspork42
    aspork42 over 6 years ago +1
    It sounds pretty similar to MQTT. You can run a free MQTT broker on any computer in your house and it will handle all the machine to machine communications. You can then also bridge it to the internet…
  • aspork42
    aspork42 over 6 years ago

    It sounds pretty similar to MQTT. You can run a free MQTT broker on any computer in your house and it will handle all the machine to machine communications.  You can then also bridge it to the internet via something like thinger.io.

    Thanks for sharing this!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • fyaocn
    fyaocn over 6 years ago in reply to dixonselvan

    Thanks.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dixonselvan
    dixonselvan over 6 years ago

    fyaocn thanks for sharing about PubHub

    The quota is exceed very soon

    Regarding the above, why don't you give thinger.io a try? It is much similar I suppose and it is free with fairly sufficient limit.

     

    For reference you can have a look at my below blogs:

    Steps for connecting Arduino MKR1000 with thinger.io for reading temperature data

    Cue System for Anosmia and Smart WheelChair #4 - IoT Temperature Monitoring [DIY]

     

    Or some related useful blogs from anpivey - Austin Spivey's Blog

    Send a Command to the MKR WiFi 1010 using Wia and MQTT

    Measuring Soil Moisture with the Wemos ESP 32

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube