Hello, Sorry for anyone who may have seen the previous post where I mistakenly posted my code in a quote box. I apologize for offending the arduino forum members who take formatting so serious!
Basically I am trying to take these two examples and combine the parts I need to update temperatures to Thingspeak using the WIFI101 shield rather that ethernet.
Here is the code I am working with
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <OneWire.h>
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "DG860AB2"; // your network SSID (name)
char pass[] = "DG860A750FB2"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiServer server(80);
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "NWFQ0WP5S5V6RR6A";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
// Initialize Arduino Ethernet Client
WiFiClient client;
OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
// Start Ethernet on Arduino
// startEthernet();
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
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, 1); // start conversion, use ds.write(0x44,1) with parasite power on at the end
delay(50000); // 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;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}
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 ) {startEthernet();}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
//String tsData;
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
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 printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
I am currently "expected unqualified-id before 'if'" for line 136
Any help would be greatly appreciated
CrossRoads:
No compiler access here, but likely missing a closing } or ) or ; somewhere before the error.
Thanks for the reply, I have checked and checked and can't find where I'm missing anything. The funny thing is, I can get both examples to work on their own, but when I manipulate the code and combine aspects of the two it comes up with the error messages
Assuming you're using the Arduino IDE, put your cursor before the first curly brace in the loop function and scroll down until you see the closing brace it's associated with.
The line the compiler is complaining about is right after that closing brace and it's complaining because that code isn't in any function. You'll need to clean up those braces.
It's hard to see because you have too much stuff in loop. Consider moving parts out into separate functions.
Look for comments that say REMOVED. This compiles but you need to declare some variables to put some lines back in.
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
#include <OneWire.h>
#include <SPI.h>
#include <WiFi101.h>
char ssid[] = "DG860AB2"; // your network SSID (name)
char pass[] = "DG860A750FB2"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiServer server(80);
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "NWFQ0WP5S5V6RR6A";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
// Initialize Arduino Ethernet Client
WiFiClient client;
OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
// Start Ethernet on Arduino
// startEthernet();
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
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, 1); // start conversion, use ds.write(0x44,1) with parasite power on at the end
delay(50000); // 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;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
// REMOVED }
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
// REMOVED {
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
}
// REMOVED, analogValue0 not defined updateThingSpeak("field1=" + analogValue0);
// }
// Check if Arduino Ethernet needs to be restarted
/* REMOVED failedCounter not defined
if (failedCounter > 3 ) {
startEthernet();
}
*/
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
//String tsData;
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
// REMOVED, failedCounter not declared failedCounter = 0;
}
else
{
// REMOVED, failedCounter not declared failedCounter++;
// REMOVED, failedCounter not declared Serial.println("Connection to ThingSpeak failed (" + String(failedCounter, DEC) + ")");
Serial.println();
}
}
else
{
// REMOVED, failedCounter not declared failedCounter++;
// REMOVED, failedCounter not declared Serial.println("Connection to ThingSpeak Failed (" + String(failedCounter, DEC) + ")");
Serial.println();
lastConnectionTime = millis();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
The curly brace on line 135 closes the loop function.
If you put every { on a line by itself, and put every } on a line by itself, and used Tools + Auto Format, misplaced curly braces are far easier to see.
PaulS:
The curly brace on line 135 closes the loop function.
If you put every { on a line by itself, and put every } on a line by itself, and used Tools + Auto Format, misplaced curly braces are far easier to see.