+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

jspspikeAdded lab4 files99480b6

master
6.0 KiB176 linesraw
1// -------------------------------------------------------------------
2// File name: Blynk.c
3// Description: This code is used to bridge the TM4C123 board and the Blynk Application
4//              via the ESP8266 WiFi board
5// Author: Mark McDermott and Andrew Lynch (Arduino source)
6// Converted to EE445L style Jonathan Valvano
7// Orig gen date: May 21, 2018
8// Last update: Sept 20, 2018
9//
10// Download latest Blynk library here:
11//   https://github.com/blynkkk/blynk-library/releases/latest
12//
13//  Blynk is a platform with iOS and Android apps to control
14//  Arduino, Raspberry Pi and the likes over the Internet.
15//  You can easily build graphic interfaces for all your
16//  projects by simply dragging and dropping widgets.
17//
18//   Downloads, docs, tutorials: http://www.blynk.cc
19//   Sketch generator:           http://examples.blynk.cc
20//   Blynk community:            http://community.blynk.cc
21//
22//------------------------------------------------------------------------------
23
24// TM4C123       ESP8266-ESP01 (2 by 4 header)
25// PE5 (U5TX) to Pin 1 (Rx)
26// PE4 (U5RX) to Pin 5 (TX)
27// PE3 output debugging
28// PE2 nc
29// PE1 output    Pin 7 Reset
30// PE0 input     Pin 3 Rdy IO2
31//               Pin 2 IO0, 10k pullup to 3.3V  
32//               Pin 8 Vcc, 3.3V (separate supply from LaunchPad 
33// Gnd           Pin 4 Gnd  
34// Place a 4.7uF tantalum and 0.1 ceramic next to ESP8266 3.3V power pin
35// Use LM2937-3.3 and two 4.7 uF capacitors to convert USB +5V to 3.3V for the ESP8266
36// http://www.ti.com/lit/ds/symlink/lm2937-3.3.pdf
37#include <stdint.h>
38#include <string.h>
39#include <stdlib.h>
40#include "tm4c123gh6pm.h"
41#include "ST7735.h"
42#include "PLL.h"
43#include "Timer2.h"
44#include "Timer3.h"
45#include "UART.h"
46#include "PortF.h"
47#include "esp8266.h"
48
49void EnableInterrupts(void);    // Defined in startup.s
50void DisableInterrupts(void);   // Defined in startup.s
51void WaitForInterrupt(void);    // Defined in startup.s
52
53uint32_t LED;      // VP1
54uint32_t LastF;    // VP74
55// These 6 variables contain the most recent Blynk to TM4C123 message
56// Blynk to TM4C123 uses VP0 to VP15
57char serial_buf[64];
58char Pin_Number[2]   = "99";       // Initialize to invalid pin number
59char Pin_Integer[8]  = "0000";     //
60char Pin_Float[8]    = "0.0000";   //
61uint32_t pin_num; 
62uint32_t pin_int;
63 
64// ----------------------------------- TM4C_to_Blynk ------------------------------
65// Send data to the Blynk App
66// It uses Virtual Pin numbers between 70 and 99
67// so that the ESP8266 knows to forward the data to the Blynk App
68void TM4C_to_Blynk(uint32_t pin,uint32_t value){
69  if((pin < 70)||(pin > 99)){
70    return; // ignore illegal requests
71  }
72// your account will be temporarily halted 
73  // if you send too much data
74  ESP8266_OutUDec(pin);       // Send the Virtual Pin #
75  ESP8266_OutChar(',');
76  ESP8266_OutUDec(value);      // Send the current value
77  ESP8266_OutChar(',');
78  ESP8266_OutString("0.0\n");  // Null value not used in this example
79}
80 
81 
82// -------------------------   Blynk_to_TM4C  -----------------------------------
83// This routine receives the Blynk Virtual Pin data via the ESP8266 and parses the
84// data and feeds the commands to the TM4C.
85void Blynk_to_TM4C(void){int j; char data;
86// Check to see if a there is data in the RXD buffer
87  if(ESP8266_GetMessage(serial_buf)){  // returns false if no message
88    // Read the data from the UART5
89#ifdef DEBUG1
90    j = 0;
91    do{
92      data = serial_buf[j];
93      UART_OutChar(data);        // Debug only
94      j++;
95    }while(data != '\n');
96    UART_OutChar('\r');        
97#endif
98           
99// Rip the 3 fields out of the CSV data. The sequence of data from the 8266 is:
100// Pin #, Integer Value, Float Value.
101    strcpy(Pin_Number, strtok(serial_buf, ","));
102    strcpy(Pin_Integer, strtok(NULL, ","));       // Integer value that is determined by the Blynk App
103    strcpy(Pin_Float, strtok(NULL, ","));         // Not used
104    pin_num = atoi(Pin_Number);     // Need to convert ASCII to integer
105    pin_int = atoi(Pin_Integer);  
106  // ---------------------------- VP #1 ----------------------------------------
107  // This VP is the LED select button
108    if(pin_num == 0x01)  {  
109      LED = pin_int;
110      PortF_Output(LED<<2); // Blue LED
111#ifdef DEBUG3
112      Output_Color(ST7735_CYAN);
113      ST7735_OutString("Rcv VP1 data=");
114      ST7735_OutUDec(LED);
115      ST7735_OutChar('\n');
116#endif
117    }                               // Parse incoming data        
118#ifdef DEBUG1
119    UART_OutString(" Pin_Number = ");
120    UART_OutString(Pin_Number);
121    UART_OutString("   Pin_Integer = ");
122    UART_OutString(Pin_Integer);
123    UART_OutString("   Pin_Float = ");
124    UART_OutString(Pin_Float);
125    UART_OutString("\n\r");
126#endif
127  }  
128}
129
130void SendInformation(void){
131  uint32_t thisF;
132  thisF = PortF_Input();
133// your account will be temporarily halted if you send too much data
134  if(thisF != LastF){
135    TM4C_to_Blynk(74, thisF);  // VP74
136#ifdef DEBUG3
137    Output_Color(ST7735_WHITE);
138    ST7735_OutString("Send 74 data=");
139    ST7735_OutUDec(thisF);
140    ST7735_OutChar('\n');
141#endif
142  }
143  LastF = thisF;
144}
145
146  
147int main(void){       
148  PLL_Init(Bus80MHz);   // Bus clock at 80 MHz
149  DisableInterrupts();  // Disable interrupts until finished with inits
150  PortF_Init();
151  LastF = PortF_Input();
152#ifdef DEBUG3
153  Output_Init();        // initialize ST7735
154  ST7735_OutString("EE445L Lab 4D\nBlynk example\n");
155#endif
156#ifdef DEBUG1
157  UART_Init(5);         // Enable Debug Serial Port
158  UART_OutString("\n\rEE445L Lab 4D\n\rBlynk example");
159#endif
160  ESP8266_Init();       // Enable ESP8266 Serial Port
161  ESP8266_Reset();      // Reset the WiFi module
162  ESP8266_SetupWiFi();  // Setup communications to Blynk Server  
163  
164  Timer2_Init(&Blynk_to_TM4C,800000); 
165  // check for receive data from Blynk App every 10ms
166
167  Timer3_Init(&SendInformation,40000000); 
168  // Send data back to Blynk App every 1/2 second
169  EnableInterrupts();
170
171  while(1) {   
172    WaitForInterrupt(); // low power mode
173  }
174}
175
176