Skip to content

Instantly share code, notes, and snippets.

@Franklin97355
Created December 16, 2016 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Franklin97355/cc149937131d684c496719017c0b8a7f to your computer and use it in GitHub Desktop.
Save Franklin97355/cc149937131d684c496719017c0b8a7f to your computer and use it in GitHub Desktop.
A_WebWeather.ino/*
Web client to Adafruit IO
This sketch connects to a website (http://www.adafruit.com)
using a WINC1500
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
* Circuit:
* - Feather M0 WiFi (WINC1500), WiFi 101 shield, or WINC1500 Breakout
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 5 September 2016
by Stephen Franklin
*/
#include <SHT1x.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_WINC1500.h>
// Define the WINC1500 board connections below.
// If you're following the Adafruit WINC1500 board
// guide you don't need to modify these:
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC and comment this out
// The SPI pins of the WINC1500 (SCK, MOSI, MISO) should be
// connected to the hardware SPI port of the Arduino.
// On an Uno or compatible these are SCK = #13, MISO = #12, MOSI = #11.
// On an Arduino Zero use the 6-pin ICSP header, see:
// https://www.arduino.cc/en/Reference/SPI
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
// Or just use hardware SPI (SCK/MOSI/MISO) and defaults, SS -> #10, INT -> #7, RST -> #5, EN -> 3-5V
//Adafruit_WINC1500 WiFi;
boolean tor;
boolean tos;
int tries;
long timeOut, rainInt, speedInt;
float temp_c, temp_f, humidity;
float windSpeed = 0, maxSpeed = 0, totalRain;
volatile int speedCount, rainCount;
char ssid[] = "<redacted>"; // your network SSID (name)
char pass[] = "<redacted>"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
char aio_key[] = "<redacted>";
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(141,101,112,175); // numeric IP for test page (no DNS)
char server[] = "io.adafruit.com"; // domain name for test page (using DNS)
//#define webpage "/testwifi/index.html" // path to test page
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Adafruit_WINC1500Client client;
// Specify data and clock connections and instantiate SHT1x object
const byte dataPin = 9;
const byte clockPin = 10;
SHT1x sht1x(dataPin, clockPin);
const byte SW1 = 6;
const byte rain = 5;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 600L * 1000L; // delay between updates, in milliseconds
void setup() {
#ifdef WINC_EN
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
#endif
//configure SW1 as an input and enable the internal pull-up resistor
pinMode(SW1, INPUT_PULLUP);
//attach SW1 to an interrupt on it's low state
attachInterrupt(digitalPinToInterrupt(SW1), incSpeed, LOW);
//configure rain as an input and enable the internal pull-up resistor
pinMode(rain, INPUT_PULLUP);
//attach rain to an interrupt on it's low state
attachInterrupt(digitalPinToInterrupt(rain), incRain, LOW);
// Serial.begin(9600);
// while(!Serial) {
// delay(100);
// }
//Serial.println("Serial started ");
tos = false;
tor = false;
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
// don't continue:
while (true);
}
connectWifi();
//Serial.println("Connected to wifi");
printWifiStatus();
sendData();
}
void loop() {
if (tos) {
speedInt = millis();
tos = false;
}
if (tor) {
rainInt = millis();
tor = false;
}
if((millis()-timeOut) > 2250){
if(speedCount > 100) speedCount = 0;
if(speedCount > 1) windSpeed = (speedCount / 2);
else windSpeed = 0;
if(windSpeed > maxSpeed) maxSpeed = windSpeed;
timeOut = millis();
speedCount = 0;
}
if (millis() - lastConnectionTime > postingInterval) {
lastConnectionTime = millis();
sendData();
}
}
void connectWifi() {
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
}
void sendData() {
// Print the values to the Adafruit IO site
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
delay(10);
temp_f = sht1x.readTemperatureF();
delay(10);
humidity = sht1x.readHumidity();
delay(10);
totalRain = rainCount;
if(WiFi.status() != WL_CONNECTED) connectWifi();
if (client.connect(server, 80)) {
// Make a HTTP request:
client.print("GET ");
client.print("/api/groups/Lacomb_weather/send.json?x-aio-key=");
client.print(aio_key);
client.print("&Temperature=");
client.print(temp_f,1);
client.print("&Humidity=");
client.print(humidity,1);
client.print("&Wind=");
client.print(maxSpeed,0);
client.print("&Rain=");
client.print(totalRain,0);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
maxSpeed = 0;
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void incSpeed() {
if((millis()-speedInt) > 10) {
speedCount++;
speedInt = millis();
tos = true;
}
}
void incRain() {
if((millis()-rainInt) > 500) {
rainCount++;
rainInt = millis();
tor = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment