ArduinoPI Python - SERIAL CONNECTION LOST DATA - python

Intent: Control arduino uno from serial port
Tools:
https://github.com/JanStevens/ArduinoPi-Python
I got the server working on both my mac and my Model b+ Raspberry.
The browser behaves as shown in the picture below in both situations.
To me it looks like the server sent the message to Arduino successfully. But the data somehow gets lost on the way. The Arduino board resets every time I access the url in my browser. I googled and found that a 10uF capacitor between ground and reset pins would prevent the reset from happening. It did, but pin 3 won't go "HIGH". I got a LED+RESISTOR plugged on pin 3 and ground accordingly. I can see the Rx led blinking every time I access the url. So it makes me think that the Arduino is misunderstanding the command from my Flask sever.
OG Arduino code:
String cmd;
bool cmdRec = false;
void setup()
{
//Start the connection with the Raspberry Pi
Serial1.begin(115200);
// Start the connection with the Laptop, for debugging only!
//Serial.begin(115200);
}
void loop()
{
handleCmd();
}
void serialEvent1() {
while(Serial1.available() > 0) {
char inByte = (char)Serial1.read();
if(inByte == ':') {
cmdRec = true;
return;
} else if(inByte == '#') {
cmd = "";
cmdRec = false;
return;
} else {
cmd += inByte;
return;
}
}
}
void handleCmd() {
if(!cmdRec) return;
// If you have problems try changing this value,
// my MEGA2560 has a lot of space
int data[80];
int numArgs = 0;
int beginIdx = 0;
int idx = cmd.indexOf(",");
String arg;
char charBuffer[20];
while (idx != -1) {
arg = cmd.substring(beginIdx, idx);
arg.toCharArray(charBuffer, 16);
data[numArgs++] = atoi(charBuffer);
beginIdx = idx + 1;
idx = cmd.indexOf(",", beginIdx);
}
// And also fetch the last command
arg = cmd.substring(beginIdx);
arg.toCharArray(charBuffer, 16);
data[numArgs++] = atoi(charBuffer);
// Now execute the command
execCmd(data);
cmdRec = false;
}
// For advanced function like switch all the leds in RGB
void execCmd(int* data) {
switch(data[0]) {
case 101:
{
for(int i = 2; i < (data[1]*2)+1; i+=2) {
pinMode(data[i], OUTPUT);
analogWrite(data[i], data[i+1]);
}
}
break;
case 102:
{
pinMode(data[1], INPUT);
int sensor = analogRead(data[1]);
Serial1.println(sensor);
}
break;
case 103:
{
String result = "";
int sensor = 0;
for(int j = 2; j < data[1]+2; j++) {
pinMode(data[j], INPUT);
sensor = analogRead(data[j]);
result += String(sensor)+",";
}
Serial1.println(result);
}
break;
default:
{
pinMode(data[0], OUTPUT);
analogWrite(data[0], data[1]);
}
break;
}
}
It does not compile this way. So I uncommented the second Serial.begin line and deleted all the "Serial1." appearances on the code. I can't see no action on the arduino IDE serial when I test it on my mac.

As the code was written with an Arduino Mega that got 2 or 3 serial ports, void serialevent1() is triggered when there is communication going on the the MEGA's SERIAL1 port. Since I am working on the UNO, that only have 1 serial port, all i had to do was delete the "1" before the parenthesis and all worked as supposed.
void serialEvent() { }

Related

Trying to program ESP32 with manual switch feedback

i want to make a program where i can manage appliances using MQTT messages as well as i want to take feedback using normal switches, i tried the following code but in that case switch one and relay on is operating as expected but switch2 & switch 3 are getting called in loop and switch 2 is automatically getting converted from 0 to 1 and hence if condition is getting verified and its getting in loop, please help
#include <WiFi.h>
#include <PubSubClient.h>
#include "ArduinoJson.h"
#define relayOne 15
#define switchOne 32
#define relayTwo 2
#define switchTwo 35
#define relayThree 4
#define switchThree 34
#define relayFour 22
#define switchFour 39
const char* ssid = "*******";//replace this with your wifi access point
const char * password = ""*******";//"; //replace with your wifi password
const char * host = "*.*.*.*"; //IP address of machine on which broker is installed
const int port = 1883;
const char * mqttUser = "user";
const char * mqttPassword = "user";
WiFiClient espClient;
PubSubClient client(espClient);
StaticJsonBuffer < 55 > jsonBuffer;
void callback(char * topic, byte * payload, unsigned int length) {
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is:");
Serial.println(length);
Serial.print("Data Received From Broker:");
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char) payload[i];
}
//Serial.println(messageTemp);
const size_t capacity = JSON_OBJECT_SIZE(2) + 20;
DynamicJsonBuffer jsonBuffer(capacity);
//const char* json = "{\"pin\":14,\"value\":1}";
const char * json = messageTemp.c_str();
JsonObject & root = jsonBuffer.parseObject(json);
int pin = root["pin"];
int value = root["value"];
Serial.println(pin);
Serial.println(value);
digitalWrite(pin, value);
}
void setup() {
Serial.begin(115200);
pinMode(relayOne, OUTPUT);
pinMode(switchOne, INPUT_PULLUP);
pinMode(relayTwo, OUTPUT);
pinMode(switchTwo, INPUT_PULLUP);
pinMode(relayThree, OUTPUT);
pinMode(switchThree, INPUT_PULLUP);
pinMode(relayFour, OUTPUT);
pinMode(switchFour, INPUT_PULLUP);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi..");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
yield();
}
Serial.println("Connected to the WiFi network");
client.setServer(host, port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword)) {
Serial.println("connected to MQTT broker");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(500);
}
}
Serial.println("ESP32 AS SUBSCRIBER");
client.subscribe("IOT"); //topic name="Sub"
}
void MQTTPOST(int pin, int value)
{
Serial.println("in mqttpost");
//payload formation begins here
String payload ="{";
payload +="\"Pin\":"; payload +=pin; payload +=",";
payload +="\"Value\":"; payload +=value;
payload +="}";
char attributes[1000];
payload.toCharArray( attributes, 1000 );
client.publish("IOT1", attributes); //topic="Pub" MQTT data post command.
Serial.println( attributes );
Serial.println("");
}
int switchOneValue = 0;
int switchTwoValue = 0;
int switchThreeValue = 0;
int switchFourValue = 0;
void loop() {
client.loop();
int valueSwitchOne = digitalRead(switchOne);
Serial.println(valueSwitchOne);
Serial.println(switchOneValue);
if(valueSwitchOne == 0 && switchOneValue == 0){
digitalWrite(relayOne, 1);
switchOneValue = 1;
MQTTPOST(relayOne, 1);
Serial.println("on");
delay(200);
}else if(valueSwitchOne == 1 && switchOneValue == 1) {
digitalWrite(relayOne, 0);
switchOneValue = 0;
MQTTPOST(relayOne, 0);
Serial.println("off");
delay(200);
}
int valueSwitchTwo = digitalRead(switchTwo);
Serial.println(valueSwitchTwo);
Serial.println(switchTwoValue);
delay(1000);
if(valueSwitchTwo == 0 && switchTwoValue == 0){
digitalWrite(relayTwo, 1);
switchTwoValue = 1;
MQTTPOST(relayTwo, 1);
Serial.println("on2");
delay(200);
}else if(valueSwitchTwo == 1 && switchTwoValue == 1) {
digitalWrite(relayTwo, 0);
switchTwoValue = 0;
MQTTPOST(relayTwo, 0);
Serial.println("off2");
delay(200);
}
int valueSwitchThree = digitalRead(switchThree);
if(valueSwitchThree == 0 && switchThreeValue == 0){
digitalWrite(relayThree, 1);
switchThreeValue = 1;
MQTTPOST(relayThree, 1);
Serial.println("on");
delay(200);
}else if(valueSwitchThree == 1 && switchThreeValue == 1) {
digitalWrite(relayThree, 0);
switchThreeValue = 0;
MQTTPOST(relayThree, 0);
Serial.println("off");
delay(200);
}
}
here is a quote from documentation on esp32:
Appendix A – ESP32 Pin Lists
A.1. Notes on ESP32 Pin Lists
Table 24: Notes on ESP32 Pin Lists
GPIO pins 34-39 are input-only. These pins do NOT feature an output
driver or internal pull-up/pull-down circuitry.
digitalRead() can return arbitrary value for those pins because there are no built-in pull-up resistors there - they are effectively dangling in the air.
It means that following code CAN NOT WORK without external pull-up resistors:
#define switchTwo 35
#define switchThree 34
#define switchFour 39
pinMode(switchTwo, INPUT_PULLUP); // THIS DOES NOT WORK for GPIOs: 34-39!!!
pinMode(switchThree, INPUT_PULLUP); // THIS DOES NOT WORK for GPIOs: 34-39 !!!
pinMode(switchFour, INPUT_PULLUP); // THIS DOES NOT WORK for GPIOs: 34-39 !!!
// unpredictable values are here when button is not pressed
int valueSwitchTwo = digitalRead(35);
int valueSwitchThree = digitalRead(34);
Solution:
either use different pins for the switches
or add real hardware resistors into your schematics

How to send a decimal number from python to arduino

i am trying to send a decimal number from python to arduino, the problems is data is sent in an other type (i don't know which one maybe binary), so i did not get the desired response!
here is my arduino code
void setup() {
// put your setup code here, to run once:
pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
char k = Serial.read();
if (k > 500) {
analogWrite(10, 255);
delay(1000);
}
if (150 < k < 500) {
analogWrite(10, 128);
}
else if (k < 150) {
analogWrite(10, 0);
delay(1000);
}
Serial.print(k);
}
}
& here is python code
import serial
import struct
arduinoData = serial.Serial('com3',9600)
data = 0
while(1==1):
serialcmd = input("serial command: ")
arduinoData.write (struct.pack('>B',serialcmd))
data = arduinoData.readline()
print(data)

how to receive only 1 single value form python to arduino

I'm new to this!!
python code
i = results[1]
i *= 100
if i >= 75:
print('Recognised as owner!')
arduino.write(str.encode('1'))
else:
print('not matches')
serial monitor
111111111111111111111111111111111111111111111111111111111111111111...
Arduino code
#include <Servo.h>
Servo servo;
char value1;
void setup()
{
servo.attach(8);
servo.write(0);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
value1 = Serial.read();
Serial.print(value1);
if (value1 == '1') {
digitalWrite(13, HIGH);
servo.write(90);
delay(100);
}
for (int i = 10; i > 0; i--){
Serial.println("go inside!!, hurry up !!");
delay(1000);
}
servo.write(0);
Serial.println("door is closed");
delay(1000);
}
question:
this program can handle the door system based on face recognition
the Arduino takes the value of "1" over and over. so later servo always looping every 10 s. because the program cant handles 1 single value.
my goal is to make sure that the Arduino only get 1 single input. so later the servo is running well
ty

calling serial.read from other function

I'm trying to connect python with Arduino code using serial but I cannot call serial.read() within the led_on_off() function.
This is the Arduino code:
int led=13;
int val=0;
char functionname='K';
#include <string.h>
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite (led, LOW);
Serial.println("Connection established...");
}
void loop()
{
functionname = Serial.read();
if (functionname= 'L')
{
led_on_off();
}
}//void loop
void led_on_off()
{
val=Serial.read()
if (val= 1)
digitalWrite(led,HIGH)
else if (val == 0)
digitalWrite(led,LOW)
}
And this is the python code:
import serial
Arduino_Serial = serial.Serial('com18',9600) # Create Serial port object called arduinoSerialData
print(Arduino_Serial.readline()) # read the serial data and print it as line
print("Enter L to ON LED and M dc motor")
input_data = input()
Arduino_Serial.write(input_data.encode())
print(Arduino_Serial.readline())
input_value=input("enter 1 or 0")
Arduino_Serial.write(input_value.encode())
my expectation is to get input (1 or 0) from python code and process it within the led_on_off function in Arduino code using the serial.read() function and turn on or off the led at pin 13.
As already stated by #KIIV your Arduino code has some syntax errors. Besides that, you are sending a string from your Python script (input function) so you need to read it as a string on Arduino. Something like this would do the job:
Python script:
import serial
# Create Serial port object called arduinoSerialData
Arduino_Serial = serial.Serial('com18', 9600)
# read the serial data and print it as line
print(Arduino_Serial.readline())
input_data = input("Enter L to ON LED and M dc motor: ")
Arduino_Serial.write(input_data.encode())
print("Received Command:", Arduino_Serial.readline())
while True:
input_value = input("enter 1 or 0")
Arduino_Serial.write(input_value.encode())
Arduino code:
int led = 13;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop()
{
//void loop
if (Serial.available())
{
String functionname = Serial.readString();
if (functionname == "L")
{
Serial.println("Command received from Arduino!");
while (1)
{
led_on_off();
}
}
}
}
void led_on_off()
{
if (Serial.available())
{
String val = Serial.readString();
if (val == "1")
digitalWrite(led, HIGH);
else if (val == "0")
digitalWrite(led, LOW);
}
}

Communication from PySerial to Arduino: corrupted data

I need to send orders from a python code (computer) to an Arduino using PySerial via USB. The orders are strings of coma separated values, ended with a termination line character, i.e. "199,8,0,1,12345,0,0\n". The maximum size for an order is 32 bytes.
Here is the code used to send 10 times the same order from python to an Arduino (for debugging purpose).
import serial
ser = serial.Serial('/dev/cu.usbmodemFA141', 57600)
for i in range(10):
ser.write("199,8,0,1,12345,0,0\n")
On the Arduino side, the order is received, processed, and reformulated using this code:
char c = 0;
int it = 0;
bool newData = false;
String lastMessage;
String orderNumber, slaveNumber, singleOrReturn, order, orderComplement, valueI, valueII;
void saveSubStr(String subStr, int iterator){
switch (iterator) {
case 1: orderNumber = subStr; break;
case 2: slaveNumber = subStr; break;
case 3: singleOrReturn = subStr; break;
case 4: order = subStr; break;
case 5: orderComplement = subStr; break;
case 6: valueI = subStr; break;
case 7: valueII = subStr; break;
default: break; // it might be that some bugs leads the code here
}
}
void formulateLastMessage(){
lastMessage = orderNumber+","+slaveNumber+","+singleOrReturn+","+order+","+orderComplement+","+valueI+","+valueII+"\n";
}
void setup() {
Serial.begin(57600);
delay(1000);
}
void loop() {
it = 0;
while (Serial.available()){
c = Serial.read();
if (c!= '\n'){
if (c != ','){
subStr += c;
}
else {
it ++;
saveSubStr(subStr, it);
subStr = ""
}
}
else {
it ++;
saveSubStr(subStr, it);
subStr = "";
newData = true;
it = 0;
}
}
if (newData) {
formulateLastMessage();
}
newData = false;
}
If I send the same order from python 10 time for instance, here are the values I get for the global variable lastMessage in the Arduino:
199,8,0,1,12345,0,0
199,8,0,1,12345,0,0
199,8,0,1,12345,0,0
0,1,12345,0,0,0,0
199,8,0,1,12345,0,0
199199,8,0,1,12345,0,0
12345,0,0,1,12345,0,0
199,8,0,1,12345,0,0
199,8,0,1,12345,0,0
I don't understand why most of the time I receive the data correctly, but sometime the string order is broken. Does anyone has an idea what could cause this problem?

Categories