Trying to connect a data logger to a Google's script at script.google.com.
Originally, it would connect for a few cycles then fail to connect.
It works fine using a static IP address. But that causes the obvious problems down the road. It's already failed once from a server getting moved around.
Using a Mega2560 with an Adafruit ATWINC1500 WiFi Breakout.
Any ideas?
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SHT31.h>
#include <RTClib.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
//WiFi Variables
const char ssid[] = "xxxxx"; // your network SSID (name)
const char pass[] = "xxxxx"; // your network password
IPAddress ip;
// Google Data Script
const int httpsPort = 443;
const char* host = "script.google.com";
IPAddress dataServer = IPAddress(172,217,9,14); // script.google.com
WiFiClient client;
String GAS_ID = "xxxxx"; // Google Script Service ID
// Send data to Google Sheet via GET request to Sheet Script
void sendData(String s) {
digitalWrite(statusPin, LOW); // Turn off LED to indicate sendData operation
logData = "Connecting to: "; logData += IpAddress2String(dataServer); echoData(logData);
while (!client.connectSSL(dataServer, httpsPort)) { // Works this way, but fails to connect if using "host" instead of dataServer
// TODO: Set AlarmError
logData = "Connection Failed. IP:"; logData += IpAddress2String(client.remoteIP());
logData += " Client Status:"; logData += client.status();
logData += ", WiFi Status: "; logData += WiFi.status(); echoData(logData);
if(WiFi.status() != WL_CONNECTED) ConnectWiFi(); // Reset WiFi connection
// TODO: Clear AlarmError
}
logData = "Server Address: "; logData += IpAddress2String(client.remoteIP()); echoData(logData);
String url = "/macros/s/" + GAS_ID + "/exec?" + s;
logData = "Requesting URL: "; logData += url; echoData(logData);
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n"
+ "User-Agent: ArduinoGTLogger1.0\r\n"
+ "Connection: close\r\n\r\n");
echoData("Request Sent; Closing Connection");
client.stop();
digitalWrite(statusPin, HIGH); // Turn on LED to indicate sendData success
}
// Wifi Connect function to facilitate online reconnects
void ConnectWiFi(){
if (WiFi.status() == WL_NO_SHIELD) {
echoData("WiFi shield not present");
// TODO: Set AlarmError
while (true); // don't continue:
}
// Keep attempting to connect to WiFi network:
while (WiFi.status() != WL_CONNECTED) {
logData = "Attempting to connect to WPA SSID: "; logData += ssid; echoData(logData);
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
// TODO: Set AlarmStatus
delay(10000); // wait 10 seconds for connection:
}
// you're connected now, so print out the status:
// TODO: Clear AlarmStatus
logData = "SSID: "; logData += WiFi.SSID(); echoData(logData);
ip = WiFi.localIP();
logData = "IP Address: "; logData += IpAddress2String(ip); echoData(logData);
logData = "Signal Strength (RSSI): "; logData += WiFi.RSSI(); echoData(logData);
}
// Simplify code by sending string to Serial and SD Card
void echoData(String s){
dataFile.println(s);
Serial.println(s);
}
// Format IPAddress for more reliable printing
String IpAddress2String(const IPAddress& ipAddress){
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
testCode.ino (3.04 KB)