To prove my point that this sensor is going nuts/faulty, I hook up an Ethernet shield and send the both the URM37 distance sensors feed to cosm.com every second so that I can "see" when the sensor is operational and when it decide to go on strike...
https://cosm.com/feeds/78111
Any idea why/how such things could happen ?
The power regulator on the sensor is not hot at all and I'm powering this up using a 12V 5A SMPS to the DC socket...
Getting the sensor reading every 1000ms ...
The full codes below for the URM37 using Cosm & HttpClient libraries
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Your Cosm key to let you upload data
char cosmKey[] = "PutYourAPIKeyHere";
// Define the strings for our datastream IDs
char sensorId[] = "Range1";
char sensorId2[] = "Range2";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_INT),
CosmDatastream(sensorId2, strlen(sensorId), DATASTREAM_INT),
};
// Finally, wrap the datastreams into a feed
CosmFeed feed(78111, datastreams, 2 ); // Setup a new stream device/feed ans put the Feed ID here
IPAddress ip(192,168,0,50); // Change to your IP address
EthernetClient client;
CosmClient cosmclient(client);
// URM Defines
unsigned int i=0;
int URPWM = 2; // PWM pin
int URTRIG = 3; // Trigger pin
int URPWM2 = 4; // PWM2 pin
int URTRIG2 = 5; // Trigger2 pin
unsigned int Distance,Distance2=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01}; // distance measure command
void setup(){
Serial.begin(9600);
Serial.println("Starting single datastream upload to Cosm...");
Serial.println("Starting Ethernet Client...");
// Setting up Ethernet static IP address
Ethernet.begin(mac, ip);
Serial.println("Ethernet client & IP address OK");
// URM Setup
PWM_Mode_Setup();
Serial.println("Setting up URM37 PWM Mode...");
delay(500); // Wait for sensors to startup...
Serial.println();
}
void loop()
{
PWM_Mode();
delay(1000); // 1 sec per feed updates
}
//PWM mode setup function
void PWM_Mode_Setup(){
pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRIG
pinMode(URTRIG2,OUTPUT);
digitalWrite(URTRIG,HIGH); // Set to HIGH
digitalWrite(URTRIG2,HIGH); // Set to HIGH
pinMode(URPWM, INPUT); // Sending Enable PWM mode command
pinMode(URPWM2, INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++){
Serial.write(EnPwmCmd[i]);
}
}
void PWM_Mode(){ // a low pull on pin COMP/TRIG triggering a sensor reading
digitalWrite(URTRIG, LOW);
delay(1);
digitalWrite(URTRIG, HIGH); // reading Pin PWM will output pulses
unsigned long DistanceMeasured=pulseIn(URPWM,LOW);
delay(10); // slight delays between sensors
digitalWrite(URTRIG2, LOW);
delay(1);
digitalWrite(URTRIG2, HIGH); // reading Pin PWM will output pulses
unsigned long DistanceMeasured2=pulseIn(URPWM2,LOW);
if( DistanceMeasured >= 50000 || DistanceMeasured2 >= 50000){
Serial.print("PWM Error Values");
}
else{
Distance=DistanceMeasured/50; // every 50us low level stands for 1cm
Distance2=DistanceMeasured2/50; // every 50us low level stands for 1cm
}
datastreams[0].setInt(Distance);
datastreams[1].setInt(Distance2);
Serial.print("Distance1 :");
Serial.println(datastreams[0].getInt());
Serial.print("Distance2 :");
Serial.println(datastreams[1].getInt());
int ret = cosmclient.put(feed,cosmKey);
Serial.print("cosmclient.put returned ");
Serial.println(ret);
}