Sending ASCII Commands to a TSI 5310 Flowmeter from a Teensy 3.5 with (Arduino IDE) - ascii

Introduction:
Okay so to start I just want to say that the sensor does send its data when commanded as I've tested this on Python connected to a COMPORT on a pc. I will include the Python Code I created that works with the sensor, so that all information is available to you guys. I also will include a link to the PJRC Forum that I've asked the same question on, because I've already gotten responses on the issue, but it still persists, and I want you guys to have what they've said at your disposal.
(Python Code & PJRC Link will be at the very bottom of the post)
Problem:
So, my problem is I cannot figure out how to properly send ASCII commands from the Teensy 3.5 and in return read the output of the Flowmeter with the Teensy 3.5. I am afraid that the hardware is connected wrong or I'm just going about something wrong.
The Serial Console will stay blank meaning nothing is available to be read in
What I've Tried - Software:
This is basic code I was given that should work for my use:
char s;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial && (millis() < 5000)) {};
Serial1.begin(115200);
delay(1000);
Serial1.print("?\r\n");
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial1.available()){
s = Serial1.read();
Serial.print(s);
}
}
What I've Tried - Hardware:
Image of TSI FlowMeter 5130 w/Cables
Black Wire - USB_C to USB_A - connected to a 5v power supply
Blue/White Wire - USB_A to MALE DB9
Image of Cables that connect the Flowmeter & Teensy 3.5
Blue/White Wire - Male DB9
Tan Serial Gender Converter - Female DB9 to Female DB9
Black Converter Board - Male DB9 to 4-Wire TTL (Red - VCC, Yellow - Transmit, Blue - Receive, Black - GND)
Image of RS232 to TTL Wiring
Yellow Wire - Teensy Transmit Pin 1
Blue Wire - Teensy Receive Pin 0
Red Wire - Currently Set to 5v, but I've tried 3.3v to no avail
Black Wire - GND
Image of LEDs Wired into Rx/Tx of Teensy to watch for data being sent
Blue LED - (Yellow - Teensy Receive Pin 0, Orange - GND)
Green LED - (Red - Teensy Transmit Pin 1, Brown - GND)
Image - 5v Power Supply
White Wire - Teensy 5v
Purple Wire - Teensy GND
Python Code:
import serial
import time
index = 0
total = 0
i = 0
avg = 0
# Serial Connection
time.sleep(.5)
ser = serial.Serial(
port="COM2", baudrate = 115200,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS, timeout=1)
# Write ASCII Commands To TSI 5300 Flow Sensor
ser.write(b'?\r\n') # Ask Sensor if it is getting a signal (Returns "OK")
ser.write(b'SUS\r\n') # Set Flow type to SLPM (Returns "OK")
ser.write(b'SG0\r\n') # Set Flow Gas to Air (Returns "OK")
ser.write(b'SSR0005\r\n') # Set Sample Rate to 5ms (Returns "OK")
ser.write(b'LPZ\r\n') # Zero Low Pressure Sensor
# Read serial output to remove all 'OK's from buffer
while (i <= 4):
OK = ser.readline() # Read one line of serial and discard it
print(OK)
i += 1
# Ask for 5 Flow readings
ser.write(b'DAFxxxxx0005\r\n') # Read 5 sensor Flow Reading
ser.readline() # Read one line of serial data and discard it
byte = ser.readline() # Read one line of serial data and store it
print("Unfiltered Bytes: " + str(byte))
string = byte.decode('utf-8') # Convert from BYTE to STRING
array = string.split(',') # Convert from STRING to STRING ARRAY
print("String Array of all 5 readings: " + str(array))
# Convert each element of the ARRAY to FLOAT then add them together
for data in array:
index += 1
data = float(data)
total += data
avg = total / index # Find the average Flow in LPM
print("Average Flow Rate: " + str(avg) + " LPM")
time.sleep(1)
ser.close()
PJRC LINK:
https://forum.pjrc.com/threads/69679-Sending-ASCII-Commands-to-a-Teensy-3-5-Via-RS232-to-TTL-Converter

Yes, you should be able to connect it to the second USB port of the Teensy. This port acts as Host. Whether it works of course depends on which USB interface your flowmeter implements. If it implements some standard (e.g. CDC aka virtual serial or some HID interface) the USB Host lib can probably communicate with it. If they did a proprietary interface you would need to write a corresponding driver first...
I assume they implemented a CDC interface. You can easily check: if you connect the flowmeter to a PC a COM Port (Windows) should appear in the device manager.

I found the solution! It didn't matter which serial it was on (serial1 or serial2), however the problem is I had to start the teensy before the flowmeter and give the flowmeter 20sec to boot up before letting the teensy send any commands! This sensor is so slow though, it takes 50 seconds to fully boot up to the test screen! I just used a 5v relay to delay the flowmeter turning on. Thanks for your help!

Related

uPython for Node MCU and L293D Motor Shield

I am trying to use the L293D Motor Shield for Node MCU, controlling it with micropython. I've only found one code example for micropython, and it does not appear to work. Does anyone have any code examples I can start with (that works)?
Can you tell why my code example does not function as expected? Should I conclude the the problem is that my motor shield is dead/defective?
I've connected everything and checked that the wire connections are ok, and that the motor works. I've uploaded my code and it runs without errors.
from machine import Pin, PWM
import time
print("hello")
""" nodemcu pins from the motor shield """
pin1 = Pin(5, Pin.OUT) # D1
pin2 = Pin(4, Pin.OUT) # D2
pin3 = Pin(0, Pin.OUT) # D3
pin4 = Pin(2, Pin.OUT) # D4
""" named after the L9110 h-bridge pins """
BIN1 = PWM(pin1, freq=750)
BIN2 = PWM(pin3, freq=750)
AIN1 = PWM(pin2, freq=750)
AIN2 = PWM(pin4, freq=750)
""" TODO: variable speed """
speed = 950
def stop_all():
for each in (BIN1, BIN2, AIN1, AIN2):
each.duty(0)
def forward():
BIN1.duty(speed)
BIN2.duty(speed)
AIN1.duty(speed)
AIN2.duty(speed)
print("inside forward")
forward()
time.sleep(5)
stop_all()
It's just stone dead. No voltage on the output of the motor shield (with our without the motor connected) and not even the slightest hum from the motor.

Serial Communication between two ESP32

I have found examples of basic arduino to arduino serial communication but have been unable to get those working on ESP32 boards. I am trying to make the same thing work between two ESP32's The two are connected:
esp1 esp2
gnd to gnd
tx2 to rx2
rx2 to tx2
Simple sketches:
//transmit sketch
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("test...");
delay(1000);
}
//receive sketch
void setup() {
Serial.begin(9600);
}
void loop() {
String received = "";
while (Serial.available())
{
received = Serial.read();
Serial.println(received);
}
}
What else is required to make this work?
I think your code comes from a simpler world in which pins were always fixed and one UART was all you had available. With the ESP32 you should probably look for a solution more along these lines:
#include <HardwareSerial.h>
HardwareSerial Serial2(2); // use uart2
Serial2.begin(19200, SERIAL_8N1, 16, 17); // pins 16 rx2, 17 tx2, 19200 bps, 8 bits no parity 1 stop bit
I hope this helps. If you still have problems after this, they're likely to be either a) the board you're using doesn't use 16 & 17 for rx2 & tx2, or b) you need 10k pull-up (not series) resistors on both lines to stop them "floating" - however some boards take care of the pull-ups for you.
All the following criteria should be meet to make it work:
ESP32 board should not use the serial port you want to use to any embedded feature. So it should be free to use.
Make sure you are using the right pins:
U
Rx
Tx
Serial
40
41
Serial1
9
10
Serial2
16
17
Make sure lines are crossed, so Tx is bind to Rx on the other board and vice versa.
Make sure that the speed is the same on both board.
To see the result both ESP32 board should be connected to the PC via USB and a console should attached to the USB ports. You can use putty for this purpose to connect the USB ports. Two instances can be run for the two USB port. Make sure the speed setup in putty is the same as it is in the code.
Whatever you type in one console will be transferred and will appear on the other console.
Upload this code to both ESP32 board:
HardwareSerial &hSerial = Serial1; //can be Serial2 as well, just use proper pins
void setup()
{
Serial.begin(115200);//open serial via USB to PC on default port
hSerial.begin(115200);//open the other serial port
}
void loop()
{
if (Serial.available()) //check incoming on default serial (USB) from PC
{
hSerial.write(Serial.read()); // read it from UBS and send it to hSerial
}
if (hSerial.available()) //check incoming on other serial from the other board
{
Serial.write(hSerial.read()); //read it from hSerial and send it to UBS
}
}

DHT11 is not reading Temperature and Humidity when Arduino is powered on using DC power Supply (12V)

I am facing one problem. I have code which read temperature and humidity using DHT11 sensor. I uploaded following code using Arduino via USB serial, I can read values of temp, humidity. Values are being read as long as Arduino is connected to same laptop via USB.
TEMPERATURE AND HUMIDITY are being read as 0 when I power on Arduino using DC12v, 700MA adapter.
I want to deploy Arduino with DHT sensors connected with it in Greenhouse to read greenhouse environmental condition but when I power on using DC adapter or battery, it is giving "0" output. Note: values are verified when values are transferred via Ethernet to the webserver.
PLEASE HELP TO SOLVE THIS PROBLEM.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

Arduino not writing to file

I'd like the arduino to write to a file whenever an ajax call is made. The ajax works, but it doesn't write to the file. All other code inside the ajax handler does execute.
void handle_ajax(){
int startUrlIndex= HTTP_req.indexOf("button");
int endUrlIndex = HTTP_req.indexOf(" HTTP");
String url = HTTP_req.substring(startUrlIndex, endUrlIndex);
int startButtonIndex = url.indexOf("device-") + 7;// 7 is length of device-, I really just want the number.
int endButtonIndex = url.indexOf("&");
String button = url.substring(startButtonIndex, endButtonIndex);
int startStateIndex = url.indexOf("state=") + 6; // 6 is length of state=, I really just want the number.
String state = url.substring(startStateIndex);
int device = button.toInt();
int newState = state.toInt();
dim_light(device, newState * 12);
write_config("", "text");
}
bool write_config(String line, String text){
configFile = SD.open("config.ini", FILE_WRITE);
if(configFile){
configFile.write("Dipshit");
}
configFile.close();
Serial.println("Works.");
return true;
}
I don't see anything wrong with the code provided.
Check the basics first:
SD card is either a standard SD card or a SDHC card.
SD card is formatted with a FAT16 or FAT32 file system.
The correct pin has been used for the CS pin in the SD.begin() command. This depends on the hardware used. http://www.arduino.cc/en/Reference/SDCardNotes
The SPI is wired up correctly (pins 11, 12, and 13 on most Arduino boards).
The hardware SS pin is set as an output (even if it isn't used as the CS pin).
I know from past experience that these little Arduinos can run out of SRAM quite quickly when reading and writing to an SD card. The ReadWrite example program uses about 50% of the UNOs SRAM alone!!
To test if this is your problem, run the SD card read/write example program (with the correct CS pin in the SD.begin() command). If this works then the problem is likely that you have run out of SRAM. Try using an Arduino MEGA 2560 instead which has 4x the amount of SRAM.
Edit: The latest Arduino IDE (v1.6.8) actually calculates how much SRAM is used by global variables. It does not take into account local variables.
Found the problem: Ram
The arduino had insufficient ram at the point of opening the SD card resulting in a failure.
If anyone else ever encounters the same issue, you need 300 or more bytes of ram. Check this by serial printing FreeRam()

Why do some analog pins on my PIC32 report zero when disconnected and others report non-zero?

I am using a PIC32MX534F064L (datasheet), and trying to read several of its analog pins (marked AN0 to AN15).
With none of those pins connected to anything, I expect to read a value of zero. Instead on AN0 through AN5 I read values between 650 and 900. Only from the rest (AN6 through AN15) I get a value of zero.
When each of the pins is connected to a source, they report correctly. Each of the pins, AN0 through AN15 will report 0 for 0.0V, and 1023 for 3.3V.
I've tried sampling the values in pairs, and each separately. Whether sampled together or apart, AN0 will report non-zero values (usually around 700-800), and AN13 will report 0.
My first thought was that I somehow failed to properly set up the ADC. Here's my code:
#include <stdio.h>
#include <plib.h>
unsigned int an0;
unsigned int offset;
char buffer[100];
int main(void)
{
SYSTEMConfigPerformance(72000000L);
CloseADC10();
#define ADC_CONFIG1 ADC_MODULE_ON | ADC_FORMAT_INTG | \
ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
#define ADC_CONFIG2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | \
ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | \
ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
#define ADC_CONFIG3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
#define ADC_CONFIGSCAN SKIP_SCAN_ALL
#define ADC_CONFIGPORT ENABLE_AN0_ANA
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN0 );
OpenADC10( ADC_CONFIG1, ADC_CONFIG2, ADC_CONFIG3, \
ADC_CONFIGPORT, ADC_CONFIGSCAN );
EnableADC10();
while ( ! mAD1GetIntFlag() ) { }
while (1)
{
offset = 8 * ((~ReadActiveBufferADC10() & 0x01));
an0 = ReadADC10(offset);
sprintf(buffer, "AN0 = %u", an0);
}
return 0;
}
Looking in the PIC's datasheet, I noticed two things:
The pins AN0 to AN5, the ones mis-reporting non-zero values, are also CNx pins. These pins are "Change Notification" pins, that are meant to raise an interrupt when the value on the pins changes.
There is a "weak pull-up" that can be enabled on all CNx pins.
So I tried disabling the "weak pull-up" by using this line:
mCNClose();
Which disables all the CNx pins and their pull-ups. Sadly, this did not help. And when I checked the value of the CN-pull-up-register (CNPUE
What else can I try? Am I doing something wrong in my code?
Well, your expectation is wrong!
The minimum input resistance for source should be only few kilo ohms check datasheet.
If ADC pins is floating (not connected) the unpredicted value of internal parasitic current will cause that measuring value will be bigger than 0. Remember the ADC sample capacitor has only few pF capacity so floating pins can oscillate in wide voltage range also from external EM (electromagnetic) influences.
So, connect at least 1M resistors to pull down voltage on ADC pin, the resistance of pull down resistor is depended of ADC sample time. If ADC sample time is short than decrease the pull down value of resistor.
EDIT:
Check datasheet page 214 parameter AD17: Recommended Impedance of Analog Voltage Source is 5 KOhms. And AD15 say that that max. Leakage Current on ADC input pins can be +/-0.61 uA.
It's probably just noise, since the inputs are high impedance when nothing is connected. Try grounding the inputs (connect to 0V) as an experiment - the values should then be close to 0. If you need the inputs to be zero when nothing is connected then connect a pull-down resistor to each input (between input and 0V) to lower the impedance - a value of 10k ohms should do it.
Do not leave pins unconnected! The unconnected pin is essentially an antenna which could pick up voltages outside of the Vss and Vdd range. Section 2.10 of the datasheet says to not leave any pins unconnected (or if you do, configure them as outputs and drive them low.)
If you want to test your A2D, you can configure the pin as a digital output (the analog setting only overrides the digital input) and then drive it high and low to test.

Resources