Tobias Simetsreiter 3 years ago
parent
commit
5e49746b44
4 changed files with 107 additions and 24 deletions
  1. 1 0
      .gitignore
  2. 63 0
      plot.py
  3. 2 0
      requirements.txt
  4. 41 24
      test.ino

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+_venv

+ 63 - 0
plot.py

@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+import matplotlib.pyplot as plt
+import matplotlib.animation as animation
+from matplotlib import style
+import numpy as np
+import random
+import serial
+
+#initialize serial port
+ser = serial.Serial()
+ser.port = '/dev/ttyS3' #Arduino serial port
+ser.baudrate = 115200
+ser.timeout = 10 #specify timeout when using readline()
+ser.open()
+if ser.is_open==True:
+	print("\nAll right, serial port now open. Configuration:\n")
+	print(ser, "\n") #print serial parameters
+
+# Create figure for plotting
+fig = plt.figure()
+ax = fig.add_subplot(1, 1, 1)
+xs = [] #store trials here (n)
+ys = [] #store relative frequency here
+rs = [] #for theoretical probability
+
+# This function is called periodically from FuncAnimation
+def animate(i, xs, ys):
+
+    #Aquire and parse data from serial port
+    line=ser.readline()      #ascii
+    line_as_list = line.split(b',')
+    i = int(line_as_list[0])
+    relProb = line_as_list[1]
+    relProb_as_list = relProb.split(b'\n')
+    relProb_float = float(relProb_as_list[0])
+	
+	# Add x and y to lists
+    xs.append(i)
+    ys.append(relProb_float)
+    rs.append(0.5)
+
+    # Limit x and y lists to 20 items
+    #xs = xs[-20:]
+    #ys = ys[-20:]
+
+    # Draw x and y lists
+    ax.clear()
+    ax.plot(xs, ys, label="Experimental Probability")
+    ax.plot(xs, rs, label="Theoretical Probability")
+
+    # Format plot
+    plt.xticks(rotation=45, ha='right')
+    plt.subplots_adjust(bottom=0.30)
+    plt.title('This is how I roll...')
+    plt.ylabel('Relative frequency')
+    plt.legend()
+    plt.axis([1, None, 0, 1.1]) #Use for arbitrary number of trials
+    #plt.axis([1, 100, 0, 1.1]) #Use for 100 trial demo
+
+# Set up plot to call animate() function periodically
+ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
+plt.show()

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+matplotlib
+pyserial

+ 41 - 24
test.ino

@@ -16,7 +16,13 @@ Atm_led led1;
 
 int poti_pin = A0;
 Atm_analog poti;
-float shouldTemp;
+
+int dial_pin_1 = 12;
+int dial_pin_2 = 14;
+Atm_encoder dial;
+
+float shouldTemp = 0;
+float isTemp = 100;
 
 bool wpsRunning = false;
 
@@ -29,7 +35,7 @@ Atm_timer sensorTimer;
 DS18B20Events thermometer(0);
 
 void setup() {
-  delay(10000);
+  delay(2000);
   Serial.begin(115200);
   Serial.println("Booting...");
   
@@ -43,34 +49,37 @@ void setup() {
       wpsSetup();
      } );
     // .onPress( led1, led1.EVT_TOGGLE );
-    
-  // sensor.begin()
-  sensorTimer.begin( 100 )
-    .repeat( ATM_COUNTER_OFF )
-    .onTimer( [] (int idx, int v, int up ) {
-      thermometer.loop();
-     } )
-    .start();
+
+  
+  dial.begin(dial_pin_1, dial_pin_2)
+    .range( 1, 120 )
+    .onChange( [] (int idx, int v, int up ) {
+      shouldTemp = (float)v;
+      // Serial.printf("Raw: %d ShouldTemp: %.10f\n", v, shouldTemp);
+      checkTemp();
+    });
   
+  /*
   poti.begin( poti_pin )
     .onChange( [] (int idx, int v, int up ) {
-      Serial.printf("Poti: '%d'\n", v);
       shouldTemp = (float)v / 20.0;
-      Serial.printf("ShouldTemp: %.10f\n", shouldTemp);
+      // Serial.printf("Raw: %d ShouldTemp: %.10f\n", v, shouldTemp);
+      checkTemp();
      });
   // sensorTimer.trace( Serial );
+  */
 
   DS18B20Events::setup(&sensor); // Link DS18B20Events to DallasTemperature object
 	DS18B20Events::setInterval(200); // Set min interval in milliseconds between 2 measurements. Default 10000 set in setup() method.
-	
-  thermometer.onChange = temperatureChanged;
-
-  /*
-  pinMode(13, OUTPUT); //GPIO 13 = Pin D7
-
 
+  sensorTimer.begin( 100 )
+    .repeat( ATM_COUNTER_OFF )
+    .onTimer( [] (int idx, int v, int up ) {
+      thermometer.loop();
+     } )
+    .start();
+  thermometer.onChange = temperatureChanged;
 
-  */
   Serial.println("Wifi Setup...");
   WiFi.mode(WIFI_STA);
   WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str());
@@ -90,11 +99,10 @@ void loop() {
 
 void temperatureChanged(uint8_t index, float tempC)
 {
-	Serial.printf("%d new tempC at index %d\n", tempC, index);
-  if (tempC < shouldTemp){
-    led1.on();
-  } else {
-    led1.off();
+  if (tempC > 0){
+    isTemp = tempC;
+    // Serial.printf("isTemp: %.10f index %d\n", isTemp, index);
+    checkTemp();
   }
 }
 
@@ -117,6 +125,15 @@ void wpsSetup(){
   }
 }
 
+void checkTemp(){
+  if (isTemp < shouldTemp){
+    led1.on();
+  } else {
+    led1.off();
+  }
+  Serial.printf("%d,%.10f,%.10f\n", millis(),shouldTemp,isTemp);
+}
+
 String boolToString(bool in){
   return in ? "true" : "false";
 }