Go Down

Topic: WiFi101 connectSSL works with IPAddress, but not with hostname (Read 2338 times) previous topic - next topic

nakor69

Apr 06, 2018, 02:40 am Last Edit: Apr 06, 2018, 02:44 am by nakor69 Reason: Insert Code for easier reading vs attachment
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?

Code: [Select]
#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])  ;
}

nakor69

I forgot I had posted this here...but I'm pretty sure I found the problem...

https://forum.arduino.cc/index.php?topic=546112.0

Go Up