Quantcast
Channel: ThingSpeak IoT Community - All Forums
Viewing all articles
Browse latest Browse all 1833

SilverNodashi on please help: how to submit multile DS18B20 readings?

$
0
0

Hi, 

 

I hope someone can help me with some sample code to upload multiple DS18B20 readings to different thingspeak "fields". My platform of choice is the Linkit One device, which has an ARM MCU, and I have found that the DallasTemperature.h library doesn't work so well. Most of the readings are "-127.00", instead of the actual temperatures. But, if I read the temperature without that library, i.e doing a 1-wire lookup, it works fine. The problem is, I can't figure out how to read 4 different temperatures, in order to submit to thingspeak. 

 

Here's my code, so far: 

 

#include <OneWire.h>
#include <Wire.h>

OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiServer.h>

#include <LWiFiClient.h>

#define WIFI_AP "Ahlers"
#define WIFI_PASSWORD "5qg8dzjpes41"
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP according to your WiFi AP configuration

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "69NWEUZ2IGPXRURZ";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)

long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

LWiFiClient client;

void setup()
{
LWiFi.begin();
Serial.begin(115200);

// keep retrying until connected to AP
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}

startWifi();

}

void loop()
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit, celsiuss;

if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
// Serial.println();
ds.reset_search();
delay(250);
return;
}

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

ds.reset();
ds.select(addr);
ds.write(0x44); // start conversion, use ds.write(0x44,1) with parasite power on at the end

delay(700); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

// Serial.print(" Data = ");
// Serial.print(present, HEX);
// Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
// Serial.print(" CRC=");
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");

celsiuss = round(celsius);
String analogValue0 = String(celsiuss, DEC);
//Serial.println(celsius);
// Print Update Response to Serial Monitor
if (client.available())
{
// char c = client.read();
// Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();

client.stop();
}

// Update ThingSpeak
// if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
// {
updateThingSpeak("field1="+analogValue0);
// }
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startWifi();}

lastConnected = client.connected();

}

void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("

");

client.print(tsData);

lastConnectionTime = millis();

if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();

failedCounter = 0;
}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}

}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();

lastConnectionTime = millis();
}
}

void startWifi()
{

client.stop();

Serial.println("Connecting Arduino to network...");
Serial.println();

delay(1000);

}


Viewing all articles
Browse latest Browse all 1833

Trending Articles