0

code conversion
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

code conversion

by tj1 on Thu Jan 04, 2018 8:57 pm

I have this sketch working with Xively, with two DHT22 sensors. Is there an easy way to get it working with Adafruit IO ?
I am not good with coding and don't remember how I got it pieced together in the first place.

Thanks

/***************************************************
This is a sketch to use the CC3000 WiFi chip & Xively

Written by Marco Schwartz for Open Home Automation
****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "DHT.h"
#include<stdlib.h>

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10

// DHT sensor
#define DHTPIN_IN 7
#define DHTPIN_OUT 2
#define DHTTYPE_IN DHT22
#define DHTTYPE_OUT DHT22

// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2); // you can change this clock speed

// DHT instance
DHT dht_IN(DHTPIN_IN, DHTTYPE_IN);
DHT dht_OUT(DHTPIN_OUT, DHTTYPE_OUT);


// WLAN parameters
#define WLAN_SSID "**********"
#define WLAN_PASS "***************"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2

// Xively parameters
#define WEBSITE "api.personal.xively.com"
#define API_key "***************************************"
#define feedID "***********"

uint32_t ip;

void setup(void)
{
// Initialize
Serial.begin(115200);

dht_IN.begin();
dht_OUT.begin();

Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}

}

void loop(void)
{
// Connect to WiFi network
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println(F("Connected!"));

/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}

// Get the website IP & print it
ip = 0;
Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);

// Get data & transform to integers
float h_IN = dht_IN.readHumidity();
float t_IN = dht_IN.readTemperature();
float h_OUT = dht_OUT.readHumidity();
float t_OUT = dht_OUT.readTemperature();


int temperature_IN = (int) (t_IN*1.8+32);
int humidity_IN = (int) h_IN;
int temperature_OUT = (int) (t_OUT*1.8+32);
int humidity_OUT = (int) h_OUT;

// Prepare JSON for Xively & get length
int length = 0;

String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"In-T\",\"current_value\" : \"" + String(temperature_IN) + "\"},"
+ "{\"id\" : \"In-H\",\"current_value\" : \"" + String(humidity_IN) + "\"},"
+ "{\"id\" : \"Out-T\",\"current_value\" : \"" + String(temperature_OUT) + "\"},"
+ "{\"id\" : \"Out-H\",\"current_value\" : \"" + String(humidity_OUT) + "\"}]}";

//+ "{\"id\" : \"Outside-Temperature\",\"current_value\" : \"" + String(temperature_OUT) + "\"},"

length = data.length();
Serial.print("Data length");
Serial.println(length);
Serial.println();

// Print request for debug purposes
Serial.print("PUT /v2/feeds/");
Serial.print(feedID);
Serial.println(".json HTTP/1.0");
Serial.println("Host: api.xively.com");
Serial.print("X-ApiKey: ");
Serial.println(API_key);
Serial.print("Content-Length: ");
Serial.println(length, DEC);
Serial.print("Connection: close");
Serial.println();
Serial.print(data);
Serial.println();

// Send request
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected()) {
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
} else {
Serial.println(F("Connection failed"));
return;
}

Serial.println(F("-------------------------------------"));
while (client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.close();
Serial.println(F("-------------------------------------"));

Serial.println(F("\n\nDisconnecting"));
cc3000.disconnect();

// Wait 10 seconds until next update
delay(10000);

}

tj1
 
Posts: 7
Joined: Sun Apr 08, 2012 1:15 pm

Please be positive and constructive with your questions and comments.