Arduino analog readings gets off and no serial com

Hi!
Have an arduino one with imp shield that is connected RX/TX numbers 0/1 on the shield. Two sensor one rht03 connected to gnd, 5V and 7 and another moisture sensor connected to A0, 5 and 6. I have two problems, one is that when pushing in the imp the analog readings goes from 0 to 511 and does not respond to changes. Otherwise the respons from that moisture sensor goes from 0-960 between open air and shortcut.
The next hurdle is that I do not seem to be able to read serial data. On a serial sniffer on a computer connected with USB I get readings but using imp I get “No Data Returned” using this code (same UART setting in both):
// UART Read Example

// configure a pin pair for UART TX/RX
hardware.uart12.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS);

function readSerial() {
imp.sleep(0.1);
local result = hardware.uart12.read();
if (result == -1) {
server.show(“No data returned.”);
} else {
server.show(result);
}

imp.wakeup(300, readSerial);

}

imp.configure(“Serial RX”, [], []);
readSerial();

Was not allowed to upload arduino file so post the code here:
// Combination of http://gardenbot.org/howTo/soilMoisture/#The_local_circuit_-_simple_voltage and http://askforcode.blogspot.se/2013/02/interfacing-rht03dht22-temperature-and.html

#include <DHT22.h>
// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define voltageFlipPin1 5
#define voltageFlipPin2 6
#define sensorPin 0
#define DHT22_PIN 7
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);

int flipTimer = 1000;

void setup(){
// start serial port
Serial.begin(19200);
pinMode(voltageFlipPin1, OUTPUT);
pinMode(voltageFlipPin2, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.println(“SENSOR DEMO”);

}

void setSensorPolarity(boolean flip){
if(flip){
digitalWrite(voltageFlipPin1, HIGH);
digitalWrite(voltageFlipPin2, LOW);
}else{
digitalWrite(voltageFlipPin1, LOW);
digitalWrite(voltageFlipPin2, HIGH);
}
}

void loop(){

//
DHT22_ERROR_t errorCode;
Serial.print(“Requesting data…”);
errorCode = myDHT22.readData();
if(errorCode == DHT_ERROR_NONE)
{
Serial.print("Got Data ");
Serial.print(myDHT22.getTemperatureC());
Serial.print(“C “);
Serial.print(myDHT22.getHumidity());
Serial.println(”%”);
}
else
{
Serial.print(“Error Code “);
Serial.print(errorCode);
Serial.println(” readData Failed”);
}
setSensorPolarity(true);
delay(flipTimer);
int val1 = analogRead(sensorPin);
delay(flipTimer);
setSensorPolarity(false);
delay(flipTimer);
// invert the reading
int val2 = 1023 - analogRead(sensorPin);
//
reportLevels(val1,val2);

}

void reportLevels(int val1,int val2){

int avg = (val1 + val2) / 2;

String msg = "avg: ";
msg += avg;
Serial.println(msg);

}

Any ideas?
Kind regards
Fredrik

A few things:

  • I’d recommend using server.log() to output debug info, as this can then be seen, timestamped, in the log
  • You’re reading a maximum of 1 byte every 5 minutes. Is that what you intended? (the 300 in imp.wakeup is seconds).
  • You’d be better off using the UART callback and building a string which you terminate with a “
    ” on the arduino end

eg:

`// somewhere to build the string
buffer <- “”;

// When the callback is called, empty the serial buffer, and output string when a newline is seen
function readSerial() {
local result = hardware.uart12.read();
while(result >= 0) {
if (result == ‘
’) {
server.log(“received line: “+buffer);
buffer=””;
} else {
buffer = buffer + result.tochar();
}
result = hardware.uart12.read();
}
}

imp.configure(“Serial RX”, [], []);

// Init uart with new data callback
hardware.uart12.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS, readSerial);
`

…that’s written blind but should work I believe.

Hi Hugo!
Thanks for your help. Tried your suggestion and also removed all other connections so now I only have 5v, GND, 0 and 1 connected. I still have communication with the simple hello world example but when trying the serial the only output I get is “Serial RX”. If I look at the arduino board when connected to USB the RX led blinks and output data to the arduino serial monitor. If I unplug the USB the led never blinks. Do you think my shield could be defect and does not recognize the port 0 and 1?

I wonder if the imp can use other pins for serial communication? If I use arduinos software serial I could try other pins but the imp complains that uart is wrong, i.e. when I change all uart12 to uart89 in your example above.

Hmm found the problem, its easier if you read all the instructions… I missed that the shield needed soldering from pin 5 and 7 to connect to software serial on Arduino i.e. through 8 and 9. Now it works!