Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shubra Qubala IT Project - Water Sensor code for the Arduino processor
Allebrum, LLC (
Senica Gonzalez -
Manual for packet assembly and options can be found here:
ultrasonic module ping distance sensor
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 3; Trig to Arduino pin 4
#define trigPin 4
#define echoPin 3
byte buffer[254]; //Buffer for message
byte serial[8]; //Array for Serial Address
byte address[2]; //Array for Network Address
unsigned int j; //Keep track of buf location
void setup(){
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
void setSerial(byte one, byte two, byte three, byte four, byte five, byte six, byte seven, byte eight){
serial[0] = one; serial[1] = two; serial[2] = three; serial[3] = four; serial[4] = five; serial[5] = six; serial[6] = seven; serial[7] = eight;
void setAddress(byte one, byte two){
address[0] = one; address[1] = two;
void createPacket(float water, float sound, float temp, float humidity){
memset(buffer, 0, sizeof(buffer)); //Initialize the buffer
char w[10], s[10], t[10], h[10];
dtostrf(water,0,2,w); dtostrf(sound,0,2,s); dtostrf(temp,0,2,t); dtostrf(humidity,0,2,h);
String data = "{\"water\":\""+String(w) +"\", \"sound\":\""+ String(s) +"\", \"temp\":\""+ String(t) +"\", \"humidity\":\""+ String(h) +"\"}";
j = 0;
buffer[j++] = 0x7E; //Start byte
buffer[j++] = 0x00; //Start length
buffer[j++] = (byte)(data.length() + 14); //Length of Data + the following 14 bytes
buffer[j++] = 0x10; //Frame type - see documentation
buffer[j++] = 0x01; //Frame ID for host to send ACK to, should probably be random but must be non-zero
for(int i=0; i<sizeof(serial); i++) //Serial address of node to send packet to. For the coordinator you can use all 0x00's or the actual serial number
buffer[j++] = serial[i];
for(int i=0; i<sizeof(address); i++) //Network address of node. If you don't know it, you can use {0xFF, 0xFE}. The ACK will contain the network address.
buffer[j++] = address[i];
buffer[j++] = 0x00; //Broadcast radius
buffer[j++] = 0x00; //Options
for(int i=0; i<data.length(); i++) //Pack in the data
buffer[j++] = (byte)(data[i]);
//Calculate checksum
int checksum = 0;
for(int i=3; i<sizeof(buffer); i++){
checksum += buffer[i];
buffer[j++] = 0xFF - (checksum & 0xFF); //Checksum is configured by subtracting the lower 8 bits of the sum of the bytes between offset 3 and the checksum from 0xFF.
Serial.write(buffer, j);
float waterLevel(){
float duration, distance, mm;
//Send trigger to request new measurement
digitalWrite(trigPin, HIGH); delayMicroseconds(1000); digitalWrite(trigPin, LOW);
//Measure how long the echoPin is HIGH - this is the duration
duration = pulseIn(echoPin, HIGH);
* Calculate the distance in millimeters
* Divide by two because the duration is the time it takes for the sound to travel to an object and back, we only want the time to the object
* If you want to calculate cm, use: Pace of Sound D = (T / 2) / 29.1 OR D = (T x 0.0343) / 2 for example: distance = (duration / 2) / 29.1;
mm = ((( (duration / 2) * 1) * 344) / (0.001 / 0.000001));
if(mm >= 4000) //If greater than 4000 (4m) we can't read it reliably
return -1;
return mm;
void loop(){
setSerial(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); //Coordinator address OR use serial number
setAddress(0xFF, 0xFE); //Coorinator network address when unknown
createPacket(waterLevel(), 0, 0, 0);
delay(3000); //Wait 3 seconds before next reading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment