laptop side:
Make sure you have the pyserial library installed. You can install it using pip using the PyCharm terminal tab:
pip install pyserial
Python code:
Code:
import serial
import time
if __name__ == '__main__':
ser = serial.Serial('COM3', 9600, timeout=1) # Adjust 'COM3' to your port
num_readings = 10
for _ in range(num_readings):
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').rstrip()
print(line)
time.sleep(1) # Delay between readings
ser.close()
ser.readline().decode('utf-8').rstrip() reads one line of data from the serial buffer,
which corresponds to the last complete line sent from the Arduino. Each call to ser.readline()
retrieves the next line of data available in the buffer, so if the Arduino is continuously
sending data, this command will read the most recent line that has been fully transmitted.
Arduino side skill code:
DiTemperature.h :
Code:
#ifndef DiTemperature_H
#define DiTemperature_H
// using Arduino hardware codes outside main:
#include <Arduino.h>
#include "LivinGrimoireLight.h"
/*
lm35 sketch
prints the temperature to the serial monitor
*/
class DiTemperature : public Skill {
private:
int _inPin; // analog pin
public:
DiTemperature(int inPin);
virtual void inOut(byte ear, byte skin, byte eye);
};
#endif
DiTemperature.c++ code:
Code:
#include <Arduino.h>
#include "DiTemperature.h"
#include "LivinGrimoireLight.h"
DiTemperature::DiTemperature(int inPin)
{
_inPin = inPin;
Serial.begin(9600); // Start serial communication at 9600 baud
}
void DiTemperature::inOut(byte ear, byte skin, byte eye) {
int value = analogRead(_inPin);
float millivolts = (value / 1024.0) * 5000;
float celsius = millivolts / 10;
Serial.print(celsius);
delay(1000);
// Serial.print(" \xC2\xB0"); // shows degree symbol
// Serial.println("C");
}
Arduino main code, with the temperature skill:
Code:
#include "DiTemperature.h"
#include "LivinGrimoireLight.h"
Chobit* c1;
void setup() {
Skill* s2 = new DiTemperature(0); // temperature skill created LM35 connected to analog pin 0
c1 = new Chobit();
c1->addSkill(s2);
}
void loop() {
c1->think(0, 0, 0);
}
Last edited by Admin on Sat Sep 28, 2024 7:58 pm; edited 1 time in total