Development

» MR-Pr 01: Reading the ADC

In this example we read the analog-to-digital converter of the Prototyping 2.0 board every second and print the value obtained through the USB port.

Required Materials

1 x Waspmote
1 x Battery
1 x MiniUSB wire
1 x Prototyping 2.0 board

Notes

*Take a look at the Prototyping 2.0 board technical guide for information about how to attach the sensor or device to be read to the analog-to-digital converter of the board.
* Remember to connect the battery to Waspmote for proper operation.
* Logger, Common and Prototyping assemblies must be loaded in the mote.
* Please note that the output is in hexadecimal.
* Please note that Float.toLong() method has been used with a precission of 2 decimals. For example, a 3413 output in decimal base means 34.13. For more information of Float class, see http://localhost:5000/gac/saguaro-system-11.3.htm?fqn=r:com.ibm.saguaro.system.Float
* To compile this example: mrc --assembly=pir-1.0 --system=waspmote --ref=logger-#.# --ref=/common-1.0 --ref=/prototyping-1.0 PrototypingExample.java

Code

/**
 *  Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
 * http://www.libelium.com
 *
 * This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Design: David Gascón
 * 
 * @version 0.1
 * @author Anartz Nuin Jiménez and Ignacio Ruiz Martín
 */

package com.libelium.examples.prototyping;

import com.ibm.saguaro.logger.Logger;
import com.ibm.saguaro.system.Float;
import com.ibm.saguaro.system.GPIO;
import com.ibm.saguaro.system.Mote;
import com.ibm.saguaro.system.Time;
import com.ibm.saguaro.system.Timer;
import com.ibm.saguaro.system.TimerEvent;
import com.ibm.saguaro.system.Util;
import com.ibm.saguaro.system.csr;
import com.ibm.waspmote.WASPMOTE;
import com.libelium.common.Callback;
import com.libelium.common.Gpio;
import com.libelium.prototyping.Prototyping;

public class PrototypingExample {
	static Timer timer;
	static Prototyping proto = new Prototyping();
	static GPIO gpio = Gpio.getInstance();
	
	static{
		proto.ON();
		

		timer = new Timer();
		
		timer.setCallback(new TimerEvent(null){
        		public void invoke(byte param, long time){
        			PrototypingExample.timeCallback(param,time);
        		}
        	});
	
        long INTERVAL = Time.toTickSpan(Time.MILLISECS,1000);
    	timer.setAlarmBySpan(INTERVAL);
	}
	protected static void timeCallback(byte param, long time) {
		
		proto.readADC(new Callback(null) {
			
			@Override
			public int invoke(byte[] buf, int len) {
				return PrototypingExample.callbackADC(buf, len);
			}
		});
		long INTERVAL = Time.toTickSpan(Time.MILLISECS,1000);
		timer.setAlarmBySpan(INTERVAL);
	}
	private static int callbackADC(byte[]data, int len){
		long result = Util.get32(data, (byte)0);
		Logger.appendString(csr.s2b("ADC reading: "));
		Logger.appendLong(Float.toLong(result, (byte)2));
		Logger.flush(Mote.INFO);
		return 0;
	}
}

Output

category: log
evname: MAPP
id: 7d
time: 11:21:10.830'000
severity: 5
module: MAPP
logmsg: MAPP: ADC reading: 00005204

Quick Publish: