<commit_before>#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <OneWire.h>
#include <WiFiSettings.h>

// CC3000 configuration
#define     ADAFRUIT_CC3000_IRQ    3    // MUST be an interrupt pin!
#define     ADAFRUIT_CC3000_VBAT   5    // VBAT and CS can be any two pins
#define     ADAFRUIT_CC3000_CS     10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, 
                                         ADAFRUIT_CC3000_IRQ, 
                                         ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);

// Wireless network configuration
// Put WiFi Config Here

// Other sketch configuration
#define     READING_DELAY_SECS     10      // Number of minutes to wait between readings.
#define     TIMEOUT_MS             15000  // How long to wait (in milliseconds) for a server connection to respond (for both AWS and NTP calls).

#define MAX_DS1820_SENSORS 1
byte addr[8];
OneWire ds(8);  // on pin 8

// State used to keep track of the current time and time since last temp reading.
unsigned long lastPolledTime = 0;   // Last value retrieved from time server
unsigned long sketchTime = 0;       // CPU milliseconds since last server query
unsigned long lastReading = 0;      // Time of last temperature reading.

void setup(void) {
  Serial.begin(115200);
  
  // Initialize and connect to the wireless network
  // This code is adapted from CC3000 example code.
  if (!cc3000.begin()) {
    Serial.println(F("Unable to initialise the CC3000!"));
    while(1);
  }
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed to connect to AP!"));
    while(1);
  }
  // Wait for DHCP to complete
  while (!cc3000.checkDHCP()) {
    delay(100);
  }
  
  // Get an initial time value by querying an NTP server.
  unsigned long t = getTime();
  while (t == 0) {
    // Failed to get time, try again in a minute.
    delay(60*1000);
    t = getTime();
  }
  lastPolledTime = t;
  sketchTime = millis();
  
  if (!ds.search(addr))  {
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("R=");
  for(int i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }

  if ( addr[0] == 0x10) {
      Serial.print("Device is a DS18S20 family device.\n");
  } else if ( addr[0] == 0x28) {
      Serial.print("Device is a DS18B20 family device.\n");
  } else {
      Serial.print("Device family is not recognized: 0x");
      Serial.println(addr[0],HEX);
      return;
  }
  
  Serial.println(F("Running..."));
}

void loop(void) {
  // Update the current time.
  // Note: If the sketch will run for more than ~24 hours, you probably want to query the time
  // server again to keep the current time from getting too skewed.
  unsigned long currentTime = lastPolledTime + (millis() - sketchTime);
  
  if ((currentTime - lastReading) >= (READING_DELAY_SECS * 1000)) {
    lastReading = currentTime;

    float currentTemp = readTempF();
    Serial.println(currentTemp);
  
    dbWrite(currentTime, currentTemp);
  }
}

float readTempF() {
  byte i;
  byte data[12];
  
  ds.reset();
  ds.select(addr);
  
  ds.write(0x44, 1); // Start conversion
  delay(850);
  ds.reset();
  ds.select(addr);
  ds.write(0xBE); // Issue Read scratchpad command
  
  // Receive 9 bytes
  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  
  return ((data[1] << 8) + data[0]) * 0.0625;
}

// Write a temperature reading to the DynamoDB table.
void dbWrite(unsigned long currentTime, float currentTemp) {
  // Generate time and date strings
  DateTime dt(currentTime);
  // Set dateTime to the ISO8601 simple date format string.
  char dateTime[17];
  memset(dateTime, 0, 17);
  dateTime8601(dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute(), dt.second(), dateTime);
  // Set date to just the year month and day of the ISO8601 simple date string.
  char date[9];
  memset(date, 0, 9);
  memcpy(date, dateTime, 8);
  // Set currentTimeStr to the string value of the current unix time (seconds since epoch).
  char currentTimeStr[8*sizeof(unsigned long)+1];
  memset(currentTimeStr, 0, 8*sizeof(unsigned long)+1);
  ultoa(currentTime, currentTimeStr, 10);

  // Generate string for the temperature reading.
  char temp[8*sizeof(unsigned long)+5];
  memset(temp, 0, 8*sizeof(unsigned long)+5);
  // Convert to fixed point string.  Using a proper float to string function
  // like dtostrf takes too much program memory (~1.5kb) to use in this sketch.
  ultoa((unsigned long) currentTemp, temp, 10);
  int n = strlen(temp);
  temp[n] = '.';
  temp[n+1] = '0' + ((unsigned long) (currentTemp*10)) % 10;
  temp[n+2] = '0' + ((unsigned long) (currentTemp*100)) % 10;
  temp[n+3] = '0' + ((unsigned long) (currentTemp*1000)) % 10;

  // Make request to DynamoDB API.
  uint32_t ip = 0;
  while (ip == 0) {
    if (!cc3000.getHostByName("fremont", &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 9200);
  if (www.connected()) {
    
    // Generate string with payload length for use in the signing and request sending.  
    char payloadlen[8*sizeof(unsigned long)+1];
    memset(payloadlen, 0, 8*sizeof(unsigned long)+1);
    ultoa(20+strlen(currentTimeStr)+strlen(temp), payloadlen, 10);
    
    // HTTP Headers
    www.fastrprint(F("POST /temp_gauge/reading HTTP/1.1\n"));
    www.fastrprint(F("Host: fremont:9200\n"));
    www.fastrprint(F("Content-Length: "));
    www.fastrprint(payloadlen);
    www.fastrprint(F("\n"));
    www.fastrprint(F("Content-Type: application/json\n"));
    www.fastrprint(F("\n"));
    
    // Payload
    www.fastrprint(F("{\"time\":")); // Length: 8
    www.fastrprint(currentTimeStr);
    www.fastrprint(F("000,\"temp\":")); // Length: 11
    www.fastrprint(temp);
    www.fastrprint(F("}")); // Length: 1
  } else {
    Serial.println(F("Connection failed"));    
    www.close();
    return;
  }
  
  // Read data until either the connection is closed, or the idle timeout is reached.
  Serial.println(F("Elasticsearch response:"));
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      Serial.print(c);
      lastRead = millis();
    }
  }
  www.close();
}



// Convert an array of bytes into a lower case hex string.
// Buffer MUST be two times the length of the input bytes array!
void hexString(uint8_t* bytes, size_t len, char* buffer) {
  for (int i = 0; i < len; ++i) {
    btoa2Padded(bytes[i], &buffer[i*2], 16);
  }
}
<commit_msg>Add a bunch of logging, configuration, etc.<commit_after>#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <OneWire.h>
#include "WiFiSettings.h"

// CC3000 configuration
#define     ADAFRUIT_CC3000_IRQ    3    // MUST be an interrupt pin!
#define     ADAFRUIT_CC3000_VBAT   5    // VBAT and CS can be any two pins
#define     ADAFRUIT_CC3000_CS     10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, 
                                         ADAFRUIT_CC3000_IRQ, 
                                         ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIV2);

// Other sketch configuration
#define     READING_DELAY_SECS     1      // Number of seconds to wait between readings.
#define     TIMEOUT_MS             15000  // How long to wait (in milliseconds) for a server connection to respond
#define     WEBSITE                "fremont"

#define MAX_DS1820_SENSORS 1
byte addr[8];
OneWire ds(8);  // on pin 8

// State used to keep track of the current time and time since last temp reading.
unsigned long lastPolledTime = 0;   // Last value retrieved from time server
unsigned long sketchTime = 0;       // CPU milliseconds since last server query
unsigned long lastReading = 0;      // Time of last temperature reading.

void setup(void) {
  Serial.begin(115200);
  
  // Initialize and connect to the wireless network
  // This code is adapted from CC3000 example code.
  if (!cc3000.begin()) {
    Serial.println(F("Unable to initialise the CC3000!"));
    while(1);
  }
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed to connect to AP!"));
    while(1);
  }
  // Wait for DHCP to complete
  while (!cc3000.checkDHCP()) {
    delay(100);
  }
  
  // Get an initial time value by querying an NTP server.
  unsigned long t = getTime();
  while (t == 0) {
    // Failed to get time, try again in a minute.
    delay(60*1000);
    t = getTime();
  }
  lastPolledTime = t;
  sketchTime = millis();
  
  if (!ds.search(addr))  {
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("R=");
  for(int i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }

  if ( addr[0] == 0x10) {
      Serial.print("Device is a DS18S20 family device.\n");
  } else if ( addr[0] == 0x28) {
      Serial.print("Device is a DS18B20 family device.\n");
  } else {
      Serial.print("Device family is not recognized: 0x");
      Serial.println(addr[0],HEX);
      return;
  }
  
  Serial.println(F("Running..."));
}

void loop(void) {
  // Update the current time.
  // Note: If the sketch will run for more than ~24 hours, you probably want to query the time
  // server again to keep the current time from getting too skewed.
  unsigned long currentTime = lastPolledTime + (millis() - sketchTime);

  Serial.print(F("Starting read. Current time: ")); Serial.println(currentTime);
  lastReading = currentTime;

  float currentTemp = readTempF();
  Serial.print(F("Current temp: ")); Serial.println(currentTemp);

  dbWrite(currentTime, currentTemp);
  
  Serial.println(F("\n\rDone writing. Delay till next read.\n\r\n\r"));
  delay(READING_DELAY_SECS * 1000);
}

float readTempF() {
  byte i;
  byte data[12];
  
  ds.reset();
  ds.select(addr);
  
  ds.write(0x44, 1); // Start conversion
  delay(850);
  ds.reset();
  ds.select(addr);
  ds.write(0xBE); // Issue Read scratchpad command
  
  // Receive 9 bytes
  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  
  return ((data[1] << 8) + data[0]) * 0.0625;
}

void dbWrite(unsigned long currentTime, float currentTemp) {
  // Generate time and date strings
  DateTime dt(currentTime);
  // Set dateTime to the ISO8601 simple date format string.
  char dateTime[17];
  memset(dateTime, 0, 17);
  dateTime8601(dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute(), dt.second(), dateTime);
  
  // Set date to just the year month and day of the ISO8601 simple date string.
  char date[9];
  memset(date, 0, 9);
  memcpy(date, dateTime, 8);
  
  // Set currentTimeStr to the string value of the current unix time (seconds since epoch).
  char currentTimeStr[8*sizeof(unsigned long)+1];
  memset(currentTimeStr, 0, 8*sizeof(unsigned long)+1);
  ultoa(currentTime, currentTimeStr, 10);

  // Generate string for the temperature reading.
  char temp[8*sizeof(unsigned long)+5];
  memset(temp, 0, 8*sizeof(unsigned long)+5);
  
  // Convert to fixed point string.  Using a proper float to string function
  // like dtostrf takes too much program memory (~1.5kb) to use in this sketch.
  ultoa((unsigned long) currentTemp, temp, 10);
  int n = strlen(temp);
  temp[n] = '.';
  temp[n+1] = '0' + ((unsigned long) (currentTemp*10)) % 10;
  temp[n+2] = '0' + ((unsigned long) (currentTemp*100)) % 10;
  temp[n+3] = '0' + ((unsigned long) (currentTemp*1000)) % 10;

  // Resolve server IP address
  uint32_t ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" I.P. address: "));
  while (ip == 0) {
    if (!cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);
  
  Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");  
  int replies = cc3000.ping(ip, 5);
  Serial.print(replies); Serial.println(F(" replies"));
  
  // Send request to Elasticsearch API
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 9200);
  if (www.connected()) {
    Serial.println(F("Connected. Sending reading to Elasticsearch"));
    // Generate string with payload length for use in the signing and request sending.  
    char payloadlen[8*sizeof(unsigned long)+1];
    memset(payloadlen, 0, 8*sizeof(unsigned long)+1);
    ultoa(20+strlen(currentTimeStr)+strlen(temp), payloadlen, 10);
    
    // HTTP Headers
    Serial.print(F("Sending headers... "));
    www.fastrprint(F("POST /temp_gauge/reading HTTP/1.1\n"));
    www.fastrprint(F("Host: fremont:9200\n"));
    www.fastrprint(F("Content-Length: "));
    www.fastrprint(payloadlen);
    www.fastrprint(F("\n"));
    www.fastrprint(F("Content-Type: application/json\n"));
    www.fastrprint(F("\n"));
    Serial.println("done.");
    
    // Payload
    Serial.print(F("Sending body... "));
    www.fastrprint(F("{\"time\":")); // Length: 8
    www.fastrprint(currentTimeStr);
    www.fastrprint(F("000,\"temp\":")); // Length: 11
    www.fastrprint(temp);
    www.fastrprint(F("}")); // Length: 1
    Serial.println(F("done."));
  } else {
    Serial.println(F("Connection failed"));    
    www.close();
    return;
  }
  
  // Read data until either the connection is closed, or the idle timeout is reached.
  unsigned long lastRead = millis();
  Serial.println(F("Elasticsearch response: "));
  while (www.connected() && (millis() - lastRead < TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      Serial.print(c);
      lastRead = millis();
    }
  }
  www.close();
}



// Convert an array of bytes into a lower case hex string.
// Buffer MUST be two times the length of the input bytes array!
void hexString(uint8_t* bytes, size_t len, char* buffer) {
  for (int i = 0; i < len; ++i) {
    btoa2Padded(bytes[i], &buffer[i*2], 16);
  }
}

{'fraction_non_alphanumeric': 0.14217710885544277, 'fraction_numerical': 0.03906195309765488, 'mean_word_length': 2.9949664429530203, 'pattern_counts': {'":': 4, '<': 28, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 38, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5871725', 'n_tokens_mistral': 5229, 'n_tokens_neox': 4755, 'n_words': 1390}